check-versions.js
//定制控制台日记的输入款式
var chalk = require('chalk')
// 加载语义化版本测试库
var semver = require('semver')
// 引入package.json文件
var packageConfig = require('../package.json')
function exec(cmd) {
// require('child_process')挪用nodejs子历程,
// execSync同步的exec要领实行command
return require('child_process').execSync(cmd).toString().trim()
}
var versionRequirements = [{
name: 'node',
// process.version是当前运用的node版本信息'v7.1.0'
// semver.clean(' =v1.2.3 ')返回'1.2.3'
// semver.clean(process.version)格式化返回当前运用的node版本信息'7.1.0'
currentVersion: semver.clean(process.version),
// 从package.json读取node版本请求
versionRequirement: packageConfig.engines.node
}, {
name: 'npm',
currentVersion: exec('npm --version'),
// 从package.json读取npm版本请求
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()
// 根据linux的范例,平常成服从0示意,而非0则示意失利。存在不满足版本请求的模块,实行失利
process.exit(1)
}
}