1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | 'use strict';
|
10 | const pjson = require('../package.json');
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | function isVersion(tag) {
|
18 | if (!tag) {
|
19 | return false;
|
20 | }
|
21 |
|
22 | const SEMVER_REGEX = /^v?(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-[\da-z-]+(?:\.[\da-z-]+)*)?(?:\+[\da-z-]+(?:\.[\da-z-]+)*)?$/ig;
|
23 | return SEMVER_REGEX.test(tag.toString());
|
24 | }
|
25 | module.exports.isVersion = isVersion;
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | function compareVersions(a, b) {
|
34 | a = a || '0';
|
35 | b = b || '0';
|
36 | const ignore = /-.*$/;
|
37 | const partsA = a.replace(ignore, '').split('.');
|
38 | const partsB = b.replace(ignore, '').split('.');
|
39 | const len = Math.max(partsA.length, partsB.length);
|
40 | const diffInLength = partsA.length - partsB.length;
|
41 | if (diffInLength < 0) {
|
42 | for (let i = partsA.length; i < len; i++) {
|
43 | partsA[i] = 0;
|
44 | }
|
45 | }
|
46 | else {
|
47 | for (let i = partsB.length; i < len; i++) {
|
48 | partsB[i] = 0;
|
49 | }
|
50 | }
|
51 | let diff;
|
52 | for (let i = 0; i < len; i++) {
|
53 | diff = (parseInt(partsA[i], 10)) - (parseInt(partsB[i], 10));
|
54 | if (diff) {
|
55 | return diff;
|
56 | }
|
57 | }
|
58 | return 0;
|
59 | }
|
60 | module.exports.compareVersions = compareVersions;
|
61 |
|
62 |
|
63 |
|
64 | function checkNodeVersion(currentVersion = process.versions.node, requiredVersion = pjson.engines.node.slice(2)) {
|
65 | if (module.exports.compareVersions(currentVersion, requiredVersion) < 0) {
|
66 | console.error('Unsupported Node.js version ' + currentVersion + ', ' +
|
67 | 'version ' + requiredVersion + ' or later is required.');
|
68 | process.exit(1);
|
69 | }
|
70 | }
|
71 | module.exports.checkNodeVersion = checkNodeVersion;
|
72 |
|
\ | No newline at end of file |