Skip to content

Commit

Permalink
fix: avoid catching init errors and avoid printing their stack (#1742)
Browse files Browse the repository at this point in the history
* Defer from "init" to index.ts to handle errors

* Extend a CLIError to avoid printing stack

* Fixing tests by relaxing the stderr match

* Assert non-zero exit code if failure is expected
  • Loading branch information
kraenhansen authored Nov 7, 2022
1 parent 74bf421 commit f0f302d
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion __e2e__/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ test('init fails if the directory already exists', () => {
fs.mkdirSync(path.join(DIR, 'TestInit'));

const {stderr} = runCLI(DIR, ['init', 'TestInit'], {expectedFailure: true});
expect(stderr).toBe(
expect(stderr).toContain(
'error Cannot initialize new project because directory "TestInit" already exists.',
);
});
Expand Down
2 changes: 2 additions & 0 deletions jest/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ ${chalk.bold('args:')} ${(args || []).join(' ')}
${chalk.bold('stderr:')} ${result.stderr}
${chalk.bold('stdout:')} ${result.stdout}
${chalk.bold('code:')} ${result.code}`);
} else if (options.expectedFailure && result.code === 0) {
throw new Error("Expected command to fail, but it didn't");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default class DirectoryAlreadyExistsError extends Error {
import {CLIError} from '@react-native-community/cli-tools';

export default class DirectoryAlreadyExistsError extends CLIError {
constructor(directory: string) {
super(
`Cannot initialize new project because directory "${directory}" already exists.`,
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/src/commands/init/errors/HelloWorldError.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default class HelloWorldError extends Error {
import {CLIError} from '@react-native-community/cli-tools';

export default class HelloWorldError extends CLIError {
constructor() {
super(
'Project name shouldn\'t contain "HelloWorld" name in it, because it is CLI\'s default placeholder name.',
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/src/commands/init/errors/InvalidNameError.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default class InvalidNameError extends Error {
import {CLIError} from '@react-native-community/cli-tools';

export default class InvalidNameError extends CLIError {
constructor(name: string) {
super(
`"${name}" is not a valid name for a project. Please use a valid identifier name (alphanumeric).`,
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/src/commands/init/errors/ReservedNameError.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default class ReservedNameError extends Error {
import {CLIError} from '@react-native-community/cli-tools';

export default class ReservedNameError extends CLIError {
constructor(name: string) {
super(
`Not a valid name for a project. Please do not use the reserved word "${name}".`,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default class TemplateAndVersionError extends Error {
import {CLIError} from '@react-native-community/cli-tools';

export default class TemplateAndVersionError extends CLIError {
constructor(template: string) {
super(
`Passing both "version" and "template" is not supported. The template you select determines the version of react-native used. Please use only one of these options, for example:
Expand Down
10 changes: 3 additions & 7 deletions packages/cli/src/commands/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,8 @@ export default (async function initialize(
const version = options.version || DEFAULT_VERSION;
const directoryName = path.relative(root, options.directory || projectName);

try {
await createProject(projectName, directoryName, version, options);
await createProject(projectName, directoryName, version, options);

const projectFolder = path.join(root, directoryName);
printRunInstructions(projectFolder, projectName);
} catch (e) {
logger.error(e.message);
}
const projectFolder = path.join(root, directoryName);
printRunInstructions(projectFolder, projectName);
});

0 comments on commit f0f302d

Please sign in to comment.