Skip to content

Commit c28bd3d

Browse files
committed
feat: (wip) stub out dep resolver
1 parent 93e6599 commit c28bd3d

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

lib/deps.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'use strict';
2+
3+
const { Arborist } = require('@npmcli/arborist');
4+
const Fs = require('fs');
5+
const Path = require('path');
6+
const Tempy = require('tempy');
7+
8+
9+
const internals = {};
10+
11+
12+
internals.walk = (node, callback) => {
13+
14+
callback(node);
15+
16+
if (!node.children) {
17+
return;
18+
}
19+
20+
node.children.forEach((child) => {
21+
22+
internals.walk(child, callback);
23+
});
24+
};
25+
26+
27+
exports.resolve = async (packageJson) => {
28+
29+
// @todo: handle lock files
30+
31+
const path = Tempy.directory();
32+
Fs.writeFileSync(Path.join(path, 'package.json'), JSON.stringify(packageJson, null, ' '));
33+
34+
const arborist = new Arborist({ path });
35+
36+
await arborist.buildIdealTree();
37+
38+
const map = {};
39+
40+
internals.walk(arborist.idealTree, (node) => {
41+
42+
if (node === arborist.idealTree) {
43+
return;
44+
}
45+
46+
map[node.name] = map[node.name] || new Set();
47+
map[node.name].add(node.package.version);
48+
});
49+
50+
const result = {};
51+
52+
for (const name of Object.keys(map).sort()) {
53+
result[name] = [...map[name]];
54+
}
55+
56+
return result;
57+
};

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@
3737
},
3838
"dependencies": {
3939
"@hapi/wreck": "^17.0.0",
40+
"@npmcli/arborist": "0.0.0-pre.13",
4041
"@pkgjs/nv": "0.0.3",
4142
"git-url-parse": "^11.1.2",
4243
"js-yaml": "^3.13.1",
4344
"minimist": "^1.2.5",
4445
"pacote": "^11.1.0",
45-
"simple-git": "^1.131.0"
46+
"simple-git": "^1.131.0",
47+
"tempy": "^0.5.0"
4648
}
4749
}

0 commit comments

Comments
 (0)