Skip to content

Commit a496b89

Browse files
committed
Use cli-util in all CLIs
1 parent c57d33a commit a496b89

File tree

26 files changed

+236
-243
lines changed

26 files changed

+236
-243
lines changed

packages/cli-utils/src/actions.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { SpawnFailure } from "bufout";
22
import chalk from "chalk";
3+
import * as commander from "@commander-js/extra-typings";
34

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

6-
export function wrapAction<Args extends unknown[]>(
7-
action: (...args: Args) => void | Promise<void>,
8-
): (...args: Args) => Promise<void> {
9-
return async (...args: Args) => {
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) {
1011
try {
11-
await action(...args);
12+
await fn.call(this, ...args);
1213
} catch (error) {
1314
process.exitCode = 1;
1415
if (error instanceof SpawnFailure) {
@@ -33,3 +34,17 @@ export function wrapAction<Args extends unknown[]>(
3334
}
3435
};
3536
}
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/package.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@
2222
"test": "tsx --test --test-reporter=@reporters/github --test-reporter-destination=stdout --test-reporter=spec --test-reporter-destination=stdout"
2323
},
2424
"dependencies": {
25-
"@commander-js/extra-typings": "^13.1.0",
26-
"bufout": "^0.3.2",
27-
"chalk": "^5.4.1",
28-
"commander": "^13.1.0",
29-
"ora": "^8.2.0",
25+
"@react-native-node-api/cli-utils": "0.1.0",
3026
"react-native-node-api": "0.5.0"
3127
},
3228
"peerDependencies": {

packages/cmake-rn/src/cli.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@ import path from "node:path";
33
import fs from "node:fs";
44
import { EventEmitter } from "node:events";
55

6-
import { Command, Option } from "@commander-js/extra-typings";
7-
import { spawn, SpawnFailure } from "bufout";
8-
import { oraPromise } from "ora";
9-
import chalk from "chalk";
6+
import {
7+
chalk,
8+
Command,
9+
Option,
10+
spawn,
11+
SpawnFailure,
12+
oraPromise,
13+
} from "@react-native-node-api/cli-utils";
14+
import { isSupportedTriplet } from "react-native-node-api";
1015

1116
import { getWeakNodeApiVariables } from "./weak-node-api.js";
1217
import {
@@ -16,7 +21,6 @@ import {
1621
platformHasTarget,
1722
} from "./platforms.js";
1823
import { BaseOpts, TargetContext, Platform } from "./platforms/types.js";
19-
import { isSupportedTriplet } from "react-native-node-api";
2024

2125
// We're attaching a lot of listeners when spawning in parallel
2226
EventEmitter.defaultMaxListeners = 100;

packages/cmake-rn/src/platforms/android.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@ import assert from "node:assert/strict";
22
import fs from "node:fs";
33
import path from "node:path";
44

5-
import { Option } from "@commander-js/extra-typings";
5+
import { Option, oraPromise, chalk } from "@react-native-node-api/cli-utils";
66
import {
77
createAndroidLibsDirectory,
88
determineAndroidLibsFilename,
99
AndroidTriplet as Target,
1010
} from "react-native-node-api";
1111

1212
import type { Platform } from "./types.js";
13-
import { oraPromise } from "ora";
14-
import chalk from "chalk";
1513

1614
// This should match https://github.com/react-native-community/template/blob/main/template/android/build.gradle#L7
1715
const DEFAULT_NDK_VERSION = "27.1.12297006";

packages/cmake-rn/src/platforms/apple.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import assert from "node:assert/strict";
22
import path from "node:path";
33
import fs from "node:fs";
44

5-
import { Option } from "@commander-js/extra-typings";
6-
import { oraPromise } from "ora";
5+
import { Option, oraPromise, chalk } from "@react-native-node-api/cli-utils";
76
import {
87
AppleTriplet as Target,
98
createAppleFramework,
@@ -12,7 +11,6 @@ import {
1211
} from "react-native-node-api";
1312

1413
import type { Platform } from "./types.js";
15-
import chalk from "chalk";
1614

1715
type XcodeSDKName =
1816
| "iphoneos"

packages/cmake-rn/src/platforms/types.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import * as commander from "@commander-js/extra-typings";
1+
import * as cli from "@react-native-node-api/cli-utils";
2+
23
import type { program } from "../cli.js";
34

4-
type InferOptionValues<Command extends commander.Command> = ReturnType<
5+
type InferOptionValues<Command extends cli.Command> = ReturnType<
56
Command["opts"]
67
>;
78

89
type BaseCommand = typeof program;
9-
type ExtendedCommand<Opts extends commander.OptionValues> = commander.Command<
10+
type ExtendedCommand<Opts extends cli.OptionValues> = cli.Command<
1011
[],
1112
Opts & InferOptionValues<BaseCommand>,
1213
Record<string, unknown> // Global opts are not supported
@@ -22,7 +23,7 @@ export type TargetContext<Target extends string> = {
2223

2324
export type Platform<
2425
Targets extends string[] = string[],
25-
Opts extends commander.OptionValues = Record<string, unknown>,
26+
Opts extends cli.OptionValues = Record<string, unknown>,
2627
Command = ExtendedCommand<Opts>,
2728
> = {
2829
/**

packages/ferric/package.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@
1717
},
1818
"dependencies": {
1919
"@napi-rs/cli": "~3.0.3",
20-
"@commander-js/extra-typings": "^13.1.0",
21-
"bufout": "^0.3.2",
22-
"chalk": "^5.4.1",
23-
"commander": "^13.1.0",
24-
"react-native-node-api": "0.5.0",
25-
"ora": "^8.2.0"
20+
"@react-native-node-api/cli-utils": "0.1.0",
21+
"react-native-node-api": "0.5.0"
2622
}
2723
}

packages/ferric/src/banner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import chalk from "chalk";
1+
import { chalk } from "@react-native-node-api/cli-utils";
22

33
const LINES = [
44
// Pagga on https://www.asciiart.eu/text-to-ascii-art

0 commit comments

Comments
 (0)