Skip to content

Commit 1cdc092

Browse files
author
Mariusz Pasinski
committed
chore: move findNodeAddonForBindings() to path-utils
1 parent 824c932 commit 1cdc092

File tree

4 files changed

+61
-53
lines changed

4 files changed

+61
-53
lines changed

packages/host/src/node/babel-plugin/plugin.test.ts

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import path from "node:path";
44

55
import { transformFileSync } from "@babel/core";
66

7-
import { plugin, findNodeAddonForBindings, type PluginOptions } from "./plugin.js";
7+
import { plugin } from "./plugin.js";
88
import { setupTempDirectory } from "../test-utils.js";
99

1010
describe("plugin", () => {
@@ -113,31 +113,3 @@ describe("plugin", () => {
113113
};
114114
});
115115
});
116-
117-
describe("findNodeAddonForBindings()", () => {
118-
it("should look for addons in common paths", (context) => {
119-
// Arrange
120-
const expectedPaths = {
121-
"addon_1": "addon_1.node",
122-
"addon_2": "build/Release/addon_2.node",
123-
"addon_3": "build/Debug/addon_3.node",
124-
"addon_4": "build/addon_4.node",
125-
"addon_5": "out/Release/addon_5.node",
126-
"addon_6": "out/Debug/addon_6.node",
127-
"addon_7": "Release/addon_7.node",
128-
"addon_8": "Debug/addon_8.node",
129-
};
130-
const tempDirectoryPath = setupTempDirectory(context,
131-
Object.fromEntries(
132-
Object.values(expectedPaths)
133-
.map((p) => [p, "// This is supposed to be a binary file"])
134-
)
135-
);
136-
// Act & Assert
137-
Object.entries(expectedPaths).forEach(([name, relPath]) => {
138-
const expectedPath = path.join(tempDirectoryPath, relPath);
139-
const actualPath = findNodeAddonForBindings(name, tempDirectoryPath);
140-
assert.equal(actualPath, expectedPath);
141-
});
142-
});
143-
});

packages/host/src/node/babel-plugin/plugin.ts

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import path from "node:path";
44
import type { PluginObj, NodePath } from "@babel/core";
55
import * as t from "@babel/types";
66

7-
import { determineModuleContext, isNodeApiModule } from "../path-utils";
7+
import {
8+
determineModuleContext,
9+
isNodeApiModule,
10+
findNodeAddonForBindings,
11+
} from "../path-utils";
812

913
export type PluginOptions = {
1014
stripPathSuffix?: boolean;
@@ -50,29 +54,6 @@ function tryResolveModulePath(id: string, from: string): string | undefined {
5054
}
5155
}
5256

53-
const nodeBindingsSubdirs = [
54-
"./",
55-
"./build/Release",
56-
"./build/Debug",
57-
"./build",
58-
"./out/Release",
59-
"./out/Debug",
60-
"./Release",
61-
"./Debug",
62-
];
63-
export function findNodeAddonForBindings(id: string, fromDir: string) {
64-
const idWithExt = id.endsWith(".node") ? id : `${id}.node`;
65-
// Support traversing the filesystem to find the Node-API module.
66-
// Currently, we check the most common directories like `bindings` does.
67-
for (const subdir of nodeBindingsSubdirs) {
68-
const resolvedPath = path.join(fromDir, subdir, idWithExt);
69-
if (isNodeApiModule(resolvedPath)) {
70-
return resolvedPath;
71-
}
72-
}
73-
return undefined;
74-
}
75-
7657
export function replaceWithRequireNodeAddon3(
7758
p: NodePath,
7859
resolvedPath: string,

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
determineNormalizedModuleContext,
10+
findNodeAddonForBindings,
1011
findNodeApiModulePaths,
1112
findPackageDependencyPaths,
1213
getLibraryName,
@@ -400,3 +401,31 @@ describe("determineModuleContext", () => {
400401
assert.equal(readCount, 1);
401402
});
402403
});
404+
405+
describe("findNodeAddonForBindings()", () => {
406+
it("should look for addons in common paths", (context) => {
407+
// Arrange
408+
const expectedPaths = {
409+
"addon_1": "addon_1.node",
410+
"addon_2": "build/Release/addon_2.node",
411+
"addon_3": "build/Debug/addon_3.node",
412+
"addon_4": "build/addon_4.node",
413+
"addon_5": "out/Release/addon_5.node",
414+
"addon_6": "out/Debug/addon_6.node",
415+
"addon_7": "Release/addon_7.node",
416+
"addon_8": "Debug/addon_8.node",
417+
};
418+
const tempDirectoryPath = setupTempDirectory(context,
419+
Object.fromEntries(
420+
Object.values(expectedPaths)
421+
.map((p) => [p, "// This is supposed to be a binary file"])
422+
)
423+
);
424+
// Act & Assert
425+
Object.entries(expectedPaths).forEach(([name, relPath]) => {
426+
const expectedPath = path.join(tempDirectoryPath, relPath);
427+
const actualPath = findNodeAddonForBindings(name, tempDirectoryPath);
428+
assert.equal(actualPath, expectedPath);
429+
});
430+
});
431+
});

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

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

406406
return latest;
407407
}
408+
409+
// NOTE: List of paths influenced by `node-bindings` itself
410+
// https://github.com/TooTallNate/node-bindings/blob/v1.3.0/bindings.js#L21
411+
const nodeBindingsSubdirs = [
412+
"./",
413+
"./build/Release",
414+
"./build/Debug",
415+
"./build",
416+
"./out/Release",
417+
"./out/Debug",
418+
"./Release",
419+
"./Debug",
420+
];
421+
422+
export function findNodeAddonForBindings(id: string, fromDir: string) {
423+
const idWithExt = id.endsWith(".node") ? id : `${id}.node`;
424+
// Support traversing the filesystem to find the Node-API module.
425+
// Currently, we check the most common directories like `bindings` does.
426+
for (const subdir of nodeBindingsSubdirs) {
427+
const resolvedPath = path.join(fromDir, subdir, idWithExt);
428+
if (isNodeApiModule(resolvedPath)) {
429+
return resolvedPath;
430+
}
431+
}
432+
return undefined;
433+
}

0 commit comments

Comments
 (0)