Skip to content

Commit bb9a78c

Browse files
authored
Fix visualizing duplicate library names (#282)
* Fix visualizing duplicate links * Remove outdated comment * Add changeset
1 parent d6fb728 commit bb9a78c

File tree

5 files changed

+39
-50
lines changed

5 files changed

+39
-50
lines changed

.changeset/polite-bikes-stay.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-native-node-api": patch
3+
---
4+
5+
Fixed visualizing duplicate library names

packages/host/src/node/cli/link-modules.ts

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ import {
1111
findNodeApiModulePathsByDependency,
1212
getAutolinkPath,
1313
getLibraryName,
14-
logModulePaths,
14+
visualizeLibraryMap,
1515
NamingStrategy,
1616
PlatformName,
17+
getLibraryMap,
1718
} from "../path-utils";
1819

1920
export type ModuleLinker = (
@@ -78,9 +79,14 @@ export async function linkModules({
7879
),
7980
);
8081

81-
if (hasDuplicateLibraryNames(absoluteModulePaths, naming)) {
82-
logModulePaths(absoluteModulePaths, naming);
83-
throw new Error("Found conflicting library names");
82+
const libraryMap = getLibraryMap(absoluteModulePaths, naming);
83+
const duplicates = new Map(
84+
Array.from(libraryMap.entries()).filter(([, paths]) => paths.length > 1),
85+
);
86+
87+
if (duplicates.size > 0) {
88+
const visualized = visualizeLibraryMap(duplicates);
89+
throw new Error("Found conflicting library names:\n" + visualized);
8490
}
8591

8692
return Promise.all(
@@ -133,17 +139,6 @@ export async function pruneLinkedModules(
133139
);
134140
}
135141

136-
export function hasDuplicateLibraryNames(
137-
modulePaths: string[],
138-
naming: NamingStrategy,
139-
): boolean {
140-
const libraryNames = modulePaths.map((modulePath) => {
141-
return getLibraryName(modulePath, naming);
142-
});
143-
const uniqueNames = new Set(libraryNames);
144-
return uniqueNames.size !== libraryNames.length;
145-
}
146-
147142
export function getLinkedModuleOutputPath(
148143
platform: PlatformName,
149144
modulePath: string,

packages/host/src/node/cli/program.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ import {
1616
findNodeApiModulePathsByDependency,
1717
getAutolinkPath,
1818
getLibraryName,
19-
logModulePaths,
19+
visualizeLibraryMap,
2020
normalizeModulePath,
2121
PlatformName,
2222
PLATFORMS,
23+
getLibraryMap,
2324
} from "../path-utils";
2425

2526
import { command as vendorHermes } from "./hermes";
@@ -115,10 +116,10 @@ program
115116
successText: `Linked ${platformDisplayName} Node-API modules into ${prettyPath(
116117
platformOutputPath,
117118
)}`,
118-
failText: (error) =>
119+
failText: () =>
119120
`Failed to link ${platformDisplayName} Node-API modules into ${prettyPath(
120121
platformOutputPath,
121-
)}: ${error.message}`,
122+
)}`,
122123
},
123124
);
124125

@@ -209,14 +210,15 @@ program
209210
dependencies,
210211
)) {
211212
console.log(
212-
chalk.blueBright(dependencyName),
213+
"\n" + chalk.blueBright(dependencyName),
213214
"→",
214215
prettyPath(dependency.path),
215216
);
216-
logModulePaths(
217+
const libraryMap = getLibraryMap(
217218
dependency.modulePaths.map((p) => path.join(dependency.path, p)),
218219
{ packageName, pathSuffix },
219220
);
221+
console.log(visualizeLibraryMap(libraryMap));
220222
}
221223
}
222224
}),

packages/host/src/node/duplicates.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

packages/host/src/node/path-utils.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import { createRequire } from "node:module";
77

88
import { chalk, prettyPath } from "@react-native-node-api/cli-utils";
99

10-
import { findDuplicates } from "./duplicates";
11-
1210
// TODO: Change to .apple.node
1311
export const PLATFORMS = ["android", "apple"] as const;
1412
export type PlatformName = "android" | "apple";
@@ -267,32 +265,33 @@ export function resolvePackageRoot(
267265
}
268266
}
269267

270-
export function logModulePaths(
271-
modulePaths: string[],
272-
// TODO: Default to iterating and printing for all supported naming strategies
273-
naming: NamingStrategy,
274-
) {
275-
const pathsPerName = new Map<string, string[]>();
268+
/**
269+
* Module paths per library name.
270+
*/
271+
export type LibraryMap = Map<string, string[]>;
272+
273+
export function getLibraryMap(modulePaths: string[], naming: NamingStrategy) {
274+
const result = new Map<string, string[]>();
276275
for (const modulePath of modulePaths) {
277276
const libraryName = getLibraryName(modulePath, naming);
278-
const existingPaths = pathsPerName.get(libraryName) ?? [];
277+
const existingPaths = result.get(libraryName) ?? [];
279278
existingPaths.push(modulePath);
280-
pathsPerName.set(libraryName, existingPaths);
279+
result.set(libraryName, existingPaths);
281280
}
281+
return result;
282+
}
282283

283-
const allModulePaths = modulePaths.map((modulePath) => modulePath);
284-
const duplicatePaths = findDuplicates(allModulePaths);
285-
for (const [libraryName, modulePaths] of pathsPerName) {
286-
console.log(
284+
export function visualizeLibraryMap(libraryMap: LibraryMap) {
285+
const result = [];
286+
for (const [libraryName, modulePaths] of libraryMap) {
287+
result.push(
287288
chalk.greenBright(`${libraryName}`),
288289
...modulePaths.flatMap((modulePath) => {
289-
const line = duplicatePaths.has(modulePath)
290-
? chalk.redBright(prettyPath(modulePath))
291-
: prettyPath(modulePath);
292-
return `\n ↳ ${line}`;
290+
return ` ↳ ${prettyPath(modulePath)}`;
293291
}),
294292
);
295293
}
294+
return result.join("\n");
296295
}
297296

298297
/**

0 commit comments

Comments
 (0)