|
| 1 | +import { type SpawnOptions } from 'node:child_process'; |
| 2 | +import { spawn } from './spawn'; |
| 3 | +import path from 'node:path'; |
| 4 | +import fs from 'fs-extra'; |
| 5 | +import assert from 'node:assert'; |
| 6 | + |
| 7 | +// This is a special case for calling bob from the XCode scripts |
| 8 | +// XCode scripts don't have the node binary properly set |
| 9 | +// We expose an env value for node instead. |
| 10 | +const NODE_BINARY = process.env['NODE_BINARY'] || 'node'; |
| 11 | + |
| 12 | +/** |
| 13 | + * Runs the React Native Community CLI with the specified arguments |
| 14 | + */ |
| 15 | +export async function runRNCCli( |
| 16 | + args: string[], |
| 17 | + options: SpawnOptions = { |
| 18 | + stdio: 'ignore', |
| 19 | + } |
| 20 | +) { |
| 21 | + const rncCliBinaryName = await getCliBinaryName(); |
| 22 | + |
| 23 | + const RNC_CLI_BINARY_PATH = path.resolve( |
| 24 | + process.cwd(), // We are always expected to run in the library |
| 25 | + 'node_modules', |
| 26 | + '.bin', |
| 27 | + rncCliBinaryName |
| 28 | + ); |
| 29 | + |
| 30 | + return await spawn(NODE_BINARY, [RNC_CLI_BINARY_PATH, ...args], options); |
| 31 | +} |
| 32 | + |
| 33 | +async function getCliBinaryName(): Promise<string> { |
| 34 | + const rncCliPackagePath = await spawn(NODE_BINARY, [ |
| 35 | + '-e', |
| 36 | + `console.log(require.resolve('@react-native-community/cli/package.json'))`, |
| 37 | + ]); |
| 38 | + |
| 39 | + const rncCliPackage = await fs.readJson(rncCliPackagePath); |
| 40 | + const binProperty = rncCliPackage.bin as Record<string, string>; |
| 41 | + assert( |
| 42 | + typeof binProperty === 'object', |
| 43 | + "React Native CLI doesn't specify proper binaries" |
| 44 | + ); |
| 45 | + |
| 46 | + const binaries = Object.keys(binProperty); |
| 47 | + const rncCliBinaryName = binaries[0] as string; |
| 48 | + assert( |
| 49 | + typeof rncCliBinaryName === 'string', |
| 50 | + "React Native Community CLI doesn't have any binaries to run" |
| 51 | + ); |
| 52 | + |
| 53 | + return rncCliBinaryName; |
| 54 | +} |
0 commit comments