//此文件是检查node+npm的版本 在build.js里面使用
'use strict'
//导入chalk模块 用来改变字体颜色
const chalk = require('chalk')
//导入semver即语义化版本 用来控制版本
const semver = require('semver')
//导入package.json
const packageConfig = require('../package.json')
//导入shelljs模块 用来执行unix命令
const shell = require('shelljs')
//封装方法 用来获取纯净的版本号
//child_process是node用来创建子进程 execSync是创建同步进程
function exec(cmd){
return require('child_process').execSync(cmd).toString().trim()
}
//node版本信息
var versionRequirements = [
{
//名称是Node
name:'node',
//当前的node版本号
currentVersion:semver.clean(process.version),
//要求的node版本号
versionRequirement:packageConfig.engines.node
}
]
if(shell.which('npm')){
//将npm添加到versionRequirements
versionRequirements.push({
//名称是npm
name:'npm',
//纯净的当前npm版本号
currentVersion:exec('npm --version'),
//要求的npm版本号
versionRequirement:packageConfig.engines.npm
})
}
module.exports=function(){
const warnings = []
for(let i = 0;i < versionRequirements.length;i++){
const mod = versionRequirements[i]
//如果当前版本号不符合要求的版本号,那么就将提示信息添加到wranings
if(!semver.satisfies(mod.currentVersion,mod.versionRequirement)){
warnings.push(mod.name + ':' +chalk.red(mod.currentVersion) + ' should be '+ chalk.green(mod.versionRequirement))
}
}
//如果有warnings 那么就打印出来
if(warnings.length){
console.log('')
console.log(chalk.yellow('To use this template,you must update following to modules;'))
console.log()
for(let i = 0;i < warnings.length;i++){
const warning = warnings[i]
console.log(' '+warning)
}
//执行失败
process.exit(1)
}
}