CHECK-VERSIONS.JS,VUE-CLI Check the version of the JS file.
' Use strict 'Const Chalk= Require (' Chalk ') Const Semver= Require (' Semver ') Const Packageconfig= require ('.. /package.json ') Const Shell= Require (' Shelljs ')functionexec (cmd) {returnRequire (' child_process '). Execsync (cmd). toString (). Trim ()}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 the use of 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) }}
Const Chalk = require (' Chalk '): The introduction of a module to output different color text on the command line, by Chalk.yellow ("Want to add color text ...") to achieve the change of text color;
Const SEMVER = require (' semver '): A NPM package that introduces a semantic version of a file, which is actually used to control the version. The common methods of "Semver" are as follows:
// ' 1.2.3 ', returns versions that conform to the version format // NULL if a non-conforming version returns nullSemver.clean (' =v1.2.3 // ' 1.2.3 ', returns a standard version number, and strips both sides of the space / / True, the first parameter is the version number of the test, the second parameter is the matching version, and if the match returns true// false, whether the first parameter is greater than the second // True, the first parameter of the version you are smaller than the second one
Reference: Https://npm.taobao.org/package/semver
Const PACKAGECONFIG = require ('.. /package.json '): Introduce Package.json to get the version.
Const SHELL = require (' Shelljs '): The package used to execute UNIX commands.
// this piece is not very familiar, the general meaning is to use the JS code synchronously executes a CMD command, and executes the ToString () and Trim () method on the returned result function exec (cmd) { return require (' child_process '). Execsync (cmd). toString (). Trim ()}
Const VERSIONREQUIREMENTS = [ { ' node ', CurrentVersion:semver.clean ( process.version), VersionRequirement:packageConfig.engines.node }]
Semver.clean (process.version) described above, returned is a version of information, PackageConfig.engines.node is in Package.json, refers to >=4.0.0
if (Shell.which (' NPM ')) { Versionrequirements.push (' npm ', currentversion:exec (' NPM --version '), versionRequirement:packageConfig.engines.npm })}
The which method of Shelljs is to look for the command in the path to the system, Shell.which (' NPM ') is to look for the NPM command, and the exec () method is to execute a command (here is the NPM--version). A function that returns the version information. More things to see here about Shelljs:
Http://www.ruanyifeng.com/blog/2015/05/command-line-with-node.html
Https://www.npmjs.com/package/shelljs
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 the use of 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) }}
This part of the code is very good to understand that the current NPM and node version is not in accordance with the definition of Package.json, if not meet the hint wraning.
Hand tear Vue-cli configuration file--check-versions.js Chapter