Skip to content

Commit d3b146b

Browse files
committed
Issue #2348843: Add Grunt for project maintainer workflow
Added post-checkout and post-merge watches for package.json and Gruntfile.js
1 parent ea49e45 commit d3b146b

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

.githooks.js.hbs

+13-8
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,21 @@ var pkg = require('../../package');
1313
// @see https://github.com/wecodemore/grunt-githooks/pull/40
1414
var hooks = pkg.githooks && pkg.githooks['{{ task }}'];
1515
if (hooks) {
16-
var ret, hook, files, commands, staged;
16+
var ret, hook, files, commands, staged, matchAll;
17+
var filesMatched = [];
1718

1819
// Iterate over all hook definitions.
1920
for (var h in hooks) {
2021
hook = hooks[h];
21-
commands = hook.commands;
22-
staged = hook.staged;
22+
commands = hook.commands || [];
23+
staged = hook.staged === void 0 ? false : !!hook.staged;
24+
matchAll = hook.matchAll === void 0 ? true : !!hook.matchAll;
2325

2426
// Iterate over all files in a hook definition.
2527
if (hook.files) {
2628
// Expand all file paths using glob (for pattern matching).
2729
if (typeof hook.files === 'string') {
28-
files = glob.sync(hook.files[f], globOptions) || [];
30+
files = glob.sync(hook.files, globOptions) || [];
2931
}
3032
if (Array.isArray(hook.files)) {
3133
files = [];
@@ -42,17 +44,20 @@ if (hooks) {
4244
// Only continue if file has been staged or modified.
4345
ret = exec((staged ? 'git diff --name-only --cached ' + file : 'git diff HEAD@{1} --stat -- ' + file));
4446
exit = ret.status;
45-
if (exit > 0 || ret.stdout === '') {
47+
if (exit === 0 && ret.stdout !== '') {
48+
filesMatched.push(file);
49+
}
50+
else if (matchAll && (exit > 0 || ret.stdout === '')) {
4651
console.log(ret.stdout);
47-
commands = false;
52+
filesMatched = [];
4853
break;
4954
}
5055
}
5156
}
5257
}
5358

54-
// Iterate over all commands that should be executed.
55-
if (commands) {
59+
// Iterate over all commands that should be executed for matched files.
60+
if (filesMatched.length) {
5661
if (typeof commands === 'string') {
5762
commands = [commands];
5863
}

package.json

+15-3
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,23 @@
1717
"postinstall": "grunt install"
1818
},
1919
"githooks": {
20+
"post-checkout": [
21+
{
22+
"files": "*(package.json|Gruntfile.js)",
23+
"commands": "npm install",
24+
"matchAll": false
25+
}
26+
],
27+
"post-merge": [
28+
{
29+
"files": "*(package.json|Gruntfile.js)",
30+
"commands": "npm install",
31+
"matchAll": false
32+
}
33+
],
2034
"pre-commit": [
2135
{
22-
"files": [
23-
"starterkits/less/less/overrides.less"
24-
],
36+
"files": "starterkits/less/less/overrides.less",
2537
"commands": [
2638
"grunt compile",
2739
"git add css/overrides.css"

0 commit comments

Comments
 (0)