Created
February 8, 2018 07:32
-
-
Save jennasalau/765a1bf66e3e563022a7dbe165e84fb1 to your computer and use it in GitHub Desktop.
Quick and dirty script to verify node and npm "engine" versions defined in `package.json` (Note it ignores patch numbers)
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
#!/usr/bin/env node | |
const exec = require("child_process").exec; | |
const pkg = require('../package.json'); | |
// Helper script that ensures developers are using the same node versions | |
function runVersionCommand(command, callback) { | |
exec(command, function(execError, stdin, stderr) { | |
const commandDescription = JSON.stringify(command); | |
if (execError) { | |
const runError = new Error("Command failed: " + commandDescription); | |
if (stderr) { | |
runError.stderr = stderr.trim(); | |
} | |
if (execError) { | |
runError.execError = execError; | |
} | |
return callback(runError); | |
} | |
else { | |
return callback(null, { | |
version: stdin.toString().split('\n')[0].trim().replace(/^v/,''), | |
}); | |
} | |
}); | |
} | |
function parseVersionString (str) { | |
if (typeof(str) !== 'string') { return false; } | |
const x = str.split('.'); | |
// parse from string or default to 0 if can't parse | |
const maj = parseInt(x[0]) || 0; | |
const min = parseInt(x[1]) || 0; | |
const pat = parseInt(x[2]) || 0; | |
return { | |
major: maj, | |
minor: min, | |
patch: pat | |
} | |
} | |
console.log(`👮 Checking engine versions`); | |
const wantedNode = parseVersionString(pkg.engines.node.replace(/^\^/,'').replace(/^v/,'')); | |
const wantedNpm = parseVersionString(pkg.engines.npm.replace(/^\^/,'').replace(/^v/,'')); | |
runVersionCommand('node --version', (err, response) => { | |
if (err) { | |
console.log(err); | |
process.exit(1); | |
} | |
const seenNode = parseVersionString(response.version); | |
if (seenNode.major !== wantedNode.major || (seenNode.major === wantedNode.major && seenNode.minor !== wantedNode.minor)) { | |
console.log('\x1b[31m%s\x1b[0m', `👮 Your "node" versions is incompatible. Wanted ${pkg.engines.node}`); | |
console.log('\x1b[31m%s\x1b[0m', `Try running "nvm use" (https://github.com/creationix/nvm)`); | |
process.exit(1); | |
} | |
runVersionCommand('npm --version', (err, response) => { | |
if (err) { | |
console.log(err); | |
process.exit(1); | |
} | |
const seenNpm = parseVersionString(response.version); | |
if (seenNpm.major !== wantedNpm.major || (seenNpm.major === wantedNpm.major && seenNpm.minor !== wantedNpm.minor)) { | |
console.log('\x1b[31m%s\x1b[0m', `👮 Your "npm" versions is incompatible. Wanted ${pkg.engines.npm}`); | |
console.log('\x1b[31m%s\x1b[0m', `Try running "npm install npm@${wantedNpm.major}.${wantedNpm.minor} -g"`); | |
process.exit(1); | |
} | |
console.log(`👮 ✅`); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment