Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/cli/src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
10 changes: 8 additions & 2 deletions packages/cli/src/commands/init/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import init from './init';
import {CLIError} from '@react-native-community/cli-tools';

export default {
func: init,
Expand Down Expand Up @@ -63,8 +64,13 @@ export default {
parse: (val: string): Record<string, string> => {
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)];
}),
);
},
Expand Down
30 changes: 20 additions & 10 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -116,7 +117,7 @@ function attachCommand<C extends Command<boolean>>(
throw new Error('A command must be either attached or detached');
}
} catch (error) {
handleError(error as Error);
handleError(error);
}
});

Expand All @@ -141,7 +142,7 @@ async function run(platformName?: string) {
try {
await setupAndRun(platformName);
} catch (e) {
handleError(e as Error);
handleError(e);
}
}

Expand Down Expand Up @@ -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);
}
}
}

Expand All @@ -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.',
);
Expand All @@ -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');
Expand Down
Loading