1 | var fs = require('fs'),
|
2 | path = require('path'),
|
3 | handlebars = require('handlebars');
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | function Hook(hookName, taskNames, options) {
|
12 |
|
13 | |
14 |
|
15 |
|
16 |
|
17 |
|
18 | this.hookName = hookName;
|
19 |
|
20 | |
21 |
|
22 |
|
23 |
|
24 |
|
25 | this.taskNames = taskNames;
|
26 |
|
27 | |
28 |
|
29 |
|
30 |
|
31 |
|
32 | this.options = options;
|
33 |
|
34 | this.markerRegExp = new RegExp(this.options.startMarker.replace(/\//g, '\\/') +
|
35 | '[\\s\\S]*' +
|
36 | this.options.endMarker.replace(/\//g, '\\/'), 'm');
|
37 | }
|
38 |
|
39 | Hook.prototype = {
|
40 |
|
41 | |
42 |
|
43 |
|
44 |
|
45 | create: function() {
|
46 |
|
47 | var hookPath = path.resolve(this.options.dest, this.hookName);
|
48 |
|
49 | var existingCode = this.getHookContent(hookPath);
|
50 |
|
51 | if (existingCode) {
|
52 | this.validateScriptLanguage(existingCode);
|
53 | }
|
54 |
|
55 | var hookContent;
|
56 | if(this.hasMarkers(existingCode)) {
|
57 | hookContent = this.insertBindingCode(existingCode);
|
58 | } else {
|
59 | hookContent = this.appendBindingCode(existingCode);
|
60 | }
|
61 |
|
62 | fs.writeFileSync(hookPath, hookContent);
|
63 |
|
64 | fs.chmodSync(hookPath, '755');
|
65 | },
|
66 |
|
67 | |
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | getHookContent: function (path) {
|
74 |
|
75 | if (fs.existsSync(path)) {
|
76 | return fs.readFileSync(path, {encoding: 'utf-8'});
|
77 | }
|
78 | },
|
79 |
|
80 | |
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | validateScriptLanguage: function (script) {
|
88 |
|
89 | if (!this.isValidScriptLanguage(script, this.options.hashbang)) {
|
90 | throw new Error('ERR_INVALID_SCRIPT_LANGUAGE');
|
91 | }
|
92 | },
|
93 |
|
94 | |
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 | isValidScriptLanguage: function(hookContent, hashbang) {
|
103 |
|
104 | var firstLine = hookContent.split('\n')[0];
|
105 | return firstLine.indexOf(hashbang) !== -1;
|
106 | },
|
107 |
|
108 | |
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 | hasMarkers: function(existingCode) {
|
115 |
|
116 | return this.markerRegExp.test(existingCode);
|
117 | },
|
118 |
|
119 | |
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | createBindingCode: function () {
|
126 |
|
127 | var template = this.loadTemplate(this.options.template);
|
128 | var bindingCode = template({
|
129 | task: this.taskNames,
|
130 | preventExit: this.options.preventExit,
|
131 | args: this.options.args,
|
132 | gruntfileDirectory: process.cwd()
|
133 | });
|
134 |
|
135 | return this.options.startMarker + '\n' + bindingCode + '\n' + this.options.endMarker;
|
136 | },
|
137 |
|
138 | |
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 | loadTemplate: function () {
|
145 |
|
146 | var template = fs.readFileSync(this.options.template, {
|
147 | encoding: 'utf-8'
|
148 | });
|
149 |
|
150 | return handlebars.compile(template);
|
151 | },
|
152 |
|
153 | |
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 | appendBindingCode: function (existingCode) {
|
160 |
|
161 | var bindingCode = this.createBindingCode();
|
162 | return (existingCode || this.options.hashbang) +
|
163 | '\n\n' +
|
164 | bindingCode;
|
165 | },
|
166 |
|
167 | |
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 | insertBindingCode: function (existingCode) {
|
174 |
|
175 | var bindingCode = this.createBindingCode();
|
176 | return existingCode.replace(this.markerRegExp, bindingCode);
|
177 | },
|
178 | };
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 | Hook.HOOK_NAMES = [
|
187 |
|
188 | 'applypatch-msg',
|
189 | 'pre-applypatch',
|
190 | 'post-applypatch',
|
191 | 'pre-commit',
|
192 | 'prepare-commit-msg',
|
193 | 'commit-msg',
|
194 | 'post-commit',
|
195 | 'pre-rebase',
|
196 | 'post-checkout',
|
197 | 'post-merge',
|
198 | 'post-rewrite',
|
199 |
|
200 |
|
201 | 'pre-receive',
|
202 | 'update',
|
203 | 'post-receive',
|
204 | 'post-update',
|
205 | 'pre-auto-gc'
|
206 | ];
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 | Hook.isNameOfAGitHook = function(name) {
|
215 | return Hook.HOOK_NAMES.indexOf(name) !== -1;
|
216 | };
|
217 |
|
218 | module.exports.Hook = Hook;
|