Version 1
[yaffs-website] / web / themes / contrib / bootstrap / .githooks.js.hbs
1 var exec = require('sync-exec');
2 var exit = 0;
3 var glob = require("glob");
4 var globOptions = {
5   ignore: [
6     'bower_components/**/*',
7     'node_modules/**/*'
8   ]
9 };
10
11 var pkg = require('../../package');
12 // Change to \{{ hook }} once that variable is available.
13 // @see https://github.com/wecodemore/grunt-githooks/pull/40
14 var hooks = pkg.githooks && pkg.githooks['{{ task }}'];
15 if (hooks) {
16   var ret, hook, files, commands, staged, matchAll;
17   var filesMatched = [];
18
19   // Iterate over all hook definitions.
20   for (var h in hooks) {
21     hook = hooks[h];
22     commands = hook.commands || [];
23     staged = hook.staged === void 0 ? false : !!hook.staged;
24     matchAll = hook.matchAll === void 0 ? true : !!hook.matchAll;
25
26     // Iterate over all files in a hook definition.
27     if (hook.files) {
28       // Expand all file paths using glob (for pattern matching).
29       if (typeof hook.files === 'string') {
30         files = glob.sync(hook.files, globOptions) || [];
31       }
32       if (Array.isArray(hook.files)) {
33         files = [];
34         for (var f = 0; f < hook.files.length; f++) {
35           files = [].concat(files, glob.sync(hook.files[f], globOptions) || []);
36         }
37       }
38
39       // All files must either be staged or modified for the entire
40       // hook definition command(s) to be executed.
41       for (f in files) {
42         var file = files[f];
43         if (file) {
44           // Only continue if file has been staged or modified.
45           ret = exec((staged ? 'git diff --name-only --cached ' + file : 'git diff HEAD@{1} --stat -- ' + file));
46           exit = ret.status;
47           if (exit === 0 && ret.stdout !== '') {
48             filesMatched.push(file);
49           }
50           else if (matchAll && (exit > 0 || ret.stdout === '')) {
51             console.log(ret.stdout);
52             filesMatched = [];
53             break;
54           }
55         }
56       }
57     }
58
59     // Iterate over all commands that should be executed for matched files.
60     if (filesMatched.length) {
61       if (typeof commands === 'string') {
62         commands = [commands];
63       }
64       for (var c in commands) {
65         var command = commands[c];
66         console.log("\033[4;35mgit: {{ task }}\033[0;35m\n>> " + command + "\033[0m\n");
67         ret = exec(command);
68         console.log(ret.stdout);
69         exit = ret.status;
70         if (exit > 0) {
71           break;
72         }
73       }
74     }
75
76     // Display any errors.
77     if (exit > 0 && ret && ret.stderr) {
78       console.log(ret.stderr);
79     }
80   }
81 }
82
83 process.exit(exit);