yorkie
Version:
githooks management forked from husky
101 lines (83 loc) • 2.54 kB
JavaScript
'use strict'
const normalize = require('normalize-path')
const stripIndent = require('strip-indent')
const pkg = require('../../package.json')
function platformSpecific() {
// On OS X and Linux, try to use nvm if it's installed
if (process.platform === 'win32') {
// Add
// Node standard installation path /c/Program Files/nodejs
// for GUI apps
// https://github.com/typicode/yorkie/issues/49
return stripIndent(
`
export PATH="$PATH:/c/Program Files/nodejs"`
)
} else {
// Using normalize to support ' in path
// https://github.com/typicode/yorkie/issues/117
const home = normalize(process.env.HOME)
return stripIndent(
`
export PATH="$PATH:/usr/local/bin:/usr/local"
load_nvm ${home}/.nvm
run_nvm`
)
return arr.join('\n')
}
}
module.exports = function getHookScript(hookName, relativePath, runnerPath) {
// On Windows normalize path (i.e. convert \ to /)
const normalizedPath = normalize(relativePath)
const noVerifyMessage =
hookName === 'prepare-commit-msg'
? '(cannot be bypassed with --no-verify due to Git specs)'
: '(add --no-verify to bypass)'
return [
stripIndent(
`
command_exists () {
command -v "$1" >/dev/null 2>&1
}
has_hook_script () {
[ -f package.json ] && cat package.json | grep -q "\\"$1\\"[[:space:]]*:"
}
load_nvm () {
command_exists nvm || {
export NVM_DIR="$1"
[ -s "$1/nvm.sh" ] && . "$1/nvm.sh"
}
}
run_nvm () {
command_exists nvm && [ -f .nvmrc ] && nvm use
}
cd "${normalizedPath}"
has_hook_script ${hookName} || exit 0`
).trim(),
platformSpecific(),
stripIndent(
`
export GIT_PARAMS="$*"
node "${runnerPath}" ${hookName} || {
echo
echo "${hookName} hook failed ${noVerifyMessage}"
exit 1
}
`
)
].join('\n')
}