-
Notifications
You must be signed in to change notification settings - Fork 45
/
check-version.js
59 lines (55 loc) · 1.9 KB
/
check-version.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
/* eslint-disable */
const execSync = require('child_process').execSync;
let nodeVersion = require('./package.json').itemVersion.node;
let yarnVersion = require('./package.json').itemVersion.yarn;
function consoleError(msg) {
console.error(`\x1b[31m%s\x1b[0m`, msg);
}
function consoleInfo(msg) {
console.info(`\x1b[36m%s\x1b[0m`, msg);
}
// 获取yarn版本
function getYarnVersion() {
try {
return execSync('yarn --version', { encoding: 'utf8' }).replace(/v/i, '').trim();
} catch (error) {
consoleError('Yarn is not installed.');
process.exit(1);
}
}
// 获取node版本
function getNodeVersion() {
try {
return execSync('node --version', { encoding: 'utf8' }).replace(/v/i, '').trim();
} catch (error) {
consoleError('Node is not installed.');
process.exit(1);
}
}
// yarn.lock 文件是否存在
function haveYarnLock() {
const fs = require('fs');
return fs.existsSync('yarn.lock');
}
// 1. yarn.lock 文件是否存在
if (!haveYarnLock()) {
consoleError('yarn.lock file does not exist.');
process.exit(1);
}
// 2. yarn 是否安装
const currentYarnversion = getYarnVersion().replace(/\./g, '');
const currentNodeVersion = getNodeVersion().replace(/\./g, '');
const _nodeVersion = nodeVersion.replace(/\./g, '').split(' ')[1];
const _yarnVersion = yarnVersion.replace(/\./g, '').split(' ')[1];
// 3. node版本是否符合
if (currentNodeVersion !== _nodeVersion) {
consoleError(`Current node version is ${getNodeVersion()}, the required version is ${nodeVersion}`);
consoleInfo(`Please use nvm to switch the node version, https://github.com/coreybutler/nvm-windows`);
process.exit(1);
}
// 4. yarn版本是否符合
if (currentYarnversion < _yarnVersion) {
consoleError(`Current yarn version is ${getYarnVersion()}, the required version is ${yarnVersion}`);
consoleInfo(`Please upgrade yarn to the required version, https://github.com/yarnpkg/yarn/releases`);
process.exit(1);
}