aqua/api/api-npm/imports.js
2023-12-06 10:50:48 +00:00

36 lines
777 B
JavaScript

// @ts-check
import Arborist from "@npmcli/arborist";
import { breadth } from "treeverse";
export async function gatherImportsFromNpm(path) {
const arb = new Arborist({ path });
return await gatherImportsFromArborist(arb);
}
export async function gatherImportsFromArborist(arborist) {
const tree = await arborist.loadActual();
let result = new Map();
breadth({
tree,
getChildren(node, _) {
let deps = [];
for (let edge of node.edgesOut.values()) {
if (edge.to === null) continue;
const dep = edge.to;
deps.push(dep);
result.set(node.realpath, [
...(result.get(node.realpath) || []),
dep.realpath,
]);
}
return deps;
},
});
return Object.fromEntries(result);
}