Created
May 17, 2018 11:40
-
-
Save zleight1/fb18e019e787517b1bb8e2b52f65b725 to your computer and use it in GitHub Desktop.
A check version script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const semver = require('semver'); | |
const chalk = require('chalk'); | |
const packageConfig = require('./package.json'); | |
const exec = function (cmd) { | |
return require('child_process') | |
.execSync(cmd).toString().trim(); | |
}; | |
const versionRequirements = [ | |
{ | |
name: 'node', | |
currentVersion: semver.clean(process.version), | |
versionRequirement: packageConfig.engines.node | |
}, | |
{ | |
name: 'npm', | |
currentVersion: exec('npm --version'), | |
versionRequirement: packageConfig.engines.npm | |
} | |
]; | |
const warnings = []; | |
for (var 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(chalk.white.bgRed.bold('******************************************************************')); | |
console.log(chalk.white.bgRed.bold('To use this template, you must update the following modules: ')); | |
console.log(); | |
for (var i = 0; i < warnings.length; i++) { | |
const warning = warnings[i]; | |
console.log(` ${warning}`); | |
} | |
console.log(chalk.white.bgRed.bold('******************************************************************')); | |
console.log(); | |
process.exit(1); | |
} | |
else { | |
console.log(''); | |
console.log(chalk.green('NPM and Node versions are all good!')); | |
console.log(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment