Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 16 additions & 19 deletions packages/cli-utils/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,32 @@ import * as commander from "@commander-js/extra-typings";

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

function wrapAction<Command extends commander.Command, Args extends unknown[]>(
fn: (this: Command, ...args: Args) => void | Promise<void>,
) {
return async function (this: Command, ...args: Args) {
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.red("CAUSE"), error.cause.message);
console.error(chalk.blue("CAUSE"), error.cause.message);
}
if (error instanceof UsageError && error.fix) {
console.error(
Expand All @@ -34,17 +45,3 @@ function wrapAction<Command extends commander.Command, Args extends unknown[]>(
}
};
}

import { Command } from "@commander-js/extra-typings";

// Patch Command to wrap all actions with our error handler

// eslint-disable-next-line @typescript-eslint/unbound-method
const originalAction = Command.prototype.action;

Command.prototype.action = function action<Command extends commander.Command>(
this: Command,
fn: Parameters<typeof originalAction>[0],
) {
return originalAction.call(this, wrapAction(fn));
};
5 changes: 3 additions & 2 deletions packages/cmake-rn/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
spawn,
oraPromise,
assertFixable,
wrapAction,
} from "@react-native-node-api/cli-utils";
import { isSupportedTriplet } from "react-native-node-api";

Expand Down Expand Up @@ -131,7 +132,7 @@ for (const platform of platforms) {
}

program = program.action(
async ({ target: requestedTargets, ...baseOptions }) => {
wrapAction(async ({ target: requestedTargets, ...baseOptions }) => {
assertFixable(
fs.existsSync(path.join(baseOptions.source, "CMakeLists.txt")),
`No CMakeLists.txt found in source directory: ${chalk.dim(baseOptions.source)}`,
Expand Down Expand Up @@ -245,7 +246,7 @@ program = program.action(
baseOptions,
);
}
},
}),
);

function getTargetsSummary(
Expand Down
Loading
Loading