-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathprogram.ts
More file actions
255 lines (237 loc) · 7.7 KB
/
Copy pathprogram.ts
File metadata and controls
255 lines (237 loc) · 7.7 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
244
245
246
247
248
249
250
251
252
253
254
255
import assert from "node:assert/strict";
import path from "node:path";
import {
Command,
chalk,
SpawnFailure,
oraPromise,
wrapAction,
prettyPath,
} from "@react-native-node-api/cli-utils";
import {
determineModuleContext,
findNodeApiModulePathsByDependency,
getLibraryName,
visualizeLibraryMap,
normalizeModulePath,
PlatformName,
PLATFORMS,
getLibraryMap,
} from "../path-utils";
import { command as vendorHermes } from "./hermes";
import { command as prebuiltHermes } from "./hermes-prebuilt";
import { packageNameOption, pathSuffixOption } from "./options";
import { linkModules, pruneLinkedModules, ModuleLinker } from "./link-modules";
import { ensureXcodeBuildPhase, createAppleLinker } from "./apple";
import { linkAndroidDir } from "./android";
export const program = new Command("react-native-node-api")
.addCommand(vendorHermes)
.addCommand(prebuiltHermes);
async function createLinker(platform: PlatformName): Promise<ModuleLinker> {
if (platform === "android") {
return linkAndroidDir;
} else if (platform === "apple") {
return createAppleLinker();
} 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(
"--prune",
"Delete vendored modules that are no longer auto-linked",
true,
)
.option("--android", "Link Android modules")
.option("--apple", "Link Apple modules")
.addOption(packageNameOption)
.addOption(pathSuffixOption)
.action(
wrapAction(
async (pathArg, { prune, pathSuffix, android, apple, packageName }) => {
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 modules = await oraPromise(
async () =>
await linkModules({
platform,
fromPath: path.resolve(pathArg),
naming: { packageName, pathSuffix },
linker: await createLinker(platform),
}),
{
text: `Linking ${platformDisplayName} Node-API modules`,
successText: `Linked ${platformDisplayName} Node-API modules`,
failText: () =>
`Failed to link ${platformDisplayName} Node-API modules`,
},
);
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, signed } of linked) {
const prettyOutputPath = outputPath
? "→ " + prettyPath(outputPath)
: "";
const signedSuffix = signed ? "🔏" : "";
if (skipped) {
console.log(
chalk.greenBright("-"),
"Skipped",
prettyPath(originalPath),
prettyOutputPath,
signedSuffix,
"(up to date)",
);
} else {
console.log(
chalk.greenBright("⚭"),
"Linked",
prettyPath(originalPath),
prettyOutputPath,
signedSuffix,
);
}
}
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(packageNameOption)
.addOption(pathSuffixOption)
.action(
wrapAction(async (fromArg, { json, pathSuffix, packageName }) => {
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(
"\n" + chalk.blueBright(dependencyName),
"→",
prettyPath(dependency.path),
);
const libraryMap = getLibraryMap(
dependency.modulePaths.map((p) => path.join(dependency.path, p)),
{ packageName, pathSuffix },
);
console.log(visualizeLibraryMap(libraryMap));
}
}
}),
);
program
.command("info <path>")
.description(
"Utility to print, module path, the hash of a single Android library",
)
.addOption(packageNameOption)
.addOption(pathSuffixOption)
.action(
wrapAction((pathInput, { pathSuffix, packageName }) => {
const resolvedModulePath = path.resolve(pathInput);
const normalizedModulePath = normalizeModulePath(resolvedModulePath);
const context = determineModuleContext(resolvedModulePath);
const libraryName = getLibraryName(resolvedModulePath, {
packageName,
pathSuffix,
});
console.log({
resolvedModulePath,
normalizedModulePath,
packageName: context.packageName,
relativePath: context.relativePath,
libraryName,
});
}),
);
program
.command("patch-xcode-project")
.description("Patch the Xcode project to include the Node-API build phase")
.argument("[path]", "Some path inside the app package", process.cwd())
.action(
wrapAction(async (pathInput) => {
const resolvedPath = path.resolve(process.cwd(), pathInput);
console.log(
"Patching Xcode project in",
prettyPath(resolvedPath),
"to include a build phase to copy, rename and sign Node-API frameworks",
);
assert.equal(
process.platform,
"darwin",
"Patching Xcode project is only supported on macOS",
);
await ensureXcodeBuildPhase(resolvedPath);
}),
);