check-versions.js

来源:互联网 发布:电脑桌面工作计划软件 编辑:程序博客网 时间:2024/06/05 09:50
这里面的代码主要是通过调用shell去打印出node和npm的版本信息,同时会给出版本依赖的警告信息.
'use strict'
const chalk = require('chalk')  // 用于在控制台输出高亮字体的插件
const semver = require('semver')  // 语义化版本检查插件
const packageConfig = require('../package.json')  // 引入package.json
const shell = require('shelljs')  //引入shelljs
// 开辟子进程执行指令cmd并返回结果
function exec (cmd) {
  return require('child_process').execSync(cmd).toString().trim()
}
// node和npm版本需求
const versionRequirements = [
  {
    name: 'node',
    currentVersion: semver.clean(process.version),
    versionRequirement: packageConfig.engines.node
  }
]

if (shell.which('npm')) {
  versionRequirements.push({
    name: 'npm',
    currentVersion: exec('npm --version'),
    versionRequirement: packageConfig.engines.npm
  })
}

module.exports = function () {
  const warnings = []
  // 依次判断版本是否符合要求
  for (let i = 0; i < versionRequirements.length; i++) {
    const 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 (let i = 0; i < warnings.length; i++) {
      const warning = warnings[i]
      console.log('  ' + warning)
    }
    console.log()
    process.exit(1)
  }
}

阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 玛巴赫 格拉德巴赫 托马斯巴赫 巴赫测试 贾汉巴赫什 欧巴赫 万巴赫 奥尔巴赫 德巴赫钢琴 巴赫图片 巴赫的作品 巴赫代表作 巴赫猜想 康巴赫炒锅怎么样 康巴赫炒锅蜂巢304质量好差 歌德巴赫猜想 小步舞曲巴赫 巴赫小步舞曲 康巴赫蜂巢304缺点 麦巴赫豪车多少钱 我的巴赫先生 康巴赫不粘锅 巴赫巴利亚王2终结 巴赫十二平均律 康巴赫锅怎么样 哥得巴赫猜想是什么 康巴赫是德国品牌吗 蒂伯巴赫村的孩子们 巴赫无伴奏大提琴组曲 巴赫g弦上的咏叹调 巴赫48首平均律 康巴赫不粘锅怎么样 巴赫 小步舞曲 纳戈尔诺-卡拉巴赫 巴达 庞巴迪 巴迪老师 艾瑞巴迪 庞巴迪三轮 庞巴迪900 庞巴迪飞机