b81ed4e6329686f60a8d94efd2f002dbbb1b60dae317f8784bb25098e11ebd064ffd518ea5b1d2f91b9a86eb06a2b902df6664932e1d7b5c62a7d7f2d8673b 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. 'use strict'
  2. const normalize = require('normalize-path')
  3. const stripIndent = require('strip-indent')
  4. const pkg = require('../../package.json')
  5. function platformSpecific() {
  6. // On OS X and Linux, try to use nvm if it's installed
  7. if (process.platform === 'win32') {
  8. // Add
  9. // Node standard installation path /c/Program Files/nodejs
  10. // for GUI apps
  11. // https://github.com/typicode/yorkie/issues/49
  12. return stripIndent(
  13. `
  14. # Node standard installation
  15. export PATH="$PATH:/c/Program Files/nodejs"`
  16. )
  17. } else {
  18. // Using normalize to support ' in path
  19. // https://github.com/typicode/yorkie/issues/117
  20. const home = normalize(process.env.HOME)
  21. return stripIndent(
  22. `
  23. # Add common path where Node can be found
  24. # Brew standard installation path /usr/local/bin
  25. # Node standard installation path /usr/local
  26. export PATH="$PATH:/usr/local/bin:/usr/local"
  27. # Try to load nvm using path of standard installation
  28. load_nvm ${home}/.nvm
  29. run_nvm`
  30. )
  31. return arr.join('\n')
  32. }
  33. }
  34. module.exports = function getHookScript(hookName, relativePath, runnerPath) {
  35. // On Windows normalize path (i.e. convert \ to /)
  36. const normalizedPath = normalize(relativePath)
  37. const noVerifyMessage =
  38. hookName === 'prepare-commit-msg'
  39. ? '(cannot be bypassed with --no-verify due to Git specs)'
  40. : '(add --no-verify to bypass)'
  41. return [
  42. stripIndent(
  43. `
  44. #!/bin/sh
  45. #yorkie ${pkg.version}
  46. command_exists () {
  47. command -v "$1" >/dev/null 2>&1
  48. }
  49. has_hook_script () {
  50. [ -f package.json ] && cat package.json | grep -q "\\"$1\\"[[:space:]]*:"
  51. }
  52. # OS X and Linux only
  53. load_nvm () {
  54. # If nvm is not loaded, load it
  55. command_exists nvm || {
  56. export NVM_DIR="$1"
  57. [ -s "$1/nvm.sh" ] && . "$1/nvm.sh"
  58. }
  59. }
  60. # OS X and Linux only
  61. run_nvm () {
  62. # If nvm has been loaded correctly, use project .nvmrc
  63. command_exists nvm && [ -f .nvmrc ] && nvm use
  64. }
  65. cd "${normalizedPath}"
  66. # Check if ${hookName} is defined, skip if not
  67. has_hook_script ${hookName} || exit 0`
  68. ).trim(),
  69. platformSpecific(),
  70. stripIndent(
  71. `
  72. # Export Git hook params
  73. export GIT_PARAMS="$*"
  74. # Run hook
  75. node "${runnerPath}" ${hookName} || {
  76. echo
  77. echo "${hookName} hook failed ${noVerifyMessage}"
  78. exit 1
  79. }
  80. `
  81. )
  82. ].join('\n')
  83. }