mirror of
https://github.com/fluencelabs/aqua.git
synced 2024-12-11 17:30:18 +00:00
36 lines
777 B
JavaScript
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);
|
|
}
|