install.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. 'use strict'
  2. const fs = require('fs')
  3. const path = require('path')
  4. const findParent = require('./utils/find-parent')
  5. const findHooksDir = require('./utils/find-hooks-dir')
  6. const getHookScript = require('./utils/get-hook-script')
  7. const is = require('./utils/is')
  8. const hooks = require('./hooks.json')
  9. const SKIP = 'SKIP'
  10. const UPDATE = 'UPDATE'
  11. const MIGRATE_GHOOKS = 'MIGRATE_GHOOKS'
  12. const MIGRATE_PRE_COMMIT = 'MIGRATE_PRE_COMMIT'
  13. const CREATE = 'CREATE'
  14. function write(filename, data) {
  15. fs.writeFileSync(filename, data)
  16. fs.chmodSync(filename, parseInt('0755', 8))
  17. }
  18. function createHook(depDir, projectDir, hooksDir, hookName, runnerPath) {
  19. const filename = path.join(hooksDir, hookName)
  20. let packageDir
  21. // prioritize package.json next to .git
  22. // this avoids double-install in lerna monorepos where both root and sub
  23. // package depends on this module
  24. if (fs.existsSync(path.join(projectDir, 'package.json'))) {
  25. packageDir = projectDir
  26. } else {
  27. packageDir = findParent(depDir, 'package.json')
  28. }
  29. // In order to support projects with package.json in a different directory
  30. // than .git, find relative path from project directory to package.json
  31. const relativePath = path.join('.', path.relative(projectDir, packageDir))
  32. const hookScript = getHookScript(hookName, relativePath, runnerPath)
  33. // Create hooks directory if needed
  34. if (!fs.existsSync(hooksDir)) fs.mkdirSync(hooksDir)
  35. if (!fs.existsSync(filename)) {
  36. write(filename, hookScript)
  37. return CREATE
  38. }
  39. if (is.ghooks(filename)) {
  40. write(filename, hookScript)
  41. return MIGRATE_GHOOKS
  42. }
  43. if (is.preCommit(filename)) {
  44. write(filename, hookScript)
  45. return MIGRATE_PRE_COMMIT
  46. }
  47. if (is.huskyOrYorkie(filename)) {
  48. write(filename, hookScript)
  49. return UPDATE
  50. }
  51. return SKIP
  52. }
  53. function installFrom(depDir) {
  54. try {
  55. const isInSubNodeModule = (depDir.match(/node_modules/g) || []).length > 1
  56. if (isInSubNodeModule) {
  57. return console.log(
  58. "trying to install from sub 'node_module' directory,",
  59. 'skipping Git hooks installation'
  60. )
  61. }
  62. const projectDir = findParent(depDir, 'package.json')
  63. const hooksDir = findHooksDir(projectDir)
  64. const runnerPath = './node_modules/yorkie/src/runner.js'
  65. if (hooksDir) {
  66. hooks
  67. .map(function(hookName) {
  68. return {
  69. hookName: hookName,
  70. action: createHook(depDir, projectDir, hooksDir, hookName, runnerPath)
  71. }
  72. })
  73. .forEach(function(item) {
  74. switch (item.action) {
  75. case MIGRATE_GHOOKS:
  76. console.log(`migrating existing ghooks ${item.hookName} script`)
  77. break
  78. case MIGRATE_PRE_COMMIT:
  79. console.log(
  80. `migrating existing pre-commit ${item.hookName} script`
  81. )
  82. break
  83. case UPDATE:
  84. break
  85. case SKIP:
  86. console.log(`skipping ${item.hookName} hook (existing user hook)`)
  87. break
  88. case CREATE:
  89. break
  90. default:
  91. console.error('Unknown action')
  92. }
  93. })
  94. console.log('done\n')
  95. } else {
  96. console.log("can't find .git directory, skipping Git hooks installation")
  97. }
  98. } catch (e) {
  99. console.error(e)
  100. }
  101. }
  102. module.exports = installFrom