diff --git a/packages/cli/src/bin.ts b/packages/cli/src/bin.ts index 365a5bf7f..08c5fee91 100755 --- a/packages/cli/src/bin.ts +++ b/packages/cli/src/bin.ts @@ -14,4 +14,5 @@ if (semver.satisfies(process.version, versionRanges.NODE_JS)) { `React Native needs Node.js ${versionRanges.NODE_JS}. You're currently on version ${process.version}. Please upgrade Node.js to a supported version and try again.`, )}`, ); + process.exitCode = 1; } diff --git a/packages/cli/src/commands/init/index.ts b/packages/cli/src/commands/init/index.ts index f89397840..c7615dc28 100644 --- a/packages/cli/src/commands/init/index.ts +++ b/packages/cli/src/commands/init/index.ts @@ -1,4 +1,5 @@ import init from './init'; +import {CLIError} from '@react-native-community/cli-tools'; export default { func: init, @@ -63,8 +64,13 @@ export default { parse: (val: string): Record => { return Object.fromEntries( val.split(',').map((option) => { - const [key, value] = option.split('='); - return [key, value]; + const separator = option.indexOf('='); + if (separator <= 0) { + throw new CLIError( + 'Yarn config options must use the format key=value,key2=value2.', + ); + } + return [option.slice(0, separator), option.slice(separator + 1)]; }), ); }, diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index dd8fd2223..ef5f525b1 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -20,7 +20,8 @@ const program = new CommanderCommand() .version(pkgJson.version, '-v --version', 'Output the current version') .enablePositionalOptions(); -const handleError = (err: Error) => { +const handleError = (error: unknown) => { + const err = error instanceof Error ? error : new Error(String(error)); logger.enable(); if (program.opts().verbose) { logger.error(err.message); @@ -116,7 +117,7 @@ function attachCommand>( throw new Error('A command must be either attached or detached'); } } catch (error) { - handleError(error as Error); + handleError(error); } }); @@ -141,7 +142,7 @@ async function run(platformName?: string) { try { await setupAndRun(platformName); } catch (e) { - handleError(e as Error); + handleError(e); } } @@ -188,10 +189,16 @@ async function setupAndRun(platformName?: string) { to only load the configuration for the specific platform. */ if (isCommandPassed('config')) { - const platformIndex = process.argv.indexOf('--platform'); - - if (platformIndex !== -1 && platformIndex < process.argv.length - 1) { - selectedPlatform = process.argv[platformIndex + 1]; + for (let index = 3; index < process.argv.length; index++) { + const arg = process.argv[index]; + if (arg === '--') { + break; + } + if (arg === '--platform') { + selectedPlatform = process.argv[++index]; + } else if (arg.startsWith('--platform=')) { + selectedPlatform = arg.slice('--platform='.length); + } } } @@ -216,8 +223,11 @@ async function setupAndRun(platformName?: string) { * When there is no `package.json` found, the CLI will enter `detached` mode and a subset * of commands will be available. That's why we don't throw on such kind of error. */ - if ((error as Error).message.includes("We couldn't find a package.json")) { - logger.debug((error as Error).message); + if ( + error instanceof Error && + error.message.includes("We couldn't find a package.json") + ) { + logger.debug(error.message); logger.debug( 'Failed to load configuration of your project. Only a subset of commands will be available.', ); @@ -240,7 +250,7 @@ async function setupAndRun(platformName?: string) { argv.push('--platform-name', platformName); } - program.parse(argv); + await program.parseAsync(argv); } const bin = require.resolve('./bin');