Created
March 17, 2020 09:00
-
-
Save fangbinwei/0fd005481cca2e39d6e9f3ef700c121b to your computer and use it in GitHub Desktop.
run 'npm install' if package.json was modified
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
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