check-versions.js解释

//此文件是检查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)
    }
}


 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值