@rnx-kit/cli 0.9.57 → 0.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/clean.ts ADDED
@@ -0,0 +1,189 @@
1
+ import type { Config as CLIConfig } from "@react-native-community/cli-types";
2
+ import { spawn } from "child_process";
3
+ import { existsSync as fileExists } from "fs";
4
+ import fs from "fs/promises";
5
+ import ora from "ora";
6
+ import os from "os";
7
+ import path from "path";
8
+
9
+ type Args = {
10
+ include?: string;
11
+ projectRoot?: string;
12
+ verify?: boolean;
13
+ };
14
+
15
+ type Task = {
16
+ label: string;
17
+ action: () => Promise<void>;
18
+ };
19
+
20
+ type CLICommand = {
21
+ [key: string]: Task[];
22
+ };
23
+
24
+ export async function rnxClean(
25
+ _argv: string[],
26
+ _config: CLIConfig,
27
+ cliOptions: Args
28
+ ): Promise<void> {
29
+ const projectRoot = cliOptions.projectRoot ?? process.cwd();
30
+ if (!fileExists(projectRoot)) {
31
+ throw new Error(`Invalid path provided! ${projectRoot}`);
32
+ }
33
+
34
+ const npm = os.platform() === "win32" ? "npm.cmd" : "npm";
35
+ const yarn = os.platform() === "win32" ? "yarn.cmd" : "yarn";
36
+
37
+ const COMMANDS: CLICommand = {
38
+ android: [
39
+ {
40
+ label: "Clean Gradle cache",
41
+ action: () => {
42
+ const candidates =
43
+ os.platform() === "win32"
44
+ ? ["android/gradlew.bat", "gradlew.bat"]
45
+ : ["android/gradlew", "gradlew"];
46
+ const gradlew = findPath(projectRoot, candidates);
47
+ if (gradlew) {
48
+ const script = path.basename(gradlew);
49
+ return execute(
50
+ os.platform() === "win32" ? script : `./${script}`,
51
+ ["clean"],
52
+ path.dirname(gradlew)
53
+ );
54
+ } else {
55
+ return Promise.resolve();
56
+ }
57
+ },
58
+ },
59
+ ],
60
+ cocoapods: [
61
+ {
62
+ label: "Clean CocoaPods cache",
63
+ action: () => execute("pod", ["cache", "clean", "--all"], projectRoot),
64
+ },
65
+ ],
66
+ metro: [
67
+ {
68
+ label: "Clean Metro cache",
69
+ action: () => cleanDir(`${os.tmpdir()}/metro-*`),
70
+ },
71
+ {
72
+ label: "Clean Haste cache",
73
+ action: () => cleanDir(`${os.tmpdir()}/haste-map-*`),
74
+ },
75
+ {
76
+ label: "Clean React Native cache",
77
+ action: () => cleanDir(`${os.tmpdir()}/react-*`),
78
+ },
79
+ ],
80
+ npm: [
81
+ {
82
+ label: "Remove node_modules",
83
+ action: () => cleanDir(`${projectRoot}/node_modules`),
84
+ },
85
+ ...(cliOptions.verify
86
+ ? [
87
+ {
88
+ label: "Verify npm cache",
89
+ action: () => execute(npm, ["cache", "verify"], projectRoot),
90
+ },
91
+ ]
92
+ : []),
93
+ ],
94
+ watchman: [
95
+ {
96
+ label: "Stop Watchman",
97
+ action: () =>
98
+ execute(
99
+ os.platform() === "win32" ? "tskill" : "killall",
100
+ ["watchman"],
101
+ projectRoot
102
+ ),
103
+ },
104
+ {
105
+ label: "Delete Watchman cache",
106
+ action: () => execute("watchman", ["watch-del-all"], projectRoot),
107
+ },
108
+ ],
109
+ yarn: [
110
+ {
111
+ label: "Clean Yarn cache",
112
+ action: () => execute(yarn, ["cache", "clean"], projectRoot),
113
+ },
114
+ ],
115
+ };
116
+
117
+ const categories = cliOptions.include?.split(",") ?? [
118
+ "metro",
119
+ "npm",
120
+ "watchman",
121
+ "yarn",
122
+ ];
123
+
124
+ const spinner = ora();
125
+ for (const category of categories) {
126
+ const commands = COMMANDS[category];
127
+ if (!commands) {
128
+ spinner.warn(`Unknown category: ${category}`);
129
+ return;
130
+ }
131
+
132
+ for (const { action, label } of commands) {
133
+ spinner.start(label);
134
+ await action()
135
+ .then(() => {
136
+ spinner.succeed();
137
+ })
138
+ .catch((e) => {
139
+ spinner.fail(`${label} » ${e}`);
140
+ });
141
+ }
142
+ }
143
+ }
144
+
145
+ function cleanDir(path: string): Promise<void> {
146
+ if (!fileExists(path)) {
147
+ return Promise.resolve();
148
+ }
149
+
150
+ return fs.rmdir(path, { maxRetries: 3, recursive: true });
151
+ }
152
+
153
+ function findPath(startPath: string, files: string[]): string | undefined {
154
+ // TODO: Find project files via `@react-native-community/cli`
155
+ for (const file of files) {
156
+ const filename = path.resolve(startPath, file);
157
+ if (fileExists(filename)) {
158
+ return filename;
159
+ }
160
+ }
161
+
162
+ return undefined;
163
+ }
164
+
165
+ function execute(command: string, args: string[], cwd: string): Promise<void> {
166
+ return new Promise((resolve, reject) => {
167
+ const process = spawn(command, args, {
168
+ cwd,
169
+ stdio: ["inherit", null, null],
170
+ });
171
+
172
+ const stderr: Buffer[] = [];
173
+ process.stderr.on("data", (data) => {
174
+ stderr.push(data);
175
+ });
176
+
177
+ process.on("close", (code, signal) => {
178
+ if (code === 0) {
179
+ resolve();
180
+ } else if (stderr) {
181
+ reject(Buffer.concat(stderr).toString().trimEnd());
182
+ } else if (signal) {
183
+ reject(`Failed with signal ${signal}`);
184
+ } else {
185
+ reject(`Failed with exit code ${code}`);
186
+ }
187
+ });
188
+ });
189
+ }
package/src/index.ts CHANGED
@@ -4,3 +4,4 @@ export { rnxStart } from "./start";
4
4
  export { rnxTest, rnxTestCommand } from "./test";
5
5
  export { rnxWriteThirdPartyNotices } from "./write-third-party-notices";
6
6
  export { parseBoolean } from "./parsers";
7
+ export { rnxClean } from "./clean";
package/src/start.ts CHANGED
@@ -6,13 +6,15 @@ import {
6
6
  startServer,
7
7
  } from "@rnx-kit/metro-service";
8
8
  import chalk from "chalk";
9
- import type { Reporter, ReportableEvent } from "metro";
10
- import type Server from "metro/src/Server";
9
+ import type { ReportableEvent, Reporter } from "metro";
11
10
  import type { Middleware } from "metro-config";
11
+ import type Server from "metro/src/Server";
12
+ import os from "os";
12
13
  import path from "path";
14
+ import qrcode from "qrcode";
13
15
  import readline from "readline";
14
- import { getKitServerConfig } from "./serve/kit-config";
15
16
  import { customizeMetroConfig } from "./metro-config";
17
+ import { getKitServerConfig } from "./serve/kit-config";
16
18
  import type { TypeScriptValidationOptions } from "./types";
17
19
 
18
20
  export type CLIStartOptions = {
@@ -85,17 +87,16 @@ export async function rnxStart(
85
87
  reportEventDelegate(event);
86
88
  }
87
89
  if (interactive && event.type === "dep_graph_loading") {
88
- terminal.log("To reload the app press '" + chalk.cyanBright("r") + "'");
89
- terminal.log(
90
- "To open developer menu press '" + chalk.cyanBright("d") + "'"
91
- );
92
- terminal.log(
93
- "To quit, press '" +
94
- chalk.cyanBright("control") +
95
- "-" +
96
- chalk.cyanBright("c") +
97
- "'"
98
- );
90
+ const dim = chalk.dim;
91
+ const press = dim(" › Press ");
92
+ [
93
+ ["r", "reload the app"],
94
+ ["d", "open developer menu"],
95
+ ["a", "show bundler address QR code"],
96
+ ["ctrl-c", "quit"],
97
+ ].forEach(([key, description]) => {
98
+ terminal.log(press + key + dim(` to ${description}.`));
99
+ });
99
100
  }
100
101
  },
101
102
  };
@@ -199,14 +200,31 @@ export async function rnxStart(
199
200
  process.emit("SIGTSTP", "SIGTSTP");
200
201
  break;
201
202
  }
202
- } else if (name === "r") {
203
- terminal.log(chalk.green("Reloading app..."));
204
- messageSocket.broadcast("reload", undefined);
205
- } else if (name === "d") {
206
- terminal.log(chalk.green("Opening developer menu..."));
207
- messageSocket.broadcast("devMenu", undefined);
208
203
  } else {
209
- terminal.log(chalk.grey(_key));
204
+ switch (name) {
205
+ case "a": {
206
+ const protocol = cliOptions.https ? "https" : "http";
207
+ const host = cliOptions.host || os.hostname();
208
+ const port = metroConfig.server.port;
209
+ const url = `${protocol}://${host}:${port}/index.bundle`;
210
+ qrcode.toString(url, { type: "terminal" }, (_err, qr) => {
211
+ terminal.log("");
212
+ terminal.log(url + ":");
213
+ terminal.log(qr);
214
+ });
215
+ break;
216
+ }
217
+
218
+ case "d":
219
+ terminal.log(chalk.green("Opening developer menu..."));
220
+ messageSocket.broadcast("devMenu", undefined);
221
+ break;
222
+
223
+ case "r":
224
+ terminal.log(chalk.green("Reloading app..."));
225
+ messageSocket.broadcast("reload", undefined);
226
+ break;
227
+ }
210
228
  }
211
229
  });
212
230
  }