-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
139 lines (116 loc) · 4.06 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
'use script';
let globalHooker;
const exit = require('exit');
const VALID_TASK_TYPE = ['string', 'array', 'function'];
const hookFunction = function(grunt) {
if (globalHooker) return globalHooker;
const hookMap = {};
const runTask = grunt.task.run;
const hooker = {
_run(tasks) {
if (!tasks || !tasks.length) return;
return runTask.apply(grunt.task, arguments);
},
_hook(where, taskName, description, task) {
if (!task) return;
if (!hookMap[taskName]) hookMap[taskName] = { pre: [], post: [], replace: [] };
const hookTaskList = hookMap[taskName][where];
const hookTaskName = `$hook_${where}_${taskName}_${hookTaskList.length}`;
hookTaskList.push(hookTaskName);
grunt.registerTask(hookTaskName, description, task);
},
_unhook(taskName, where) {
const hookTask = hookMap[taskName];
const tasks = grunt.task._tasks;
hookTask[where].forEach(function(task) { delete tasks[task]; });
hookTask[where] = [];
},
hook(taskName, description, task, where) {
const descType = grunt.util.kindOf(description);
const taskNameType = grunt.util.kindOf(taskName);
let taskType = grunt.util.kindOf(task);
if (
(descType !== 'string' && description) || // hook('clean', []) hook('clean', function() {}, true)
(descType === 'string' && description && (!task || taskType === 'boolean')) // hook('build', 'clean') hook('build', 'clean', true)
) {
where = task;
task = description;
description = null;
taskType = grunt.util.kindOf(task);
}
if (
!task ||
VALID_TASK_TYPE.indexOf(taskType) < 0 ||
(taskType === 'array' && !task.length)
) return;
const whereType = grunt.util.kindOf(where);
if (!whereType) {
where = 'pre';
} else if (whereType === 'boolean') {
where = where ? 'post' : 'pre';
}
if (taskNameType === 'array') {
taskName.forEach((tsn) => this._hook(where, tsn, description, task));
} else {
this._hook(where, taskName, description, task);
}
},
unhook(taskName, where) {
if (grunt.util.kindOf(taskName) === 'array') {
taskName.forEach((tsn) => this.unhook(tsn, where));
} else {
const hookTask = hookMap[taskName];
if (!hookTask) return;
if (where === 'pre' || !where || where === 'both') this._unhook(taskName, 'pre');
if (where === 'post' || !where || where === 'both') this._unhook(taskName, 'post');
if (where === 'replace' || !where || where === 'both') this._unhook(taskName, 'replace');
if (!hookTask.pre.length && !hookTask.post.length && !hookTask.replace.length) delete hookMap[taskName];
}
}
};
// override original function
grunt.task.run = function(taskName) {
if (grunt.util.kindOf(taskName) === 'array') {
taskName.forEach(function(task) {
grunt.task.run(task);
});
} else {
const hookTask = hookMap[taskName];
if (hookTask && taskName) {
// pre
hooker._run(hookTask.pre);
if (
hookTask.replace &&
hookTask.replace.length
) {
// replace
hooker._run(hookTask.replace.slice(hookTask.replace.length - 1));
} else {
// original
runTask.apply(this, arguments);
}
// post
hooker._run(hookTask.post);
} else {
return runTask.apply(this, arguments);
}
}
}
// add grunt method
grunt.hookTask = grunt.task.hookTask = hooker.hook.bind(hooker);
grunt.unhookTask = grunt.task.unhookTask = hooker.unhook.bind(hooker);
return (globalHooker = hooker);
};
Object.defineProperty(hookFunction, 'hook', {
get: function() {
console.error('Error: Use hooker like "require(\'grunt-task-hooker\')(grunt)"'.red);
exit(0);
}
});
Object.defineProperty(hookFunction, 'unhook', {
get: function() {
console.error('Error: Use hooker like "require(\'grunt-task-hooker\')(grunt)"'.red);
exit(0);
}
});
module.exports = hookFunction;