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