/* 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); }); });