-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathprogram.ts
More file actions
243 lines (225 loc) · 7.08 KB
/
program.ts
File metadata and controls
243 lines (225 loc) · 7.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import assert from "node:assert/strict";
import path from "node:path";
import { EventEmitter } from "node:stream";
import {
Command,
chalk,
SpawnFailure,
oraPromise,
wrapAction,
prettyPath,
} from "@react-native-node-api/cli-utils";
import {
determineModuleContext,
findNodeApiModulePathsByDependency,
getAutolinkPath,
getLibraryName,
logModulePaths,
normalizeModulePath,
PlatformName,
PLATFORMS,
} from "../path-utils";
import { command as vendorHermes } from "./hermes";
import { pathSuffixOption } from "./options";
import { linkModules, pruneLinkedModules, ModuleLinker } from "./link-modules";
import { linkXcframework } from "./apple";
import { linkAndroidDir } from "./android";
// We're attaching a lot of listeners when spawning in parallel
EventEmitter.defaultMaxListeners = 100;
export const program = new Command("react-native-node-api").addCommand(
vendorHermes,
);
function getLinker(platform: PlatformName): ModuleLinker {
if (platform === "android") {
return linkAndroidDir;
} else if (platform === "apple") {
return linkXcframework;
} else {
throw new Error(`Unknown platform: ${platform as string}`);
}
}
function getPlatformDisplayName(platform: PlatformName) {
if (platform === "android") {
return "Android";
} else if (platform === "apple") {
return "Apple";
} else {
throw new Error(`Unknown platform: ${platform as string}`);
}
}
program
.command("link")
.argument("[path]", "Some path inside the app package", process.cwd())
.option(
"--force",
"Don't check timestamps of input files to skip unnecessary rebuilds",
false,
)
.option(
"--prune",
"Delete vendored modules that are no longer auto-linked",
true,
)
.option("--android", "Link Android modules")
.option("--apple", "Link Apple modules")
.addOption(pathSuffixOption)
.action(
wrapAction(
async (pathArg, { force, prune, pathSuffix, android, apple }) => {
console.log("Auto-linking Node-API modules from", chalk.dim(pathArg));
const platforms: PlatformName[] = [];
if (android) {
platforms.push("android");
}
if (apple) {
platforms.push("apple");
}
if (platforms.length === 0) {
console.error(
`No platform specified, pass one or more of:`,
...PLATFORMS.map((platform) => chalk.bold(`\n --${platform}`)),
);
process.exitCode = 1;
return;
}
for (const platform of platforms) {
const platformDisplayName = getPlatformDisplayName(platform);
const platformOutputPath = getAutolinkPath(platform);
const modules = await oraPromise(
() =>
linkModules({
platform,
fromPath: path.resolve(pathArg),
incremental: !force,
naming: { pathSuffix },
linker: getLinker(platform),
}),
{
text: `Linking ${platformDisplayName} Node-API modules into ${prettyPath(
platformOutputPath,
)}`,
successText: `Linked ${platformDisplayName} Node-API modules into ${prettyPath(
platformOutputPath,
)}`,
failText: (error) =>
`Failed to link ${platformDisplayName} Node-API modules into ${prettyPath(
platformOutputPath,
)}: ${error.message}`,
},
);
if (modules.length === 0) {
console.log("Found no Node-API modules 🤷");
}
const failures = modules.filter((result) => "failure" in result);
const linked = modules.filter((result) => "outputPath" in result);
for (const { originalPath, outputPath, skipped } of linked) {
const prettyOutputPath = outputPath
? "→ " + prettyPath(path.basename(outputPath))
: "";
if (skipped) {
console.log(
chalk.greenBright("-"),
"Skipped",
prettyPath(originalPath),
prettyOutputPath,
"(up to date)",
);
} else {
console.log(
chalk.greenBright("⚭"),
"Linked",
prettyPath(originalPath),
prettyOutputPath,
);
}
}
for (const { originalPath, failure } of failures) {
assert(failure instanceof SpawnFailure);
console.error(
"\n",
chalk.redBright("✖"),
"Failed to copy",
prettyPath(originalPath),
);
console.error(failure.message);
failure.flushOutput("both");
process.exitCode = 1;
}
if (prune) {
await pruneLinkedModules(platform, modules);
}
}
},
),
);
program
.command("list")
.description("Lists Node-API modules")
.argument("[from-path]", "Some path inside the app package", process.cwd())
.option("--json", "Output as JSON", false)
.addOption(pathSuffixOption)
.action(
wrapAction(async (fromArg, { json, pathSuffix }) => {
const rootPath = path.resolve(fromArg);
const dependencies = await findNodeApiModulePathsByDependency({
fromPath: rootPath,
platform: PLATFORMS,
includeSelf: true,
});
if (json) {
console.log(JSON.stringify(dependencies, null, 2));
} else {
const dependencyCount = Object.keys(dependencies).length;
const xframeworkCount = Object.values(dependencies).reduce(
(acc, { modulePaths }) => acc + modulePaths.length,
0,
);
console.log(
"Found",
chalk.greenBright(xframeworkCount),
"Node-API modules in",
chalk.greenBright(dependencyCount),
dependencyCount === 1 ? "package" : "packages",
"from",
prettyPath(rootPath),
);
for (const [dependencyName, dependency] of Object.entries(
dependencies,
)) {
console.log(
chalk.blueBright(dependencyName),
"→",
prettyPath(dependency.path),
);
logModulePaths(
dependency.modulePaths.map((p) => path.join(dependency.path, p)),
{ pathSuffix },
);
}
}
}),
);
program
.command("info <path>")
.description(
"Utility to print, module path, the hash of a single Android library",
)
.addOption(pathSuffixOption)
.action(
wrapAction((pathInput, { pathSuffix }) => {
const resolvedModulePath = path.resolve(pathInput);
const normalizedModulePath = normalizeModulePath(resolvedModulePath);
const { packageName, relativePath } =
determineModuleContext(resolvedModulePath);
const libraryName = getLibraryName(resolvedModulePath, {
pathSuffix,
});
console.log({
resolvedModulePath,
normalizedModulePath,
packageName,
relativePath,
libraryName,
});
}),
);