UNPKG

2.31 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.traverseDependencyGraph = void 0;
4const fs = require("fs-extra");
5const path_1 = require("path");
6const util = require("./util");
7/**
8 * Traverses the dependency graph and invokes the provided callback method for
9 * each individual dependency root directory (including the current package).
10 * The dependency roots are de-duplicated based on their absolute path on the
11 * file system.
12 *
13 * @param packageDir the current package's root directory (i.e: where the
14 * `package.json` file is located)
15 * @param callback the function to invoke with each package's informations
16 * @param host the dependency graph traversal host to use (this parameter
17 * should typically not be provided unless this module is
18 * being unit tested)
19 */
20async function traverseDependencyGraph(packageDir, callback, host = {
21 readJson: fs.readJson,
22 findDependencyDirectory: util.findDependencyDirectory,
23}) {
24 return real$traverseDependencyGraph(packageDir, callback, host, new Set());
25}
26exports.traverseDependencyGraph = traverseDependencyGraph;
27async function real$traverseDependencyGraph(packageDir, callback, host, visited) {
28 // We're at the root if we have not visited anything yet. How convenient!
29 const isRoot = visited.size === 0;
30 if (visited.has(packageDir)) {
31 return void 0;
32 }
33 visited.add(packageDir);
34 const meta = await host.readJson((0, path_1.join)(packageDir, 'package.json'));
35 if (!(await callback(packageDir, meta, isRoot))) {
36 return void 0;
37 }
38 const deps = new Set([
39 ...Object.keys(meta.dependencies ?? {}),
40 ...Object.keys(meta.peerDependencies ?? {}),
41 ]);
42 return Promise.all(Array.from(deps)
43 // No need to pacmak the dependency if it's built-in, or if it's bundled
44 .filter((m) => !util.isBuiltinModule(m) &&
45 !meta.bundledDependencies?.includes(m) &&
46 !meta.bundleDependencies?.includes(m))
47 .map(async (dep) => {
48 const dependencyDir = await host.findDependencyDirectory(dep, packageDir);
49 return real$traverseDependencyGraph(dependencyDir, callback, host, visited);
50 })).then();
51}
52//# sourceMappingURL=dependency-graph.js.map
\No newline at end of file