Skip to content

Commit beff0f9

Browse files
authored
Refactor CLIs into using a shared utility package (more) (#244)
* Export and use explicit wrapAction instead of monkey-patching commander * Flush output when SpawnFailures are cause * Update vendor-hermes to use UsageError
1 parent 9861bad commit beff0f9

File tree

6 files changed

+362
-357
lines changed

6 files changed

+362
-357
lines changed

packages/cli-utils/src/actions.ts

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,32 @@ import * as commander from "@commander-js/extra-typings";
44

55
import { UsageError } from "./errors.js";
66

7-
function wrapAction<Command extends commander.Command, Args extends unknown[]>(
8-
fn: (this: Command, ...args: Args) => void | Promise<void>,
9-
) {
10-
return async function (this: Command, ...args: Args) {
7+
export function wrapAction<
8+
Args extends unknown[],
9+
Opts extends commander.OptionValues,
10+
GlobalOpts extends commander.OptionValues,
11+
Command extends commander.Command<Args, Opts, GlobalOpts>,
12+
ActionArgs extends unknown[],
13+
>(fn: (this: Command, ...args: ActionArgs) => void | Promise<void>) {
14+
return async function (this: Command, ...args: ActionArgs) {
1115
try {
1216
await fn.call(this, ...args);
1317
} catch (error) {
1418
process.exitCode = 1;
1519
if (error instanceof SpawnFailure) {
1620
error.flushOutput("both");
21+
} else if (
22+
error instanceof Error &&
23+
error.cause instanceof SpawnFailure
24+
) {
25+
error.cause.flushOutput("both");
1726
}
27+
// Ensure some visual distance to the previous output
28+
console.error();
1829
if (error instanceof UsageError || error instanceof SpawnFailure) {
1930
console.error(chalk.red("ERROR"), error.message);
2031
if (error.cause instanceof Error) {
21-
console.error(chalk.red("CAUSE"), error.cause.message);
32+
console.error(chalk.blue("CAUSE"), error.cause.message);
2233
}
2334
if (error instanceof UsageError && error.fix) {
2435
console.error(
@@ -34,17 +45,3 @@ function wrapAction<Command extends commander.Command, Args extends unknown[]>(
3445
}
3546
};
3647
}
37-
38-
import { Command } from "@commander-js/extra-typings";
39-
40-
// Patch Command to wrap all actions with our error handler
41-
42-
// eslint-disable-next-line @typescript-eslint/unbound-method
43-
const originalAction = Command.prototype.action;
44-
45-
Command.prototype.action = function action<Command extends commander.Command>(
46-
this: Command,
47-
fn: Parameters<typeof originalAction>[0],
48-
) {
49-
return originalAction.call(this, wrapAction(fn));
50-
};

packages/cmake-rn/src/cli.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
spawn,
1111
oraPromise,
1212
assertFixable,
13+
wrapAction,
1314
} from "@react-native-node-api/cli-utils";
1415
import { isSupportedTriplet } from "react-native-node-api";
1516

@@ -131,7 +132,7 @@ for (const platform of platforms) {
131132
}
132133

133134
program = program.action(
134-
async ({ target: requestedTargets, ...baseOptions }) => {
135+
wrapAction(async ({ target: requestedTargets, ...baseOptions }) => {
135136
assertFixable(
136137
fs.existsSync(path.join(baseOptions.source, "CMakeLists.txt")),
137138
`No CMakeLists.txt found in source directory: ${chalk.dim(baseOptions.source)}`,
@@ -245,7 +246,7 @@ program = program.action(
245246
baseOptions,
246247
);
247248
}
248-
},
249+
}),
249250
);
250251

251252
function getTargetsSummary(

0 commit comments

Comments
 (0)