| 78 | } |
| 79 | |
| 80 | function setupHook(hookName, install) { |
| 81 | var hook = path.resolve(hooks, hookName); |
| 82 | var hookFile = path.relative(hooks, "./bin/"+hookName); |
| 83 | var hookExists = fs.existsSync(hook); |
| 84 | var _isOtherHook; |
| 85 | function isOtherHook() { |
| 86 | if (typeof(_isOtherHook) == 'undefined') { |
| 87 | var content = fs.readFileSync(hook, { encoding: 'utf8' }); |
| 88 | _isOtherHook = !/clns\/node-commit-msg/.test(content); |
| 89 | } |
| 90 | return _isOtherHook; |
| 91 | } |
| 92 | |
| 93 | // Uninstall hook |
| 94 | |
| 95 | if (uninstall) { |
| 96 | if (hookExists && !isOtherHook()) { |
| 97 | try { |
| 98 | fs.unlinkSync(hook); |
| 99 | console.log('Uninstall hook at ' + hook); |
| 100 | } catch(e) { |
| 101 | throw e; |
| 102 | } |
| 103 | } |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | // Install hook |
| 108 | |
| 109 | if (install) { |
| 110 | if (hookExists && isOtherHook()) { |
| 111 | console.error('A hook already exists at %s.\n' + |
| 112 | 'Remove it and re-install the module again to install the hook.', |
| 113 | hook); |
| 114 | } else if (!hookExists) { |
| 115 | try { |
| 116 | fs.symlinkSync(hookFile, hook); |
| 117 | console.log('Install hook at ' + hook); |
| 118 | } catch(e) { |
| 119 | throw e; |
| 120 | } |
| 121 | } |
| 122 | } else { |
| 123 | console.log('Skip \'%s\' hook', hookName); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | setupHook('commit-msg', !noClientHook); |
| 128 | setupHook('update', !noServerHook); |