-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathactions.ts
More file actions
47 lines (45 loc) · 1.48 KB
/
actions.ts
File metadata and controls
47 lines (45 loc) · 1.48 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 { SpawnFailure } from "bufout";
import chalk from "chalk";
import * as commander from "@commander-js/extra-typings";
import { UsageError } from "./errors.js";
export function wrapAction<
Args extends unknown[],
Opts extends commander.OptionValues,
GlobalOpts extends commander.OptionValues,
Command extends commander.Command<Args, Opts, GlobalOpts>,
ActionArgs extends unknown[],
>(fn: (this: Command, ...args: ActionArgs) => void | Promise<void>) {
return async function (this: Command, ...args: ActionArgs) {
try {
await fn.call(this, ...args);
} catch (error) {
process.exitCode = 1;
if (error instanceof SpawnFailure) {
error.flushOutput("both");
} else if (
error instanceof Error &&
error.cause instanceof SpawnFailure
) {
error.cause.flushOutput("both");
}
// Ensure some visual distance to the previous output
console.error();
if (error instanceof UsageError || error instanceof SpawnFailure) {
console.error(chalk.red("ERROR"), error.message);
if (error.cause instanceof Error) {
console.error(chalk.blue("CAUSE"), error.cause.message);
}
if (error instanceof UsageError && error.fix) {
console.error(
chalk.green("FIX"),
error.fix.command
? chalk.dim("Run: ") + error.fix.command
: error.fix.instructions,
);
}
} else {
throw error;
}
}
};
}