UNPKG

2.53 kBJavaScriptView Raw
1/*
2 * Copyright (c) 2018, salesforce.com, inc.
3 * All rights reserved.
4 * Licensed under the BSD 3-Clause license.
5 * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
6 */
7// Note: Leave this file as ES5 js for compatibility with earlier Node.js versions
8/* eslint-disable no-console, no-process-exit, prefer-template */
9'use strict';
10const pjson = require('../package.json');
11/**
12 * Determines whether or not a tag string is a semantic version.
13 *
14 * @param {*} tag The possible version string
15 * @returns {boolean} True, if the string is recognized as a semantic version
16 */
17function isVersion(tag) {
18 if (!tag) {
19 return false;
20 }
21 // From https://github.com/sindresorhus/semver-regex
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}
25module.exports.isVersion = isVersion;
26/**
27 * Compares two semantic version strings.
28 *
29 * @param {string} a The first version
30 * @param {string} b The second version
31 * @returns {number} < 0 if a < b, 0 if a == b, > 0 if a > b
32 */
33function 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}
60module.exports.compareVersions = compareVersions;
61/**
62 * Checks the current Node version for compatibility before launching the CLI.
63 */
64function 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}
71module.exports.checkNodeVersion = checkNodeVersion;
72//# sourceMappingURL=versions.js.map
\No newline at end of file