-
Notifications
You must be signed in to change notification settings - Fork 21
/
Gruntfile.js
390 lines (346 loc) · 9.16 KB
/
Gruntfile.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
const fs = require("fs");
const merge = require("deepmerge");
const createWebstore = require("chrome-webstore-upload");
module.exports = function(grunt) {
require("load-grunt-tasks")(grunt);
// exclude the jsx plugin so the JSXTransformer isn't included
const commonModules = [
"jsx",
"common/react",
"common/base"
];
const modulesConfig = {
mainConfigFile: "src/js/require-config.js",
baseUrl: "src/js",
dir: "build/rjs",
skipDirOptimize: true,
optimize: "none",
wrap: false,
inlineText: true,
preserveLicenseComments: true,
verbose: true,
debug: true,
modules: [
{
name: "common/react"
},
{
name: "common/base"
},
{
name: "popup/main",
exclude: commonModules,
// we have to include the require-config file, since it
// maps the names used by ReactVirtualized for React and
// ReactDOM to those used by the rest of the app, and
// the optimizer doesn't include that map by default.
// we only need that for the main module.
include: ["require-config"]
},
{
name: "background/background",
exclude: commonModules
},
{
name: "options/main",
exclude: commonModules
}
],
onBuildWrite: function(
moduleName,
path,
singleContents)
{
// we're inlining the text and not including the text plugin,
// since it's part of jsx, so get rid of the text! and jsx!
// prefixes. otherwise, require will complain about not
// finding the text or jsx modules.
return singleContents.replace(/jsx!|text!/g, "");
}
};
const buildManifestPath = "build/out/manifest.json";
const devPopupPath = "src/popup.html";
const prodPopupPath = "build/prod/popup.html";
const outPopupPath = "build/out/popup.html";
const extensionInfo = {
quickey: {
fullName: "QuicKey – The quick tab switcher",
shortName: "QuicKey",
extensionID: "ldlghkoiihaelfnggonhjnfiabmaficg",
srcManifestPath: "src/manifest.json",
},
qktest: {
fullName: "QK Test",
shortName: "QK Test",
extensionID: "ddbinmabfeaibekibbpbnblmkddlkhpo",
srcManifestPath: "build/qktest-manifest.json",
}
};
const config = {
verbose: true,
clean: {
out: [
"build/out/*",
"release/*"
],
rjs: [
"build/rjs"
]
},
copy: {
crx: {
src: "build/out.crx",
dest: "release/QuicKey.crx"
},
quickey: {
src: extensionInfo.quickey.srcManifestPath,
dest: "build/out/manifest.json"
},
qktest: {
src: extensionInfo.qktest.srcManifestPath,
dest: "build/out/manifest.json"
}
},
sync: {
out: {
files: [
{
cwd: "src/",
dest: "build/out/",
src: [
"css/*.css",
"img/**",
"manifest.json"
]
},
{
cwd: "build/prod/",
dest: "build/out/",
src: ["**"]
}
]
},
build: {
files: [
{
cwd: "build/rjs/",
dest: "build/",
src: [
"build.txt"
]
}
]
}
},
exec: {
pack: {
command: '"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe" ' +
"--pack-extension=C:\\Projects\\Tools\\KeyTab\\keytab-extension\\build\\out " +
'--pack-extension-key="C:\\Projects\\Tools\\KeyTab\\keytab-extension\\QuicKey.pem"',
callback: function() {
grunt.task.run(["copy:crx"]);
}
}
},
compress: {
quickey: {
options: {
archive: "release/QuicKey.zip"
},
files: [
{
expand: true,
cwd: "build/out/",
src: "**"
}
]
},
qktest: {
options: {
archive: "release/QK Test.zip"
},
files: [
{
expand: true,
cwd: "build/out/",
src: "**"
}
]
}
},
confirm: {
publish: {
options: {
question: function()
{
const target = this.args[0] || "quickey";
const manifest = grunt.file.readJSON(extensionInfo[target].srcManifestPath);
return `Publish v${manifest.version} of ${target}? Type "${target}" to continue.\n`;
},
proceed: function(
answer)
{
const target = this.args[0] || "quickey";
return answer === target;
}
}
}
},
terser: {
rjs: {
files: {
"build/out/js/common/error-handler.js": "src/js/lib/error-handler.js",
"build/out/js/common/almond.js": "build/almond.js",
"build/out/js/common/base.js": "build/rjs/common/base.js",
"build/out/js/common/react.js": "build/rjs/common/react.js",
"build/out/js/lib/pinyin.js": "build/rjs/lib/pinyin.js",
"build/out/js/background/background.js": "build/rjs/background/background.js",
"build/out/js/popup/init.js": "src/js/popup/init.js",
"build/out/js/popup/main.js": "build/rjs/popup/main.js",
"build/out/js/options/main.js": "build/rjs/options/main.js"
}
}
},
shell: {
buildPinyin: {
command: "npm run build:pinyin"
}
},
requirejs: {
modulesConfig: {
options: modulesConfig
}
}
};
const rootPattern = /<div id="root">[^<]+(<.+)\s+/m;
const popupPlaceholder = "<!-- rendered html -->";
grunt.initConfig(config);
// the lodash grunt task doesn't seem to work
// node node_modules\lodash-cli\bin\lodash include=remove,escape,dropRightWhile,toPairs,memoize,pull exports=amd
grunt.registerTask("time", function() {
console.log("Started at", new Date().toLocaleString());
});
grunt.registerTask("buildPopup", function() {
const devPopup = grunt.file.read(devPopupPath);
const devBody = devPopup.match(rootPattern)[1];
const outPopup = grunt.file.read(prodPopupPath).replace(popupPlaceholder, devBody);
// write out the built popup template with the latest React code
grunt.file.write(outPopupPath, outPopup);
});
function updateManifest(
path,
...overrides)
{
let manifest = grunt.file.readJSON(path);
manifest = merge.all([manifest, ...overrides]);
grunt.file.write(path, JSON.stringify(manifest, null, "\t"));
return manifest;
}
function incrementVersion(
manifestPath)
{
const manifest = grunt.file.readJSON(manifestPath);
const version = manifest.version.split(".");
let versionString;
version[2] = parseInt(version[2]) + 1;
versionString = version.join(".");
updateManifest(manifestPath, { version: versionString });
return versionString;
}
grunt.registerTask("incrementVersion", function(target = "quickey") {
// set the name back to the full name and icon tooltip that was
// overridden in the cleanupManifest task
const {srcManifestPath} = extensionInfo[target];
const versionString = incrementVersion(buildManifestPath);
updateManifest(srcManifestPath, { version: versionString });
console.log(`Updated ${srcManifestPath} to version ${versionString}.`);
});
grunt.registerTask("cleanupManifest", function(target, env) {
const manifest = grunt.file.readJSON(buildManifestPath);
let {fullName, shortName} = extensionInfo[target];
if (env == "dev") {
fullName = shortName = `${manifest.short_name} BUILD ${new Date().toLocaleString()}`;
}
updateManifest(buildManifestPath,
{
// we don't need the unsafe-eval policy in the built extension
content_security_policy: manifest.content_security_policy.replace("'unsafe-eval' ", ""),
name: fullName,
short_name: shortName,
browser_action: {
default_title: shortName
}
});
});
function publish(
extensionID,
zipStream)
{
const clientInfo = grunt.file.readJSON("keys/client.json");
const refreshInfo = grunt.file.readJSON("keys/refresh-token.json");
const webstore = createWebstore({
extensionId: extensionID,
clientId: clientInfo.installed.client_id,
clientSecret: clientInfo.installed.client_secret,
refreshToken: refreshInfo.refresh_token
});
return webstore.fetchToken()
.then(token => {
console.log(`Fetched webstore token (${token}).`);
return webstore.uploadExisting(zipStream, token)
.then((response) => {
if (response.uploadState == "SUCCESS") {
console.log(`Extension uploaded (${extensionID}).`);
return webstore.publish("default", token)
.then(({statusDetail}) => console.log(`Publish status: ${statusDetail}`));
} else {
console.error(response);
}
});
})
.catch(console.error);
}
grunt.registerTask("upload", function(target = "quickey") {
const done = this.async();
const zipStream = fs.createReadStream(config.compress[target].options.archive);
const extensionID = extensionInfo[target].extensionID;
publish(extensionID, zipStream)
.then(done);
});
grunt.registerTask("build", function(target = "quickey", env = "dev") {
grunt.task.run([
"time",
"clean:rjs",
"sync:out",
`copy:${target}`,
`cleanupManifest:${target}:${env}`,
"buildPopup",
"requirejs",
"terser",
"sync:build",
"clean:rjs"
]);
});
grunt.registerTask("pack", function(target = "quickey", test) {
const tasks = [
"clean:out",
`build:${target}:prod`,
];
// increment the version in the manifest only if this isn't a test
if (test !== "test") {
tasks.push("incrementVersion");
}
tasks.push("compress:" + target);
grunt.task.run(tasks);
});
grunt.registerTask("publish", function(target = "quickey") {
grunt.task.run([
"pack:" + target,
"confirm:publish:" + target,
"upload:" + target
]);
});
grunt.registerTask("default", [
"build"
]);
};