1. 程式人生 > >check-versions.js和dev-client.js

check-versions.js和dev-client.js

rim 控制臺 anti exec process ++ 輸出帶顏色字體 n) nbsp

// 用於在控制臺輸出帶顏色字體的插件
var chalk = require(‘chalk‘)

// 語義化版本檢查插件(The semantic version parser used by npm)
var semver = require(‘semver‘)

// 引入package.json
var packageConfig = require(‘../package.json‘)

// 開辟子進程執行指令cmd並返回結果
function exec (cmd) {
return require(‘child_process‘).execSync(cmd).toString().trim()
}

// node和npm版本需求

var versionRequirements = [
{
name: ‘node‘,
currentVersion: semver.clean(process.version),
versionRequirement: packageConfig.engines.node
},
{
name: ‘npm‘,
currentVersion: exec(‘npm --version‘),
versionRequirement: packageConfig.engines.npm
}
]

module.exports = function () {
var warnings = []
// 依次判斷版本是否符合要求

for (var i = 0; i < versionRequirements.length; i++) {
var mod = versionRequirements[i]
if (!semver.satisfies(mod.currentVersion, mod.versionRequirement)) {
warnings.push(mod.name + ‘: ‘ +
chalk.red(mod.currentVersion) + ‘ should be ‘ +
chalk.green(mod.versionRequirement)
)
}
}

// 如果有警告則將其輸出到控制臺
if (warnings.length) {
console.log(‘‘)
console.log(chalk.yellow(‘To use this template, you must update following to modules:‘))
console.log()
for (var i = 0; i < warnings.length; i++) {
var warning = warnings[i]
console.log(‘ ‘ + warning)
}
console.log()
process.exit(1)
}
}

check-versions.js和dev-client.js