Created
May 16, 2019 20:19
-
-
Save Joaquin6/5a532ae7b70c598173dec148aeec93e5 to your computer and use it in GitHub Desktop.
This script can run on a preinstall hook to validate engine versions.
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 | |
const { execSync } = require('child_process'); | |
const { clean, satisfies } = require('semver'); | |
const { green, red, yellow } = require('chalk'); | |
const { engines } = require('../package.json'); | |
const exec = cmd => execSync(cmd).toString().trim(); | |
const getEngineRequirement = name => ({ | |
name, | |
currentVersion: name === 'node' ? clean(process.version) : exec(`${name} --version`), | |
versionRequirement: engines[name], | |
}); | |
const getVersionRequirements = engine => engine | |
? getEngineRequirement(engine) | |
: Object.keys(engines).map(getEngineRequirement); | |
const getRequirementWarnings = (requirements = getVersionRequirements()) => requirements | |
.filter(({ currentVersion: a, versionRequirement: b }) => !satisfies(a, b)) | |
.map(({ currentVersion, name, versionRequirement }) => | |
`${name}: ${red(currentVersion)} should be ${green(versionRequirement)}`); | |
const logMessage = (warnings = []) => { | |
let msg = ''; | |
let title = 'All Engines are Compatible'; | |
let shouldExit = false; | |
if (warnings.length) { | |
shouldExit = true; | |
title = 'Detected Incompatible Engines:'; | |
warnings.forEach(warning => { | |
msg += '\n\t' + warning; | |
}); | |
} | |
// eslint-disable-next-line no-console | |
console.log(` | |
${yellow(title)} ${msg} | |
`); | |
if (shouldExit) { | |
process.exit(1); | |
} | |
}; | |
const checkVersions = engine => logMessage(getRequirementWarnings(getVersionRequirements(engine))); | |
module.exports = checkVersions; | |
checkVersions(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment