Skip to content

Instantly share code, notes, and snippets.

@cef62
Created November 15, 2016 15:15
Show Gist options
  • Save cef62/f12972792053c5f4321d36041327f6ad to your computer and use it in GitHub Desktop.
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.
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,
}
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 }
#!/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