aqua/api/api-npm/imports.js

36 lines
777 B
JavaScript
Raw Normal View History

2023-12-06 10:50:48 +00:00
// @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);
}