git pre-commit hook that lints js code
[fnpeditor.git] / pre-commit.template.js
diff --git a/pre-commit.template.js b/pre-commit.template.js
new file mode 100644 (file)
index 0000000..2f0363b
--- /dev/null
@@ -0,0 +1,22 @@
+/* jshint node: true */
+'use strict';
+
+var exec = require('child_process').exec;
+var fs = require('fs');
+
+exec('git diff --cached --name-status', function (err, stdout) {
+    void(err);
+    var toLint = [];
+    stdout.split('\n').forEach(function(line) {
+        var filePath = line.split('\t')[1];
+        if(filePath && filePath.substr(-3) === '.js' && fs.existsSync(filePath)) {
+            toLint.push(filePath);
+        }
+    });
+    if(toLint.length) {
+        exec('grunt {{task}} {{args}} --jshint-targets=' + toLint.join(','), function (err, stdout) {
+            console.log(stdout);
+            process.exit(err ? -1 : 0);
+        });
+    }
+});