|
| 1 | +var util = require('util'); |
| 2 | +var path = require('path'); |
| 3 | +var spawn = require('child_process').spawn; |
| 4 | +var vow = require('vow'); |
| 5 | +var vowFs = require('vow-fs'); |
| 6 | +var config = require('../package.json').gitHooks; |
| 7 | + |
| 8 | +module.exports = { |
| 9 | + /** |
| 10 | + * Installs git hooks. |
| 11 | + */ |
| 12 | + install: function (currentPath) { |
| 13 | + return getClosestPath(config.dirs.root, currentPath).then(function (gitPath) { |
| 14 | + var hooksPath = path.resolve(gitPath, config.dirs.hooks); |
| 15 | + var hooksOldPath = path.resolve(gitPath, config.dirs.hooksOld); |
| 16 | + |
| 17 | + return vow.all([ |
| 18 | + vowFs.exists(hooksPath), |
| 19 | + vowFs.exists(hooksOldPath) |
| 20 | + ]) |
| 21 | + .spread(function (isHooksExist, isHooksOldExist) { |
| 22 | + if (isHooksOldExist) { |
| 23 | + throw new Error('git-hooks already installed'); |
| 24 | + } |
| 25 | + |
| 26 | + if (isHooksExist) { |
| 27 | + return vowFs.move(hooksPath, hooksOldPath); |
| 28 | + } |
| 29 | + }) |
| 30 | + .then(function () { |
| 31 | + return vowFs.makeDir(hooksPath); |
| 32 | + }) |
| 33 | + .then(function () { |
| 34 | + return vow.all( |
| 35 | + config.hooks.map(function (hookName) { |
| 36 | + var hookPath = path.resolve(hooksPath, hookName); |
| 37 | + return vowFs.write(hookPath, config.template.join('\n'), {mode: '0777'}); |
| 38 | + }) |
| 39 | + ); |
| 40 | + }); |
| 41 | + }); |
| 42 | + }, |
| 43 | + |
| 44 | + /** |
| 45 | + * Uninstall git hooks. |
| 46 | + */ |
| 47 | + uninstall: function (currentPath) { |
| 48 | + return getClosestPath(config.dirs.root, currentPath).then(function (gitPath) { |
| 49 | + var hooksPath = path.resolve(gitPath, config.dirs.hooks); |
| 50 | + var hooksOldPath = path.resolve(gitPath, config.dirs.hooksOld); |
| 51 | + |
| 52 | + return vow.all([ |
| 53 | + vowFs.exists(hooksPath), |
| 54 | + vowFs.exists(hooksOldPath) |
| 55 | + ]) |
| 56 | + .spread(function (isHooksExist, isHooksOldExist) { |
| 57 | + if (!isHooksExist) { |
| 58 | + throw new Error('git-hooks is not installed'); |
| 59 | + } |
| 60 | + |
| 61 | + return vowFs.removeDir(hooksPath).then(function () { |
| 62 | + if (isHooksOldExist) { |
| 63 | + return vowFs.move(hooksOldPath, hooksPath); |
| 64 | + } |
| 65 | + }); |
| 66 | + }); |
| 67 | + }); |
| 68 | + }, |
| 69 | + |
| 70 | + /** |
| 71 | + * Runs a git hook. |
| 72 | + * |
| 73 | + * @param {String} filename Path to git hook. |
| 74 | + * @param {String} [arg] Git hook argument. |
| 75 | + */ |
| 76 | + run: function (filename, arg) { |
| 77 | + var hookName = path.basename(filename); |
| 78 | + var hooksDirname = path.resolve(path.dirname(filename), '../../.githooks', hookName); |
| 79 | + |
| 80 | + return vowFs.exists(hooksDirname).then(function (isExists) { |
| 81 | + if (isExists) { |
| 82 | + return vowFs.listDir(hooksDirname).then(function (list) { |
| 83 | + var hooks = list.map(function (hookName) { |
| 84 | + return path.resolve(hooksDirname, hookName); |
| 85 | + }); |
| 86 | + return runHooks(hooks, [arg]); |
| 87 | + }); |
| 88 | + } |
| 89 | + }); |
| 90 | + } |
| 91 | +}; |
| 92 | + |
| 93 | +/** |
| 94 | + * Runs hooks. |
| 95 | + * |
| 96 | + * @param {String[]} hooks List of hook names to execute. |
| 97 | + * @param {String[]} args |
| 98 | + * @param {vow.Deferred} [defer] |
| 99 | + */ |
| 100 | +function runHooks(hooks, args, defer) { |
| 101 | + defer = defer || vow.defer(); |
| 102 | + if (!hooks.length) { |
| 103 | + return defer.resolve(0); |
| 104 | + } |
| 105 | + |
| 106 | + var hook = spawn(hooks.shift(), args, {stdio: 'inherit'}); |
| 107 | + hook.on('close', function (code) { |
| 108 | + if (code === 0) { |
| 109 | + runHooks(hooks, args, defer); |
| 110 | + } else { |
| 111 | + defer.reject(code); |
| 112 | + } |
| 113 | + }); |
| 114 | + return defer.promise(); |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * Returns the closest directory. |
| 119 | + * It starts looking from the current directory and does it up to the fs root. |
| 120 | + * It returns undefined in case where the specified directory isn't found. |
| 121 | + * |
| 122 | + * @param {String} dirname Directory name to look for. |
| 123 | + * @param {String} [currentPath] Current started path to search. |
| 124 | + * @returns {String|undefined} |
| 125 | + */ |
| 126 | +function getClosestPath(dirname, currentPath) { |
| 127 | + currentPath = currentPath || __dirname; |
| 128 | + |
| 129 | + // reaches ths fs root |
| 130 | + if (currentPath === '/') { |
| 131 | + return vow.reject(new Error(util.format('Directory %s is not found', dirname))); |
| 132 | + } |
| 133 | + |
| 134 | + var dirnamePath = path.join(currentPath, dirname); |
| 135 | + return vowFs.exists(dirnamePath).then(function (isExists) { |
| 136 | + return isExists ? |
| 137 | + dirnamePath : |
| 138 | + getClosestPath(dirname, path.resolve(currentPath, '..')); |
| 139 | + }); |
| 140 | +} |
0 commit comments