Created
February 18, 2021 15:20
-
-
Save jzaefferer/39bd074b5a448cace1e3fe9f7c57e2b4 to your computer and use it in GitHub Desktop.
Automatically check if installed packages are up-to-date, before start and lint scripts, and on `git pull`; MIT License, Copyright Jörn Zaefferer
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 | |
//@ts-check | |
const { execSync } = require('child_process') | |
const mainPackage = require('../package.json') | |
const packageLock = require('../package-lock.json') | |
const outdated = [] | |
Object.keys(mainPackage.dependencies) | |
.concat(Object.keys(mainPackage.devDependencies)) | |
.forEach(packageName => { | |
try { | |
const installedPackage = require(`${packageName}/package.json`) | |
const installedVersion = installedPackage.version | |
const desiredVersion = packageLock.dependencies[packageName].version | |
if (installedVersion !== desiredVersion) { | |
outdated.push( | |
`${packageName}:, installed ${installedVersion}, desired ${desiredVersion}` | |
) | |
} | |
} catch (error) { | |
if (error.code !== 'ERR_PACKAGE_PATH_NOT_EXPORTED') { | |
console.error(error) | |
process.exit(1) | |
} | |
} | |
}) | |
if (outdated.length > 0) { | |
console.log(`some package(s) are outdated, updating`) | |
console.log(outdated.join('\n')) | |
execSync(`npm i --registry=https://registry.npmjs.org/`) | |
} |
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
{ | |
"other": "stuff, | |
"scripts": { | |
"prelint": "node scripts/check-packages.js", | |
"prestart": "node scripts/check-packages.js" | |
}, | |
"husky": { | |
"hooks": { | |
"post-merge": "node scripts/check-packages.js" | |
} | |
}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ah, I see - thanks! I misunderstood the use case.
so basically for yarn it's the same as you and your team did:
Would be cool if we turned this into a module that supports npm, yarn and pnpm probably!