Add comments

This commit is contained in:
InversionSpaces 2023-12-06 10:56:59 +00:00
parent 1c490fa73a
commit 5044dd030d
2 changed files with 21 additions and 0 deletions

View File

@ -2,8 +2,19 @@ import Arborist from "@npmcli/arborist";
import type { Imports } from "./index.d.ts";
/**
* Gather imports for aqua compiler from
* actual node_modules folder created by npm.
* Underneath it uses @npmcli/arborist.
* @param path path to project with node_modules folder
*/
export declare async function gatherImportsFromNpm(path: string): Imports;
/**
* Same as `gatherImportsFromNpm` but uses
* already created arborist instance.
* @param arborist arborist instance
*/
export declare async function gatherImportsFromArborist(
arborist: Arborist,
): Imports;

View File

@ -12,24 +12,34 @@ export async function gatherImportsFromNpm(path) {
export async function gatherImportsFromArborist(arborist) {
const tree = await arborist.loadActual();
/**
* Traverse dependency tree to construct map
* (real path of a package) -> (real paths of its immediate dependencies)
*/
let result = new Map();
breadth({
tree,
getChildren(node, _) {
let deps = [];
for (let edge of node.edgesOut.values()) {
// Skip dependencies that are not installed.
if (edge.to === null) continue;
// NOTE: Any errors in edge are ignored.
const dep = edge.to;
// Gather dependencies to traverse them.
deps.push(dep);
// Gather dependencies real paths.
result.set(node.realpath, [
...(result.get(node.realpath) || []),
dep.realpath,
]);
}
return deps;
},
});
// Convert map to object.
return Object.fromEntries(result);
}