├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── hook-template ├── index.js ├── package.json └── test └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - 0.12 4 | - node 5 | notifications: 6 | email: 7 | on_success: never 8 | on_failure: always 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Maximilian Hoffmann 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | captain-git-hook [![version][1]][2] [![build][3]][4] 2 | ================ 3 | 4 | define git hooks as scripts in your package.json (requires node `>= 0.12`) 5 | 6 | Usage 7 | ----- 8 | 9 | __package.json__ 10 | ```json 11 | { 12 | "scripts": { 13 | "pre-commit": "echo 'this prevents commiting' && exit 1" 14 | } 15 | } 16 | ``` 17 | 18 | Add a script to your `package.json` for any git hook you want to listen to. 19 | 20 | Currently supported hooks: 21 | 22 | ```bash 23 | pre-commit 24 | post-commit 25 | post-merge 26 | pre-push 27 | ``` 28 | 29 | Installation 30 | ------------ 31 | 32 | ```bash 33 | npm i captain-git-hook 34 | ``` 35 | 36 | LICENSE 37 | ------- 38 | 39 | The MIT License (MIT) Maximilian Hoffmann 40 | 41 | [1]: http://img.shields.io/npm/v/captain-git-hook.svg?style=flat 42 | [2]: https://www.npmjs.org/package/captain-git-hook 43 | [3]: http://img.shields.io/travis/maxhoffmann/captain-git-hook.svg?style=flat 44 | [4]: https://travis-ci.org/maxhoffmann/captain-git-hook 45 | -------------------------------------------------------------------------------- /hook-template: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | "use strict"; 3 | var path = require("path"); 4 | var execSync = require("child_process").execSync; 5 | var hook = path.basename(__filename); 6 | var config = require(path.resolve(process.cwd(), "package.json")); 7 | 8 | if (!config.scripts[hook]) { 9 | process.exit(0); 10 | } 11 | 12 | try { 13 | execSync(config.scripts[hook], { 14 | encoding: "utf-8", 15 | stdio: "inherit" 16 | }); 17 | } catch (error) { 18 | process.exit(error.status); 19 | } 20 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | var fs = require('fs'); 3 | var path = require('path'); 4 | var template = fs.readFileSync('./hook-template', 'utf-8'); 5 | 6 | var projectDir = path.resolve(__dirname, '..', '..'); 7 | if (process.env.NODE_ENV === 'testing') { 8 | projectDir = path.join(__dirname, 'test'); 9 | } 10 | var hookDir = path.resolve(projectDir, '.git/hooks/'); 11 | 12 | var hooks = [ 13 | 'pre-commit', 14 | 'post-commit', 15 | 'post-merge', 16 | 'pre-push' 17 | ]; 18 | 19 | hooks.forEach(function(hook) { 20 | installHook(hook); 21 | }); 22 | 23 | function installHook(hook) { 24 | try { 25 | fs.writeFileSync(path.join(hookDir, hook), template, { mode: '755' }); 26 | console.log('installed '+path.join(hookDir, hook)); 27 | } catch(error) { 28 | console.error('installing '+hook+' failed: ', error); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "captain-git-hook", 3 | "version": "2.0.0", 4 | "description": "define git hooks as scripts in your package.json", 5 | "main": "index.js", 6 | "engines": { 7 | "node": ">=0.12" 8 | }, 9 | "engineStrict": true, 10 | "scripts": { 11 | "postinstall": "node ./", 12 | "test": "NODE_ENV=testing tape test/ | tap-dot" 13 | }, 14 | "repository": { 15 | "type": "git", 16 | "url": "ssh://git@github.com:maxhoffmann/captain-git-hook.git" 17 | }, 18 | "keywords": [ 19 | "git", 20 | "hooks", 21 | "hook", 22 | "package.json", 23 | "package" 24 | ], 25 | "author": "Maximilian Hoffmann", 26 | "license": "MIT", 27 | "devDependencies": { 28 | "fs-extra": "^0.16.5", 29 | "tap-dot": "^1.0.0", 30 | "tape": "^3.5.0" 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | var fs = require('fs-extra'); 3 | var path = require('path'); 4 | var test = require('tape'); 5 | 6 | test('creates hooks', function(is) { 7 | is.plan(2); 8 | 9 | fs.removeSync('test/.git'); 10 | fs.ensureDirSync('test/.git/hooks'); 11 | 12 | require('../'); 13 | 14 | var hooks = fs.readdirSync('test/.git/hooks'); 15 | var stat = fs.statSync(path.join(__dirname, '.git/hooks', hooks[0])); 16 | 17 | is.equal(hooks.length, 4); 18 | is.equal(stat.mode, 33261); // 755 19 | }); 20 | --------------------------------------------------------------------------------