Created
May 12, 2021 21:09
-
-
Save designfrontier/58ea12f8fd8e2ded668798e876518340 to your computer and use it in GitHub Desktop.
node script that returns non-zero exit code if dependencies are updated but not the lock file
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 cp = require('child_process'); | |
const pkg = require('../package.json'); | |
const filesToParse = JSON.parse(process.env.files); | |
const checkPackageJSON = () => { | |
const s = cp.execSync('git diff HEAD~1 HEAD package.json').toString(); | |
const changedLines = s | |
.match(/[+]( ){2,4}.*/g) | |
.map((line) => line.replace(/[+ "]/g, '').split(':').shift()); | |
// find the keys associated with the line with the + | |
// find that key in the package.json file | |
// see if it is in devDependencies or dependencies | |
// if so... we care | |
// if not... we don't. | |
return changedLines.reduce((acc, key) => { | |
return acc || !!pkg.devDependencies[key] || !!pkg.dependencies[key]; | |
}, false); | |
}; | |
if ( | |
filesToParse.includes('package.json') && | |
!filesToParse.includes('package-lock.json') && | |
checkPackageJSON() | |
) { | |
console.log(`************************************************ | |
It appears you changed the package.json file but | |
did not npm install afterwards. Please do so and | |
commit the lock file | |
************************************************`); | |
process.exit(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment