@rnx-kit/cli 0.10.0 → 0.11.2

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,198 @@
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 spinner = ora();
35
+ try {
36
+ require.resolve("@react-native-community/cli-clean");
37
+ spinner.warn(
38
+ "`rnx-clean` has been upstreamed to `@react-native-community/cli`. Please use `npx react-native clean` instead."
39
+ );
40
+ } catch (_) {
41
+ // Ignore
42
+ }
43
+
44
+ const npm = os.platform() === "win32" ? "npm.cmd" : "npm";
45
+ const yarn = os.platform() === "win32" ? "yarn.cmd" : "yarn";
46
+
47
+ const COMMANDS: CLICommand = {
48
+ android: [
49
+ {
50
+ label: "Clean Gradle cache",
51
+ action: () => {
52
+ const candidates =
53
+ os.platform() === "win32"
54
+ ? ["android/gradlew.bat", "gradlew.bat"]
55
+ : ["android/gradlew", "gradlew"];
56
+ const gradlew = findPath(projectRoot, candidates);
57
+ if (gradlew) {
58
+ const script = path.basename(gradlew);
59
+ return execute(
60
+ os.platform() === "win32" ? script : `./${script}`,
61
+ ["clean"],
62
+ path.dirname(gradlew)
63
+ );
64
+ } else {
65
+ return Promise.resolve();
66
+ }
67
+ },
68
+ },
69
+ ],
70
+ cocoapods: [
71
+ {
72
+ label: "Clean CocoaPods cache",
73
+ action: () => execute("pod", ["cache", "clean", "--all"], projectRoot),
74
+ },
75
+ ],
76
+ metro: [
77
+ {
78
+ label: "Clean Metro cache",
79
+ action: () => cleanDir(`${os.tmpdir()}/metro-*`),
80
+ },
81
+ {
82
+ label: "Clean Haste cache",
83
+ action: () => cleanDir(`${os.tmpdir()}/haste-map-*`),
84
+ },
85
+ {
86
+ label: "Clean React Native cache",
87
+ action: () => cleanDir(`${os.tmpdir()}/react-*`),
88
+ },
89
+ ],
90
+ npm: [
91
+ {
92
+ label: "Remove node_modules",
93
+ action: () => cleanDir(`${projectRoot}/node_modules`),
94
+ },
95
+ ...(cliOptions.verify
96
+ ? [
97
+ {
98
+ label: "Verify npm cache",
99
+ action: () => execute(npm, ["cache", "verify"], projectRoot),
100
+ },
101
+ ]
102
+ : []),
103
+ ],
104
+ watchman: [
105
+ {
106
+ label: "Stop Watchman",
107
+ action: () =>
108
+ execute(
109
+ os.platform() === "win32" ? "tskill" : "killall",
110
+ ["watchman"],
111
+ projectRoot
112
+ ),
113
+ },
114
+ {
115
+ label: "Delete Watchman cache",
116
+ action: () => execute("watchman", ["watch-del-all"], projectRoot),
117
+ },
118
+ ],
119
+ yarn: [
120
+ {
121
+ label: "Clean Yarn cache",
122
+ action: () => execute(yarn, ["cache", "clean"], projectRoot),
123
+ },
124
+ ],
125
+ };
126
+
127
+ const categories = cliOptions.include?.split(",") ?? [
128
+ "metro",
129
+ "npm",
130
+ "watchman",
131
+ "yarn",
132
+ ];
133
+
134
+ for (const category of categories) {
135
+ const commands = COMMANDS[category];
136
+ if (!commands) {
137
+ spinner.warn(`Unknown category: ${category}`);
138
+ return;
139
+ }
140
+
141
+ for (const { action, label } of commands) {
142
+ spinner.start(label);
143
+ await action()
144
+ .then(() => {
145
+ spinner.succeed();
146
+ })
147
+ .catch((e) => {
148
+ spinner.fail(`${label} » ${e}`);
149
+ });
150
+ }
151
+ }
152
+ }
153
+
154
+ function cleanDir(path: string): Promise<void> {
155
+ if (!fileExists(path)) {
156
+ return Promise.resolve();
157
+ }
158
+
159
+ return fs.rmdir(path, { maxRetries: 3, recursive: true });
160
+ }
161
+
162
+ function findPath(startPath: string, files: string[]): string | undefined {
163
+ // TODO: Find project files via `@react-native-community/cli`
164
+ for (const file of files) {
165
+ const filename = path.resolve(startPath, file);
166
+ if (fileExists(filename)) {
167
+ return filename;
168
+ }
169
+ }
170
+
171
+ return undefined;
172
+ }
173
+
174
+ function execute(command: string, args: string[], cwd: string): Promise<void> {
175
+ return new Promise((resolve, reject) => {
176
+ const process = spawn(command, args, {
177
+ cwd,
178
+ stdio: ["inherit", null, null],
179
+ });
180
+
181
+ const stderr: Buffer[] = [];
182
+ process.stderr.on("data", (data) => {
183
+ stderr.push(data);
184
+ });
185
+
186
+ process.on("close", (code, signal) => {
187
+ if (code === 0) {
188
+ resolve();
189
+ } else if (stderr) {
190
+ reject(Buffer.concat(stderr).toString().trimEnd());
191
+ } else if (signal) {
192
+ reject(`Failed with signal ${signal}`);
193
+ } else {
194
+ reject(`Failed with exit code ${code}`);
195
+ }
196
+ });
197
+ });
198
+ }
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";