Skip to content

Commit 8f969b2

Browse files
committed
fix: minimize usage of process.exit(1)
1 parent 45a4db3 commit 8f969b2

File tree

6 files changed

+24
-55
lines changed

6 files changed

+24
-55
lines changed

packages/create-react-native-library/src/index.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,11 @@ async function promptPath(argv: Args, local: boolean) {
210210
}
211211

212212
if (await fs.pathExists(folder)) {
213-
console.log(
213+
throw new Error(
214214
`A folder already exists at ${kleur.blue(
215215
folder
216216
)}! Please specify another folder name or delete the existing one.`
217217
);
218-
219-
process.exit(1);
220218
}
221219

222220
return folder;

packages/create-react-native-library/src/utils/assert.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ export async function assertNpxExists() {
99
} catch (error) {
1010
// @ts-expect-error: TS doesn't know about `code`
1111
if (error != null && error.code === 'ENOENT') {
12-
console.log(
12+
throw new Error(
1313
`Couldn't find ${kleur.blue(
1414
'npx'
1515
)}! Please install it by running ${kleur.blue('npm install -g npx')}`
1616
);
17-
18-
process.exit(1);
1917
} else {
2018
throw error;
2119
}
@@ -76,9 +74,7 @@ export function assertUserInput(
7674
message += `: ${validation}`;
7775
}
7876

79-
console.log(message);
80-
81-
process.exit(1);
77+
throw new Error(message);
8278
}
8379
}
8480
}

packages/react-native-builder-bob/src/build.ts

+8-13
Original file line numberDiff line numberDiff line change
@@ -34,46 +34,42 @@ export async function build(argv: Argv) {
3434
const result = loadConfig(root);
3535

3636
if (!result?.config) {
37-
logger.error(
37+
throw new Error(
3838
`No configuration found. Run '${argv.$0} init' to create one automatically.`
3939
);
40-
process.exit(1);
4140
}
4241

4342
const options: Options = result!.config;
4443

4544
if (!options.targets?.length) {
46-
logger.error(
47-
`No targets found in the configuration in '${path.relative(
45+
throw new Error(
46+
`No 'targets' found in the configuration in '${path.relative(
4847
root,
4948
result!.filepath
5049
)}'.`
5150
);
52-
process.exit(1);
5351
}
5452

5553
const source = options.source;
5654

5755
if (!source) {
58-
logger.error(
59-
`No source option found in the configuration in '${path.relative(
56+
throw new Error(
57+
`No 'source' option found in the configuration in '${path.relative(
6058
root,
6159
result!.filepath
6260
)}'.`
6361
);
64-
process.exit(1);
6562
}
6663

6764
const output = options.output;
6865

6966
if (!output) {
70-
logger.error(
71-
`No source option found in the configuration in '${path.relative(
67+
throw new Error(
68+
`No 'output' option found in the configuration in '${path.relative(
7269
root,
7370
result!.filepath
7471
)}'.`
7572
);
76-
process.exit(1);
7773
}
7874

7975
const exclude = options.exclude ?? '**/{__tests__,__fixtures__,__mocks__}/**';
@@ -197,7 +193,6 @@ async function buildTarget({
197193
});
198194
break;
199195
default:
200-
logger.error(`Invalid target ${kleur.blue(targetName)}.`);
201-
process.exit(1);
196+
throw new Error(`Invalid target ${kleur.blue(targetName)}.`);
202197
}
203198
}

packages/react-native-builder-bob/src/init.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import kleur from 'kleur';
44
import dedent from 'dedent';
55
import isGitDirty from 'is-git-dirty';
66
import prompts, { type PromptObject } from './utils/prompts';
7-
import * as logger from './utils/logger';
87
import { loadConfig } from './utils/loadConfig';
98

109
// eslint-disable-next-line @typescript-eslint/no-require-imports,import-x/no-commonjs
@@ -30,10 +29,9 @@ export async function init() {
3029
}
3130

3231
if (!(await fs.pathExists(projectPackagePath))) {
33-
logger.error(
32+
throw new Error(
3433
`Couldn't find a 'package.json' file in '${root}'.\n Are you in a project folder?`
3534
);
36-
process.exit(1);
3735
}
3836

3937
const pkg = JSON.parse(await fs.readFile(projectPackagePath, 'utf-8'));
@@ -71,10 +69,9 @@ export async function init() {
7169
}
7270

7371
if (!entryFile) {
74-
logger.error(
72+
throw new Error(
7573
`Couldn't find a 'index.js'. 'index.ts' or 'index.tsx' file under '${source}'.\n Please re-run the CLI after creating it.`
7674
);
77-
process.exit(1);
7875
}
7976

8077
pkg.devDependencies = Object.fromEntries(

packages/react-native-builder-bob/src/targets/codegen/index.ts

+4-11
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,9 @@ export default async function build({ root, report }: Options) {
3636
const codegenType = packageJson.codegenConfig?.type;
3737

3838
if (codegenType === undefined) {
39-
report.error(
39+
throw new Error(
4040
"Couldn't find the 'type' value in 'codegenConfig'. Please check your package.json's 'codegenConfig' property and make sure 'type' is defined. https://reactnative.dev/docs/the-new-architecture/using-codegen#configuring-codegen"
4141
);
42-
process.exit(1);
4342
}
4443

4544
try {
@@ -54,7 +53,7 @@ export default async function build({ root, report }: Options) {
5453
} catch (e: unknown) {
5554
if (e != null && typeof e === 'object') {
5655
if ('stdout' in e && e.stdout != null) {
57-
report.error(
56+
throw new Error(
5857
`Errors found while generating codegen files:\n${e.stdout.toString()}`
5958
);
6059
} else if ('message' in e && typeof e.message === 'string') {
@@ -63,19 +62,13 @@ export default async function build({ root, report }: Options) {
6362
"Error: Cannot find module '@react-native-community/cli/package.json'"
6463
)
6564
) {
66-
report.error(
65+
throw new Error(
6766
"You don't have `@react-native-community/cli` in your root package's dev dependencies. Please install it and make sure it uses the same version as your application."
6867
);
69-
} else {
70-
report.error(e.message);
7168
}
72-
} else {
73-
throw e;
7469
}
75-
} else {
76-
throw e;
7770
}
7871

79-
process.exit(1);
72+
throw e;
8073
}
8174
}

packages/react-native-builder-bob/src/targets/custom.ts

+7-17
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import path from 'path';
33
import fs from 'fs-extra';
44
import type { Input } from '../types';
55
import { spawn } from '../utils/spawn';
6-
import dedent from 'dedent';
76
import del from 'del';
87

98
type Options = Omit<Input, 'output'> & {
@@ -15,13 +14,11 @@ type Options = Omit<Input, 'output'> & {
1514

1615
export default async function customTarget({ options, root, report }: Options) {
1716
if (options?.script == null) {
18-
report.error(
19-
dedent(
20-
`No script was provided with the custom target.
21-
Example: ${kleur.green('{["custom", { "script": "generateTypes" }}')}`
22-
)
17+
throw new Error(
18+
`No 'script' was provided with the custom target. Example: ${kleur.green(
19+
'{["custom", { "script": "generateTypes" }}'
20+
)}`
2321
);
24-
process.exit(1);
2522
}
2623

2724
const pathToClean = options.clean
@@ -45,16 +42,9 @@ export default async function customTarget({ options, root, report }: Options) {
4542
)}`
4643
);
4744

48-
try {
49-
await spawn(packageManagerExecutable, packageManagerArgs, {
50-
stdio: ['ignore', 'ignore', 'inherit'],
51-
});
52-
} catch (e) {
53-
report.error(
54-
`An error occurred when running ${kleur.blue(options.script)}`
55-
);
56-
process.exit(1);
57-
}
45+
await spawn(packageManagerExecutable, packageManagerArgs, {
46+
stdio: ['ignore', 'ignore', 'inherit'],
47+
});
5848

5949
report.success(`Ran the ${kleur.blue(options.script)} script succesfully`);
6050

0 commit comments

Comments
 (0)