-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathcli.ts
More file actions
47 lines (42 loc) · 1.14 KB
/
cli.ts
File metadata and controls
47 lines (42 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { logger, RockError, type RockCLIOptions } from '@rock-js/tools';
import type { Command } from 'commander';
export function curryOptions(programCommand: Command, options: RockCLIOptions) {
options.forEach((option) => {
if (option.parse) {
programCommand = programCommand.option(
option.name,
option.description,
option.parse,
option.default
);
} else {
programCommand = programCommand.option(
option.name,
option.description,
option.default
);
}
});
return programCommand;
}
export function actionRunner<T, R>(fn: (...args: T[]) => Promise<R>) {
return async function wrappedCLIAction(...args: T[]) {
try {
await fn(...args);
} catch (error) {
if (error instanceof RockError) {
if (logger.isVerbose()) {
logger.error(error);
} else {
logger.error(error.message);
if (error.cause) {
logger.error(`Cause: ${error.cause}`);
}
}
} else {
logger.error(`Unexpected error while running command:`, error);
}
process.exit(1);
}
};
}