Skip to content

Instantly share code, notes, and snippets.

@fangbinwei
Created March 17, 2020 09:00
Show Gist options
  • Save fangbinwei/0fd005481cca2e39d6e9f3ef700c121b to your computer and use it in GitHub Desktop.
Save fangbinwei/0fd005481cca2e39d6e9f3ef700c121b to your computer and use it in GitHub Desktop.
run 'npm install' if package.json was modified
const util = require('util')
const childProcess = require('child_process')
const exec = util.promisify(childProcess.exec)
async function main() {
try {
const { stdout: changeFiles } = await exec(
'git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD'
)
if (changeFiles.includes('package.json')) {
console.log(`# package.json update, so run 'npm install' #\n`)
await runNpmInstall()
}
} catch (err) {
console.error(`# Failed to get diff info/ run 'npm install' `)
console.error(`# error message:
--------------------->
${err.message}
<--------------------`)
process.exit()
}
}
function runNpmInstall() {
return new Promise((resolve, reject) => {
const npmInstall = childProcess.spawn(
process.platform === 'win32' ? 'npm.cmd' : 'npm',
['install'],
{
stdio: 'inherit'
}
)
npmInstall.on('exit', (code, signal) => {
resolve()
})
npmInstall.on('error', err => {
reject(err)
})
})
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment