This repository has been archived by the owner on Apr 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
check-engines.js
65 lines (56 loc) · 1.94 KB
/
check-engines.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
// Cut-and-paste from FBJS scripts
/*eslint-disable max-len*/
'use strict';
var fs = require('fs');
var assert = require('assert');
var exec = require('child_process').exec;
var semver = require('semver');
var f = require('util').format;
// Make sure we have a package.json to parse. Take it as the first argument
// (actually the 3rd for argv).
assert(
process.argv.length >= 3,
'Expected to receive a package.json file argument to parse'
);
var packageFilePath = process.argv[2];
var packageData;
try {
var packageFile = fs.readFileSync(packageFilePath, {encoding: 'utf-8'});
packageData = JSON.parse(packageFile);
} catch (e) {
assert(
false,
f('Expected to be able to parse %s as JSON but we got this error instead: %s', packageFilePath, e)
);
}
var enginesKey = process.argv[3] === '--dev' ? 'devEngines' : 'engines';
var engines = packageData[enginesKey];
if (engines.node !== undefined) {
// First check that engines are valid semver
assert(
semver.validRange(engines.node),
f('engines.node (%s) is not a valid semver range', engines.node)
);
// Then actually check that our version satisfies
var nodeVersion = process.versions.node;
assert(
semver.satisfies(nodeVersion, engines.node),
f('Current node version is not supported for development, expected "%s" to satisfy "%s".', nodeVersion, engines.node)
);
}
if (engines.npm !== undefined) {
// First check that engines are valid semver
assert(
semver.validRange(engines.npm),
f('engines.npm (%s) is not a valid semver range', engines.npm)
);
// Then actually check that our version satisfies
exec('npm --version', function(err, stdout, stderr) {
assert(err === null, f('Failed to get npm version... %s'), stderr);
var npmVersion = stdout.trim();
assert(
semver.satisfies(npmVersion, engines.npm),
f('Current npm version is not supported for development, expected "%s" to satisfy "%s".', npmVersion, engines.npm)
);
});
}