Skip to content

Commit 7288cd4

Browse files
committed
refactor(cli): add custom CliError and fix printing help
1 parent 36b3cf5 commit 7288cd4

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

@commitlint/cli/src/cli-error.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export class CliError extends Error {
2+
__proto__ = Error;
3+
4+
public type: string;
5+
6+
constructor(message: string, type: string) {
7+
super(message);
8+
9+
this.type = type;
10+
11+
Object.setPrototypeOf(this, CliError.prototype);
12+
}
13+
}

@commitlint/cli/src/cli.ts

+8-11
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
ParserPreset,
1616
QualifiedConfig
1717
} from '@commitlint/types';
18+
import {CliError} from './cli-error';
1819

1920
const pkg = require('../package');
2021

@@ -106,9 +107,9 @@ const cli = yargs
106107
.usage(`${pkg.name}@${pkg.version} - ${pkg.description}\n`)
107108
.usage(
108109
`[input] reads from stdin if --edit, --env, --from and --to are omitted`
109-
).argv;
110+
);
110111

111-
main(cli).catch(err => {
112+
main(cli.argv).catch(err => {
112113
setTimeout(() => {
113114
if (err.type === pkg.name) {
114115
process.exit(1);
@@ -137,12 +138,11 @@ async function main(options: CliFlags) {
137138
.filter(Boolean);
138139

139140
if (messages.length === 0 && !checkFromRepository(flags)) {
140-
// TODO: create custom error??
141-
const err = new Error(
142-
'[input] is required: supply via stdin, or --env or --edit or --from and --to'
141+
const err = new CliError(
142+
'[input] is required: supply via stdin, or --env or --edit or --from and --to',
143+
pkg.name
143144
);
144-
(err as any).type = pkg.name; // TODO
145-
console.log(`${cli.help}\n`);
145+
yargs.showHelp('log');
146146
console.log(err.message);
147147
throw err;
148148
}
@@ -241,10 +241,7 @@ async function main(options: CliFlags) {
241241
}
242242

243243
if (!report.valid) {
244-
// TODO: create custom error??
245-
const err = new Error(output);
246-
(err as any).type = pkg.name;
247-
throw err;
244+
throw new CliError(output, pkg.name);
248245
}
249246
}
250247

0 commit comments

Comments
 (0)