Skip to content

Instantly share code, notes, and snippets.

@melihovv
Created June 15, 2015 19:47
Show Gist options
  • Save melihovv/9e94cb41ff28ecb58d53 to your computer and use it in GitHub Desktop.
Save melihovv/9e94cb41ff28ecb58d53 to your computer and use it in GitHub Desktop.
Pre-commit hook.
#!/usr/bin/env node
"use strict";
var path = require('path');
var execSync = require('child_process').execSync;
var hook = path.basename(__filename);
var config = require(path.resolve(process.cwd(), 'package'));
if (!config[hook]) {
process.exit(0);
}
var output = [];
var error = false;
var errorStatus = '';
config[hook].forEach(function (command) {
var cmd = config.scripts[command];
try {
output.push(execSync(cmd, {encoding: 'utf-8'}));
} catch (e) {
console.log(e.stdout);
console.error(e.stderr);
error = true;
errorStatus = e.status;
}
});
if (error) {
process.exit(errorStatus);
} else {
output.forEach(function(message) {
if (message) {
console.log(message);
}
});
}
@melihovv
Copy link
Author

package.json:

{
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "lint": "jshint ."
  },
  "pre-commit": [
    "lint",
    "test"
  ]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment