-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathlink-modules.ts
More file actions
156 lines (141 loc) · 3.82 KB
/
link-modules.ts
File metadata and controls
156 lines (141 loc) · 3.82 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
import path from "node:path";
import fs from "node:fs";
import { SpawnFailure } from "bufout";
import {
findNodeApiModulePathsByDependency,
getAutolinkPath,
getLibraryName,
logModulePaths,
NamingStrategy,
PlatformName,
prettyPath,
} from "../path-utils";
import chalk from "chalk";
export type ModuleLinker = (
options: LinkModuleOptions
) => Promise<LinkModuleResult>;
export type LinkModulesOptions = {
platform: PlatformName;
incremental: boolean;
naming: NamingStrategy;
fromPath: string;
linker: ModuleLinker;
};
export type LinkModuleOptions = Omit<
LinkModulesOptions,
"fromPath" | "linker"
> & {
modulePath: string;
};
export type ModuleDetails = {
originalPath: string;
outputPath: string;
libraryName: string;
};
export type LinkModuleResult = ModuleDetails & {
skipped: boolean;
};
export type ModuleOutputBase = {
originalPath: string;
skipped: boolean;
};
type ModuleOutput = ModuleOutputBase &
(
| { outputPath: string; failure?: never }
| { outputPath?: never; failure: SpawnFailure }
);
export async function linkModules({
fromPath,
incremental,
naming,
platform,
linker,
}: LinkModulesOptions): Promise<ModuleOutput[]> {
// Find all their xcframeworks
const dependenciesByName = findNodeApiModulePathsByDependency({
fromPath,
platform,
includeSelf: true,
});
// Find absolute paths to xcframeworks
const absoluteModulePaths = Object.values(dependenciesByName).flatMap(
(dependency) => dependency.modulePaths.map(
(modulePath) => path.join(dependency.path, modulePath)
)
);
if (hasDuplicateLibraryNames(absoluteModulePaths, naming)) {
logModulePaths(absoluteModulePaths, naming);
throw new Error("Found conflicting library names");
}
return Promise.all(
absoluteModulePaths.map(async (originalPath) => {
try {
return await linker({
modulePath: originalPath,
incremental,
naming,
platform,
});
} catch (error) {
if (error instanceof SpawnFailure) {
return {
originalPath,
skipped: false,
failure: error,
};
} else {
throw error;
}
}
})
);
}
export async function pruneLinkedModules(
platform: PlatformName,
linkedModules: ModuleOutput[]
) {
if (linkedModules.some(({ failure }) => failure)) {
// Don't prune if any of the modules failed to copy
return;
}
const platformOutputPath = getAutolinkPath(platform);
// Pruning only when all modules are copied successfully
const expectedPaths = new Set([...linkedModules.map((m) => m.outputPath)]);
await Promise.all(
fs.readdirSync(platformOutputPath).map(async (entry) => {
const candidatePath = path.resolve(platformOutputPath, entry);
if (!expectedPaths.has(candidatePath)) {
console.log(
"🧹Deleting",
prettyPath(candidatePath),
chalk.dim("(no longer linked)")
);
await fs.promises.rm(candidatePath, { recursive: true, force: true });
}
})
);
}
export function hasDuplicateLibraryNames(
modulePaths: string[],
naming: NamingStrategy
): boolean {
const libraryNames = modulePaths.map((modulePath) => {
return getLibraryName(modulePath, naming);
});
const uniqueNames = new Set(libraryNames);
return uniqueNames.size !== libraryNames.length;
}
export function getLinkedModuleOutputPath(
platform: PlatformName,
modulePath: string,
naming: NamingStrategy
): string {
const libraryName = getLibraryName(modulePath, naming);
if (platform === "android") {
return path.join(getAutolinkPath(platform), libraryName);
} else if (platform === "apple") {
return path.join(getAutolinkPath(platform), libraryName + ".xcframework");
} else {
throw new Error(`Unsupported platform: ${platform}`);
}
}