Skip to content

Commit 75ac1fa

Browse files
author
Mariusz Pasinski
committed
feat: cherry-pick findNodeAddonForBindings()
1 parent 8e53ab5 commit 75ac1fa

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import fswin from "fswin";
77
import {
88
determineModuleContext,
99
findNodeApiModulePaths,
10+
findNodeAddonForBindings,
1011
findPackageDependencyPaths,
1112
getLibraryName,
1213
isNodeApiModule,
@@ -372,3 +373,31 @@ describe("determineModuleContext", () => {
372373
assert.equal(readCount, 1);
373374
});
374375
});
376+
377+
describe("findNodeAddonForBindings()", () => {
378+
it("should look for addons in common paths", (context) => {
379+
// Arrange
380+
const expectedPaths = {
381+
"addon_1": "addon_1.node",
382+
"addon_2": "build/Release/addon_2.node",
383+
"addon_3": "build/Debug/addon_3.node",
384+
"addon_4": "build/addon_4.node",
385+
"addon_5": "out/Release/addon_5.node",
386+
"addon_6": "out/Debug/addon_6.node",
387+
"addon_7": "Release/addon_7.node",
388+
"addon_8": "Debug/addon_8.node",
389+
};
390+
const tempDirectoryPath = setupTempDirectory(context,
391+
Object.fromEntries(
392+
Object.values(expectedPaths)
393+
.map((p) => [p, "// This is supposed to be a binary file"])
394+
)
395+
);
396+
// Act & Assert
397+
Object.entries(expectedPaths).forEach(([name, relPath]) => {
398+
const expectedPath = path.join(tempDirectoryPath, relPath);
399+
const actualPath = findNodeAddonForBindings(name, tempDirectoryPath);
400+
assert.equal(actualPath, expectedPath);
401+
});
402+
});
403+
});

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,3 +385,29 @@ export function getLatestMtime(fromPath: string): number {
385385

386386
return latest;
387387
}
388+
389+
// NOTE: List of paths influenced by `node-bindings` itself
390+
// https://github.com/TooTallNate/node-bindings/blob/v1.3.0/bindings.js#L21
391+
const nodeBindingsSubdirs = [
392+
"./",
393+
"./build/Release",
394+
"./build/Debug",
395+
"./build",
396+
"./out/Release",
397+
"./out/Debug",
398+
"./Release",
399+
"./Debug",
400+
];
401+
402+
export function findNodeAddonForBindings(id: string, fromDir: string) {
403+
const idWithExt = id.endsWith(".node") ? id : `${id}.node`;
404+
// Support traversing the filesystem to find the Node-API module.
405+
// Currently, we check the most common directories like `bindings` does.
406+
for (const subdir of nodeBindingsSubdirs) {
407+
const resolvedPath = path.join(fromDir, subdir, idWithExt);
408+
if (isNodeApiModule(resolvedPath)) {
409+
return resolvedPath;
410+
}
411+
}
412+
return undefined;
413+
}

0 commit comments

Comments
 (0)