mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-11-08 08:44:37 +08:00
63 lines
1.6 KiB
JavaScript
63 lines
1.6 KiB
JavaScript
import { createRequire } from "node:module";
|
|
import { dirname, join } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
const load = createRequire(import.meta.url);
|
|
const here = dirname(fileURLToPath(import.meta.url));
|
|
|
|
// Looks for a prebuilt addon in each packaging location, the way
|
|
// packages with optional native helpers do.
|
|
export function probe(name) {
|
|
for (const dir of ["prebuilds", "native"]) {
|
|
try {
|
|
const addon = load(join(here, dir, `${name}.node`));
|
|
if (typeof addon?.run === "function") return "loaded";
|
|
} catch {
|
|
// Try the next location.
|
|
}
|
|
}
|
|
return "absent";
|
|
}
|
|
|
|
export function resolveHelper(name, resolvePackage) {
|
|
try {
|
|
return (resolvePackage ?? load.resolve)(name);
|
|
} catch {
|
|
return "unresolved";
|
|
}
|
|
}
|
|
|
|
// Requires a file that is installed: an addon, or a module that is not part
|
|
// of the compiled program.
|
|
export function loadInstalled(kind) {
|
|
try {
|
|
return typeof load(join(here, kind === "addon" ? "native/installed.node" : "installed.js"));
|
|
} catch {
|
|
return "fallback";
|
|
}
|
|
}
|
|
|
|
let probes = 0;
|
|
function candidate(name) {
|
|
probes++;
|
|
return `./${name}-${probes}.node`;
|
|
}
|
|
|
|
// Resolves computed candidate paths: the arguments run before the lookup
|
|
// fails, as an assignment and as a statement of their own.
|
|
export function locate(name) {
|
|
let where = "none";
|
|
try {
|
|
where = load.resolve(candidate(name));
|
|
} catch {
|
|
where = `missing after ${probes}`;
|
|
}
|
|
try {
|
|
load.resolve(candidate(name));
|
|
where += ",found";
|
|
} catch {
|
|
where += `,missing after ${probes}`;
|
|
}
|
|
return where;
|
|
}
|