Created
October 19, 2021 18:47
-
-
Save Jordan-Gilliam/a9b9fb60e6a0c7a27cca8836b46cdb89 to your computer and use it in GitHub Desktop.
Validates that the versions of tools specified in `engines` in the package.json are installed on the machine.
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
{ | |
"name": "rocket-science-computer-validator", | |
"version": "1.0.0", | |
"description": "I use this to validate people's computers have the proper versions of node and npm installed for rocket-science", | |
"bin": "./validate-system.js", | |
"dependencies": { | |
"semver": "7.1.3" | |
} | |
} |
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 | |
// CREDIT Kentcdodds workshop setup script ease of use | |
var path = require('path') | |
var execSync = require('child_process').execSync | |
var semver = require('semver') | |
var specified = process.argv[2] | |
var cwd = specified | |
? path.isAbsolute(specified) | |
? specified | |
: path.join(process.cwd(), specified) | |
: process.cwd() | |
var pkg = require(path.join(cwd, 'package.json')) | |
function validateVersion(desired, command, message) { | |
var actual = '0.0.0' | |
try { | |
actual = execSync(command) | |
.toString() | |
.trim() | |
} catch (error) { | |
return ( | |
'There was an error running the command `' + | |
command + | |
'`:\n' + | |
error.message | |
) | |
} | |
return semver.satisfies(actual, desired) ? null : message(actual, desired) | |
} | |
var messages = { | |
node: function(actual, desired) { | |
return ( | |
'This computer has node@' + | |
actual + | |
' installed, but node@' + | |
desired + | |
' is required. Please update node: https://nodejs.org' | |
) | |
}, | |
npm: function(actual, desired) { | |
return ( | |
'This computer has npm@' + | |
actual + | |
' installed, but npm@' + | |
desired + | |
' is required. Please update npm by running `npm install --global npm@' + | |
desired + | |
'`.' | |
) | |
}, | |
} | |
var validators = { | |
node: function(desired) { | |
return validateVersion(desired, 'node --version', messages.node) | |
}, | |
npm: function(desired) { | |
return validateVersion(desired, 'npm --version', messages.npm) | |
}, | |
} | |
var errors = Object.keys(pkg.engines) | |
.map(function(engine) { | |
if (!validators[engine]) { | |
throw new Error('Engine "' + engine + '" is unsupported.') | |
} | |
return validators[engine](pkg.engines[engine]) | |
}) | |
.filter(Boolean) | |
if (errors.length) { | |
console.error( | |
'There were errors validating the compatibility of this computer:', | |
) | |
console.error('\n ' + errors.join('\n ') + '\n\n') | |
console.error( | |
'If you would like to just ignore this error, then feel free to do so and install dependencies as you normally would in "' + | |
cwd + | |
'". Just know that things may not work properly if you do...', | |
) | |
process.exit(1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment