Skip to content

Commit 500e1d8

Browse files
feat: exit gracefully on errors
1 parent 78fb99d commit 500e1d8

1 file changed

Lines changed: 18 additions & 8 deletions

File tree

src/index.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { API } from './api.js';
44
import { ArgumentError } from './errors/argument.error.js';
55
import { ReferenceError } from './errors/reference.error.js';
66

7-
export function cli(args: string[]) {
8-
const program = new Command()
7+
function buildProgram(): Command {
8+
return new Command()
99
.name('faker')
1010
.version(packageJson.version)
1111
.description(packageJson.description)
@@ -31,13 +31,23 @@ export function cli(args: string[]) {
3131
}
3232

3333
process.stdout.write(String(entryRef()));
34-
process.exit(0);
3534
});
35+
}
36+
37+
export function cli(args: string[]) {
38+
try {
39+
const program = buildProgram();
40+
41+
const hasArgs = args.length > 2;
42+
if (!hasArgs) {
43+
program.help();
44+
} else {
45+
program.parse(args);
46+
}
3647

37-
const hasArgs = args.length > 2;
38-
if (!hasArgs) {
39-
program.help();
40-
} else {
41-
program.parse(args);
48+
process.exit(0);
49+
} catch (error) {
50+
process.stderr.write(String(error));
51+
process.exit(1);
4252
}
4353
}

0 commit comments

Comments
 (0)