Skip to content

Instantly share code, notes, and snippets.

@qubyte
Last active December 31, 2015 13:59
Show Gist options
  • Save qubyte/7997483 to your computer and use it in GitHub Desktop.
Save qubyte/7997483 to your computer and use it in GitHub Desktop.
A script to install git hooks. Run these as the `postinstall` script in package.json. The example here writes a pre-commit hook that runs `npm test`. By having this as a self-contained script and not as a Grunt or Gulp task, Gulp or Grunt can be strict devDependencies.
/* This script operates asynchronously. It can be written synchronously, but
* not a lot is saved once you try-catch everything. Uses the async module.
*
* MIT licence.
*/
var fs = require('fs');
var path = require('path');
var async = require('async');
// Write as many of these as you like.
function writePreCommit(callback) {
var filename = path.join(__dirname, '.git', 'hooks', 'pre-commit');
var content = 'npm test';
fs.writeFile(filename, content, function (err) {
if (err) {
return callback(err);
}
fs.chmod(filename, '755', callback);
});
}
// Stack the write functions up in the parallel.
function writeHooks(callback) {
async.parallel([
writePreCommit
], callback);
}
// Check that the git hooks directory exists. If it does, write the hooks and
// exit.
fs.stat(path.join(__dirname, '.git', 'hooks'), function (err, stat) {
if (err || !stat.isDirectory()) {
console.log('No git directory found. Hooks will not be installed.');
return process.exit(0);
}
writeHooks(function (err) {
if (err) {
console.error(err);
return process.exit(1);
}
console.log('Git hooks installed');
process.exit(0);
});
});
/* This script operates asynchronously. It can be written synchronously, but
* not a lot is saved once you try-catch everything. Uses the co module. On
* node 0.11.2+ with the --harmony flag generators are available. This
* version uses Co, which requires generators.
*
* MIT licence.
*/
var path = require('path');
var co = require('co');
var fs = require('co-fs');
// Write the pre-commit script.
function* writePreCommit() {
'use strict';
var filename = path.join(__dirname, '.git', 'hooks', 'pre-commit');
var content = 'npm test';
var write = yield fs.writeFile(filename, content);
var chmod = yield fs.chmod(filename, '755');
return [write, chmod];
}
// Write the hook scripts in parallel.
function* writeHooks() {
'use strict';
// Stack more hook writers in here to write them in parallel.
return yield [
co(writePreCommit)
];
}
// Check that the hooks directory exists and write the hooks.
function* execute() {
'use strict';
var stat = yield fs.stat(path.join(__dirname, '.git', 'hooks'));
if (!stat.isDirectory()) {
console.log('No git directory found. Hooks will not be installed.');
return process.exit(0);
}
var hooks = yield co(writeHooks);
return [stat, hooks];
}
// Execuse the writehoos.
co(execute)(function (err) {
'use strict';
if (err) {
console.error(err);
return process.exit(1);
}
console.log('Git hooks installed');
process.exit(0);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment