-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
110 lines (102 loc) · 2.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
'use strict';
const Arborist = require('@npmcli/arborist');
const colors = require('colors/safe');
const { manifest } = require('pacote');
const lockfileInfo = require('lockfile-info');
const flat = require('array.prototype.flat');
function prune(tree, {
dev: keepDev,
production: keepProduction,
peer: keepPeer,
}) {
if (!keepDev || !keepProduction) {
for (const node of tree.children.values()) {
if ((!keepDev && node.dev) || (!keepProduction && !node.dev) || (!keepPeer && node.peer)) {
node.root = null;
}
}
}
return tree;
}
async function getBaseTree({
mode,
arb,
fullMetadata,
packumentCache,
logger,
}) {
const {
hasNodeModulesDir,
hasLockfile,
hasPackageJSON,
lockfileVersion,
} = await lockfileInfo();
if (mode === 'actual' || (mode === 'auto' && hasNodeModulesDir)) {
const messages = flat([
hasNodeModulesDir ? `\`${colors.gray('node_modules')}\` found` : [],
mode === 'actual' ? 'mode is “actual”' : [],
]);
logger(colors.green(`${messages.join(', ')}; loading tree from disk...`));
return arb.loadActual({ fullMetadata: true, packumentCache });
}
if (mode === 'virtual' || (mode === 'auto' && hasLockfile)) {
if (hasLockfile && lockfileVersion < 2) {
const messages = ['v1 lockfile found'].concat(mode === 'virtual' ? 'mode is “virtual”' : []);
logger(colors.green(`${messages.join(', ')}; loading ideal tree from lockfile...`));
const tree = await arb.buildIdealTree({ fullMetadata: true });
await Promise.all(Array.from(
tree.children.values(),
async (node) => {
// eslint-disable-next-line no-param-reassign
node.package = await manifest(`${node.name}@${node.package.version}`, {
fullMetadata: true,
packumentCache,
});
},
));
return tree;
}
const messages = flat([
hasLockfile ? 'Lockfile found' : [],
mode === 'virtual' ? 'mode is “virtual”' : [],
]);
logger(colors.green(`${messages.join(', ')}; loading virtual tree from lockfile...`));
return arb.loadVirtual({ fullMetadata: true, packumentCache });
}
const messages = flat([
`\`${colors.gray('package.json')}\` ${hasPackageJSON ? '' : 'not '}found`,
mode === 'ideal' ? 'mode is “ideal”' : [],
]);
logger(colors.green(`${messages.join(', ')}; building ideal tree from \`${colors.gray('package.json')}\`...`));
return arb.buildIdealTree({ fullMetadata, packumentCache, update: true });
}
const defaultLogger = (x) => console.log(x);
module.exports = async function getTree(mode, {
dev = false,
peer = true,
production = true,
fullMetadata = false,
packumentCache = new Map(),
path = process.cwd(),
logger = defaultLogger,
} = {}) {
const arb = new Arborist({
Arborist, // TODO: investigate why this absurdity is necessary
fullMetadata,
packumentCache,
path,
});
const tree = await getBaseTree({
mode,
arb,
fullMetadata,
packumentCache,
logger,
});
prune(tree, {
dev,
production,
peer,
});
return tree;
};