UNPKG

7.04 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.testEngine = testEngine;
7exports.checkOne = checkOne;
8exports.check = check;
9exports.shouldCheck = shouldCheck;
10
11var _errors;
12
13function _load_errors() {
14 return _errors = require('./errors.js');
15}
16
17var _map;
18
19function _load_map() {
20 return _map = _interopRequireDefault(require('./util/map.js'));
21}
22
23var _misc;
24
25function _load_misc() {
26 return _misc = require('./util/misc.js');
27}
28
29var _yarnVersion;
30
31function _load_yarnVersion() {
32 return _yarnVersion = require('./util/yarn-version.js');
33}
34
35var _semver;
36
37function _load_semver() {
38 return _semver = require('./util/semver.js');
39}
40
41function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
42
43const semver = require('semver');
44
45const VERSIONS = Object.assign({}, process.versions, {
46 yarn: (_yarnVersion || _load_yarnVersion()).version
47});
48
49function isValid(items, actual) {
50 let isNotWhitelist = true;
51 let isBlacklist = false;
52
53 for (var _iterator = items, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
54 var _ref;
55
56 if (_isArray) {
57 if (_i >= _iterator.length) break;
58 _ref = _iterator[_i++];
59 } else {
60 _i = _iterator.next();
61 if (_i.done) break;
62 _ref = _i.value;
63 }
64
65 const item = _ref;
66
67 // blacklist
68 if (item[0] === '!') {
69 isBlacklist = true;
70
71 if (actual === item.slice(1)) {
72 return false;
73 }
74 // whitelist
75 } else {
76 isNotWhitelist = false;
77
78 if (item === actual) {
79 return true;
80 }
81 }
82 }
83
84 // npm allows blacklists and whitelists to be mixed. Blacklists with
85 // whitelisted items should be treated as whitelists.
86 return isBlacklist && isNotWhitelist;
87}
88
89const aliases = (0, (_map || _load_map()).default)({
90 iojs: 'node' // we should probably prompt these libraries to fix this
91});
92
93const ignore = ['npm', // we'll never satisfy this for obvious reasons
94'teleport', // a module bundler used by some modules
95'rhino', // once a target for older modules
96'cordovaDependencies', // http://bit.ly/2tkUePg
97'parcel'];
98
99function testEngine(name, range, versions, looseSemver) {
100 const actual = versions[name];
101 if (!actual) {
102 return false;
103 }
104
105 if (!semver.valid(actual, looseSemver)) {
106 return false;
107 }
108
109 if (semver.satisfies(actual, range, looseSemver)) {
110 return true;
111 }
112
113 if (name === 'yarn' && (0, (_semver || _load_semver()).satisfiesWithPrereleases)(actual, range, looseSemver)) {
114 return true;
115 }
116
117 if (name === 'node' && semver.gt(actual, '1.0.0', looseSemver)) {
118 // WARNING: this is a massive hack and is super gross but necessary for compatibility
119 // some modules have the `engines.node` field set to a caret version below semver major v1
120 // eg. ^0.12.0. this is problematic as we enforce engines checks and node is now on version >=1
121 // to allow this pattern we transform the node version to fake ones in the minor range 10-13
122 const major = semver.major(actual, looseSemver);
123 const fakes = [`0.10.${major}`, `0.11.${major}`, `0.12.${major}`, `0.13.${major}`];
124 for (var _iterator2 = fakes, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
125 var _ref2;
126
127 if (_isArray2) {
128 if (_i2 >= _iterator2.length) break;
129 _ref2 = _iterator2[_i2++];
130 } else {
131 _i2 = _iterator2.next();
132 if (_i2.done) break;
133 _ref2 = _i2.value;
134 }
135
136 const actualFake = _ref2;
137
138 if (semver.satisfies(actualFake, range, looseSemver)) {
139 return true;
140 }
141 }
142 }
143
144 // incompatible version
145 return false;
146}
147
148function isValidArch(archs) {
149 return isValid(archs, process.arch);
150}
151
152function isValidPlatform(platforms) {
153 return isValid(platforms, process.platform);
154}
155
156function checkOne(info, config, ignoreEngines) {
157 let didIgnore = false;
158 let didError = false;
159 const reporter = config.reporter;
160 const human = `${info.name}@${info.version}`;
161
162 const pushError = msg => {
163 const ref = info._reference;
164
165 if (ref && ref.optional) {
166 ref.ignore = true;
167 ref.incompatible = true;
168
169 reporter.info(`${human}: ${msg}`);
170 if (!didIgnore) {
171 reporter.info(reporter.lang('optionalCompatibilityExcluded', human));
172 didIgnore = true;
173 }
174 } else {
175 reporter.error(`${human}: ${msg}`);
176 didError = true;
177 }
178 };
179
180 const os = info.os,
181 cpu = info.cpu,
182 engines = info.engines;
183
184
185 if (shouldCheckPlatform(os, config.ignorePlatform) && !isValidPlatform(os)) {
186 pushError(reporter.lang('incompatibleOS', process.platform));
187 }
188
189 if (shouldCheckCpu(cpu, config.ignorePlatform) && !isValidArch(cpu)) {
190 pushError(reporter.lang('incompatibleCPU', process.arch));
191 }
192
193 if (shouldCheckEngines(engines, ignoreEngines)) {
194 for (var _iterator3 = (0, (_misc || _load_misc()).entries)(info.engines), _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
195 var _ref3;
196
197 if (_isArray3) {
198 if (_i3 >= _iterator3.length) break;
199 _ref3 = _iterator3[_i3++];
200 } else {
201 _i3 = _iterator3.next();
202 if (_i3.done) break;
203 _ref3 = _i3.value;
204 }
205
206 const entry = _ref3;
207
208 let name = entry[0];
209 const range = entry[1];
210
211 if (aliases[name]) {
212 name = aliases[name];
213 }
214
215 if (VERSIONS[name]) {
216 if (!testEngine(name, range, VERSIONS, config.looseSemver)) {
217 pushError(reporter.lang('incompatibleEngine', name, range, VERSIONS[name]));
218 }
219 } else if (ignore.indexOf(name) < 0) {
220 reporter.warn(`${human}: ${reporter.lang('invalidEngine', name)}`);
221 }
222 }
223 }
224
225 if (didError) {
226 throw new (_errors || _load_errors()).MessageError(reporter.lang('foundIncompatible'));
227 }
228}
229
230function check(infos, config, ignoreEngines) {
231 for (var _iterator4 = infos, _isArray4 = Array.isArray(_iterator4), _i4 = 0, _iterator4 = _isArray4 ? _iterator4 : _iterator4[Symbol.iterator]();;) {
232 var _ref4;
233
234 if (_isArray4) {
235 if (_i4 >= _iterator4.length) break;
236 _ref4 = _iterator4[_i4++];
237 } else {
238 _i4 = _iterator4.next();
239 if (_i4.done) break;
240 _ref4 = _i4.value;
241 }
242
243 const info = _ref4;
244
245 checkOne(info, config, ignoreEngines);
246 }
247}
248
249function shouldCheckCpu(cpu, ignorePlatform) {
250 return !ignorePlatform && Array.isArray(cpu) && cpu.length > 0;
251}
252
253function shouldCheckPlatform(os, ignorePlatform) {
254 return !ignorePlatform && Array.isArray(os) && os.length > 0;
255}
256
257function shouldCheckEngines(engines, ignoreEngines) {
258 return !ignoreEngines && typeof engines === 'object';
259}
260
261function shouldCheck(manifest, options) {
262 return shouldCheckCpu(manifest.cpu, options.ignorePlatform) || shouldCheckPlatform(manifest.os, options.ignorePlatform) || shouldCheckEngines(manifest.engines, options.ignoreEngines);
263}
\No newline at end of file