Last active
March 6, 2024 01:37
-
-
Save kentcdodds/abbc32701f78fa70298d444c2303b6d9 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 hidden or 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": "workshop-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 a workshop", | |
"bin": "./validate-system.js", | |
"dependencies": { | |
"semver": "7.1.3" | |
} | |
} |
This file contains hidden or 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 | |
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 + | |
'`.' | |
) | |
}, | |
git: function(actual, desired) { | |
return ( | |
'This computer has git@' + | |
actual + | |
' installed, but git@' + | |
desired + | |
' is required. Please install the proper version of git https://git-scm.com/book/en/v2/Getting-Started-Installing-Git' | |
) | |
} | |
} | |
var validators = { | |
node: function(desired) { | |
return validateVersion(desired, 'node --version', messages.node) | |
}, | |
npm: function(desired) { | |
return validateVersion(desired, 'npm --version', messages.npm) | |
}, | |
git: function(desired) { | |
var actual = '0.0.0' | |
try { | |
const versionOutput = execSync('git --version') | |
.toString() | |
.trim() | |
try { | |
actual = versionOutput | |
.match(/git version (?<version>\d+\.\d+\.\d+)/) | |
.groups | |
.version | |
} catch (error) { | |
return ( | |
'There was an error extracting the git version ' + | |
'from the git --version output:\n' + | |
versionOutput + | |
'\n\nIt should say "git version NUMBER.NUMBER.NUMBER" but' + | |
'did not. Please report this error output!' | |
) | |
} | |
} catch (error) { | |
return ( | |
'There was an error running the command `' + | |
command + | |
'`:\n' + | |
error.message | |
) | |
} | |
return semver.satisfies(actual, desired) ? null : messages.git(actual, desired) | |
}, | |
} | |
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) | |
} |
Whats the error?
This doesn't seem to work for node version 15
Whats the error?
This doesn't seem to work for node version 15
It says to only use between node versions 10 and 14
The latest version of all the workshops allow for Node 15 now.
@kentcdodds would you add support to Node 19 too?
That is configured in the package.json of the workshops. Feel free to open a pull request if you'd like.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This doesn't seem to work for node version 15