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