Created
November 15, 2016 15:15
-
-
Save cef62/f12972792053c5f4321d36041327f6ad to your computer and use it in GitHub Desktop.
Run npm install after post-merge hook only when package.json changes. Javascript only.
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 { exec } = require('./helpers') | |
const changedFile = (filename, partial) => exec( | |
`git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD`, | |
`Something went wrong reading last git-pulled files list.`, | |
true | |
).then((stdout) => stdout.split('\n') | |
.some((name) => { | |
let match | |
if (partial) { | |
match = name.endsWith(filename) | |
} else { | |
match = name === filename | |
} | |
return match | |
}) | |
) | |
const checkRun = (filename, command) => | |
changedFile(filename) | |
.then((changed) => changed && exec( | |
command, | |
`Something went wrong executing: "${command}"` | |
)) | |
module.exports = { | |
changedFile, | |
checkRun, | |
} |
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 sh = require('shelljs') | |
const Promise = require('bluebird') | |
const exec = (code, errorMsg = 'Something went wrong', silent = false) => | |
new Promise((resolve, reject) => { | |
sh.exec(code, { silent }, (errorCode, stdout, stderr) => { | |
if (errorCode) { | |
reject(stderr || errorMsg) | |
} | |
resolve(stdout) | |
}) | |
}) | |
module.exports = { exec } |
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 | |
const { echo, exit } = require('shelljs') | |
const { checkRun } = require('./git-utils') | |
const packageJSON = 'package.json' | |
const defaultCmd = 'npm install' | |
const { | |
filename = packageJSON, | |
cmd = defaultCmd, | |
} = require('yargs').option('f', { | |
alias: 'filename', | |
requiresArg: true, | |
describe: 'Complete filename with path (relative to git repo) to check.', | |
type: 'string' | |
}) | |
.option('c', { | |
alias: 'cmd', | |
requiresArg: true, | |
describe: 'Command to be executed if the given file is changed', | |
type: 'string' | |
}).argv | |
const action = () => checkRun(filename, cmd) | |
action() | |
.then(() => exit(0)) | |
.catch((err) => { | |
echo(err) | |
exit(1) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment