-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcheckEngines.js
166 lines (153 loc) · 6.08 KB
/
checkEngines.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
'use strict';
const colors = require('colors/safe');
const fromEntries = require('object.fromentries');
const groupBy = require('object.groupby');
const { inspect } = require('util');
const EXITS = require('./exit-codes');
const table = require('./table');
function isSubset(inner, outer) {
const outerS = new Set(outer);
return inner.every((item) => outerS.has(item));
}
module.exports = async function checkEngines(
selectedEngines,
rootEngines,
rootValids,
graphValids,
graphAllowed,
graphRanges,
shouldSave,
) {
const engineEntries = selectedEngines.map((engine) => [engine, '*'])
.concat(Object.entries(graphRanges).map(([engine, { displayRange }]) => [engine, displayRange]))
.filter(([engine, displayRange]) => engine === 'node' || displayRange !== '*');
const engines = fromEntries(engineEntries);
const fixMessage = shouldSave
? `\n\`${colors.gray('ls-engines')}\` will automatically fix this, per the \`${colors.gray('--save')}\` option, by adding the following to your \`${colors.gray('package.json')}\`:`
: `\nYou can fix this by running \`${colors.bold(colors.gray('ls-engines --save'))}\`, or by manually adding the following to your \`${colors.gray('package.json')}\`:`;
let allOmitted = true;
let anyOmitted = false;
let allStar = true;
let anyStar = false;
const same = [];
const subset = [];
const superset = [];
const conflicting = {};
selectedEngines.forEach((engine) => {
const value = rootEngines[engine];
if (typeof value === 'string') {
allOmitted = false;
if (value === '*') {
anyStar = true;
} else {
allStar = false;
}
} else {
anyOmitted = true;
}
const all = new Set(rootValids[engine].concat(graphValids[engine]));
const isSame = all.size === rootValids[engine].length && all.size === graphValids[engine].length;
const rootIsSubsetOfGraph = isSubset(rootValids[engine], graphValids[engine]);
const graphIsSubsetOfRoot = isSubset(graphValids[engine], rootValids[engine]);
if (isSame) {
same.push(engine);
conflicting[engine] = [];
} else {
const packageInvalids = graphAllowed
.filter(([, , { [engine]: vs }]) => !rootValids[engine].every((v) => vs.includes(v)))
.map(([name, depEngines, { [engine]: vs }]) => [name, depEngines[engine], rootValids[engine].filter((v) => !vs.includes(v))])
.sort(([a], [b]) => a.localeCompare(b));
conflicting[engine] = Object.entries(groupBy(packageInvalids, ([name]) => name)).map(([
name,
results,
]) => [
name,
Array.from(new Set(results.flatMap(([, depEngines]) => depEngines))).join('\n'),
]);
// if (isSubset(rootValids[engine], graphValids[engine])) {
if (graphIsSubsetOfRoot) {
superset.push(engine);
} else if (rootIsSubsetOfGraph) {
subset.push(engine);
}
// }
}
});
let message;
if (allOmitted) {
message = '\nYour “engines” field is missing! Prefer explicitly setting a supported engine range.';
} else if (anyOmitted) {
message = '\nYour “engines” field has some of your selected engines missing! Prefer explicitly setting a supported engine range.';
} else if (allStar) {
message = '\nYour “engines” field has your selected engines set to `*`! Prefer explicitly setting a supported engine range.';
} else if (anyStar) {
message = '\nYour “engines” field has some of your selected engines set to `*`! Prefer explicitly setting a supported engine range.';
}
if (message) {
throw {
code: EXITS.IMPLICIT,
output: [
colors.bold(colors.red(message)),
fixMessage,
colors.blue(`"engines": ${JSON.stringify(engines, null, 2)}`),
],
save(pkg) {
// eslint-disable-next-line no-param-reassign
pkg.engines = { ...pkg.engines, ...engines };
},
};
}
if (same.length === selectedEngines.length) {
return {
output: [
colors.bold(colors.green('\nYour “engines” field exactly matches your dependency graph’s requirements!')),
],
};
}
if (superset.length > 0 || subset.length > 0) {
const expandMessage = shouldSave
? `\n\`${colors.gray('ls-engines')}\` will automatically ${superset.length > 0 ? 'narrow' : 'widen'} your support, per the \`${colors.gray('--save')}\` option, by adding the following to your \`${colors.gray('package.json')}\`:`
: `\nIf you want to ${superset.length > 0 ? 'narrow' : 'widen'} your support, you can run \`${colors.bold(colors.gray('ls-engines --save'))}\`, or manually add the following to your \`${colors.gray('package.json')}\`:`;
const conflictingTable = conflicting.node.length > 0 ? `\n${table([].concat(
[[`Conflicting dependencies (${conflicting.node.length})`, 'engines.node'].map((x) => colors.bold(colors.gray(x)))],
conflicting.node.map(([name, range]) => [name, range].map((x) => colors.gray(x))),
))}` : [];
const result = {
code: superset.length > 0 ? EXITS.INEXACT : EXITS.SUCCESS,
output: [].concat(
colors.bold(colors[superset.length > 0 ? 'yellow' : 'green'](`\nYour “engines” field allows ${superset.length > 0 ? 'more' : 'fewer'} node versions than your dependency graph does.`)),
conflictingTable,
process.env.DEBUG ? table([].concat(
[['Graph deps', 'engines'].map((x) => colors.bold(colors.gray(x)))],
graphAllowed.map(([a, b]) => [colors.blue(a), inspect(b, { depth: Infinity, maxArrayLength: null })]),
)) : [],
expandMessage,
colors.blue(`"engines": ${JSON.stringify(engines, null, 2)}`),
),
save(pkg) {
// eslint-disable-next-line no-param-reassign
pkg.engines = { ...pkg.engines, ...engines };
},
};
if (result.code !== EXITS.SUCCESS) {
throw result;
}
return result;
}
throw {
code: EXITS.INEXACT,
output: [
colors.red('\nYour “engines” field does not exactly match your dependency graph‘s requirements!'),
fixMessage,
colors.blue(`"engines": ${JSON.stringify(engines, null, 2)}`),
process.env.DEBUG ? table([].concat(
[['Graph deps', 'engines'].map((x) => colors.bold(colors.gray(x)))],
graphAllowed.map(([a, b]) => [colors.blue(a), inspect(b, { depth: Infinity, maxArrayLength: null })]),
)) : [],
],
save(pkg) {
// eslint-disable-next-line no-param-reassign
pkg.engines = { ...pkg.engines, ...engines };
},
};
};