forked from kee-org/browser-addon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
520 lines (469 loc) · 18.8 KB
/
gulpfile.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
var gulp = require("gulp");
var tslint = require("gulp-tslint");
var ts = require("gulp-typescript");
var fs = require("fs");
var sourcemaps = require('gulp-sourcemaps');
var zip = require('gulp-zip');
var merge = require('merge-stream');
var sequence = require('run-sequence');
var del = require('del');
var replace = require('gulp-replace');
var signAddon = require('sign-addon').default;
// Some tasks set DEBUG to false so that a production build can be executed.
// There doesn't appear to be a way to pass this as a local variable so we
// have to ensure every invocation of gulp only builds in either production
// or debug mode but not both (tasks run concurrently)
var DEBUG = true;
const buildDirDebug = "build/debug/all";
const buildDirProd = "build/prod/all";
const buildDirDebugFirefox = "build/debug/firefox";
const buildDirProdFirefox = "build/prod/firefox";
const buildDirDebugChrome = "build/debug/chrome";
const buildDirProdChrome = "build/prod/chrome";
const globStaticManifest = 'manifest.json';
const globStaticLocales = '_locales/**';
const globStaticReleaseNotes = 'release-notes/*.{js,css,html,map}';
const globStaticCommonFonts = 'common/fonts/**';
const globStaticCommonImages = 'common/images/**';
const globStaticCommon = 'common/*.{css,html}';
const globStaticLib = 'lib/**/*.*';
const globStaticDialogs = 'dialogs/*.{css,html}';
const globStaticSettings = 'settings/*.{css,html}';
const globStaticBackground = 'background/*.{css,html}';
const globStaticPage = 'page/*.{css,html}';
const globStaticPanels = 'panels/*.{css,html}';
const globStaticPopup = 'popup/*.{css,html}';
const globTSCommon = 'common/**/*.ts';
const globTSCommonOut = 'common.js';
const globTSPopup = 'popup/**/*.ts';
const globTSPopupOut = 'popup.js';
const globTSPanels = 'panels/**/*.ts';
const globTSPanelsOut = 'panels.js';
const globTSPage = 'page/**/*.ts';
const globTSPageOut = 'page.js';
const globTSBackground = 'background/**/*.ts';
const globTSBackgroundOut = 'app.js';
const globTSSettings = 'settings/**/*.ts';
const globTSSettingsOut = 'settings.js';
const globTSDialogs = 'dialogs/**/*.ts';
const globTSDialogsOut = 'dialogs/**/*.js';
/********** TOP-LEVEL ORCHESTRATION **********/
gulp.task('default', ['build:debug']);
gulp.task('build', ['build:debug']);
gulp.task('build:debug', function (done) {
sequence(['compilets', 'static'], done);
});
gulp.task('build:prod', function (done) {
DEBUG = false;
sequence(['compilets', 'static'], done);
});
gulp.task("compilets", function (done) {
sequence("compilets:common", ["compilets:background", "compilets:popup",
"compilets:panels", "compilets:page", "compilets:settings",
"compilets:dialogs"], done);
});
/********** LINTING TYPESCRIPT **********/
gulp.task("lint:ts", ["lint:background", "lint:popup", "lint:panels", "lint:page", "lint:settings", "lint:dialogs" ]);
gulp.task("lint:background", function() {
return gulp.src([globTSBackground])
.pipe(tslint({
formatter: "verbose"
}))
.pipe(tslint.report())
});
gulp.task("lint:page", function() {
return gulp.src([globTSPage])
.pipe(tslint({
formatter: "verbose"
}))
.pipe(tslint.report())
});
gulp.task("lint:popup", function() {
return gulp.src([globTSPopup])
.pipe(tslint({
formatter: "verbose"
}))
.pipe(tslint.report())
});
gulp.task("lint:panels", function() {
return gulp.src([globTSPanels])
.pipe(tslint({
formatter: "verbose"
}))
.pipe(tslint.report())
});
gulp.task("lint:settings", function() {
return gulp.src([globTSSettings])
.pipe(tslint({
formatter: "verbose"
}))
.pipe(tslint.report())
});
gulp.task("lint:dialogs", function() {
return gulp.src([globTSDialogs])
.pipe(tslint({
formatter: "verbose"
}))
.pipe(tslint.report())
});
gulp.task("lint:common", function() {
return gulp.src([globTSCommon])
.pipe(tslint({
formatter: "verbose"
}))
.pipe(tslint.report())
});
/********** COMPILING SHARED TYPESCRIPT LIBRARY **********/
var tsProjectCommon = ts.createProject("common/tsconfig.json", {
outFile: globTSCommonOut,
declaration: true
});
gulp.task("compilets:common", ["clean:ts:common", "lint:common"], function() {
if (DEBUG) {
var tsResult = tsProjectCommon.src()
.pipe(sourcemaps.init())
.pipe(tsProjectCommon());
return merge(
tsResult.dts.pipe(gulp.dest('typedefs')),
tsResult.js.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(buildDirDebug + '/common'))
.pipe(gulp.dest(buildDirDebugChrome + '/common'))
.pipe(gulp.dest(buildDirDebugFirefox + '/common'))
);
} else {
var tsResult = tsProjectCommon.src()
.pipe(tsProjectCommon());
return merge(
tsResult.dts.pipe(gulp.dest('typedefs')),
tsResult.js
.pipe(gulp.dest(buildDirProd + '/common'))
.pipe(gulp.dest(buildDirProdChrome + '/common'))
.pipe(gulp.dest(buildDirProdFirefox + '/common'))
);
}
});
/********** COMPILING TYPESCRIPT **********/
var buildTypescript = function (tsProject, destination) {
if (DEBUG) {
return tsProject.src()
.pipe(sourcemaps.init())
.pipe(tsProject())
.js
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(buildDirDebug + '/' + destination))
.pipe(gulp.dest(buildDirDebugChrome + '/' + destination))
.pipe(gulp.dest(buildDirDebugFirefox + '/' + destination));
} else {
return tsProject.src()
.pipe(tsProject())
.js
.pipe(gulp.dest(buildDirProd + '/' + destination))
.pipe(gulp.dest(buildDirProdChrome + '/' + destination))
.pipe(gulp.dest(buildDirProdFirefox + '/' + destination));
}
};
var tsProjectBackground = ts.createProject("background/tsconfig.json", {
outFile: globTSBackgroundOut
});
gulp.task("compilets:background", ["clean:ts:background", "lint:background"], function() {
return buildTypescript(tsProjectBackground, "background");
});
var tsProjectPopup = ts.createProject("popup/tsconfig.json", {
outFile: globTSPopupOut
});
gulp.task("compilets:popup", ["clean:ts:popup", "lint:popup"], function() {
return buildTypescript(tsProjectPopup, "popup");
});
var tsProjectPanels = ts.createProject("panels/tsconfig.json", {
outFile: globTSPanelsOut
});
gulp.task("compilets:panels", ["clean:ts:panels", "lint:panels"], function() {
return buildTypescript(tsProjectPanels, "panels");
});
var tsProjectPage = ts.createProject("page/tsconfig.json", {
outFile: globTSPageOut
});
gulp.task("compilets:page", ["clean:ts:page", "lint:page"], function() {
return buildTypescript(tsProjectPage, "page");
});
var tsProjectSettings = ts.createProject("settings/tsconfig.json", {
outFile: globTSSettingsOut
});
gulp.task("compilets:settings", ["clean:ts:settings", "lint:settings"], function() {
return buildTypescript(tsProjectSettings, "settings");
});
var tsProjectDialogs = ts.createProject("dialogs/tsconfig.json", {});
gulp.task("compilets:dialogs", ["clean:ts:dialogs", "lint:dialogs"], function() {
return buildTypescript(tsProjectDialogs, "dialogs");
});
/********** STATIC FILE COPYING / MANIPULATION **********/
var copyStatic = function (glob, dir) {
return gulp.src(glob)
.pipe(gulp.dest((DEBUG ? buildDirDebug : buildDirProd) + dir))
.pipe(gulp.dest((DEBUG ? buildDirDebugFirefox : buildDirProdFirefox) + dir))
.pipe(gulp.dest((DEBUG ? buildDirDebugChrome : buildDirProdChrome) + dir))
}
gulp.task('static:popup', ["clean:static:popup"], function() {
return copyStatic(globStaticPopup, '/popup');
});
gulp.task('static:panels', ["clean:static:panels"], function() {
return copyStatic(globStaticPanels, '/panels');
});
gulp.task('static:page', ["clean:static:page"], function() {
return copyStatic(globStaticPage, '/page');
});
gulp.task('static:background', ["clean:static:background"], function() {
return copyStatic(globStaticBackground, '/background');
});
gulp.task('static:settings', ["clean:static:settings"], function() {
return copyStatic(globStaticSettings, '/settings');
});
gulp.task('static:dialogs', ["clean:static:dialogs"], function() {
return copyStatic(globStaticDialogs, '/dialogs');
});
gulp.task('static:lib', ["clean:static:lib"], function() {
return copyStatic(globStaticLib, '/lib');
});
gulp.task('static:common', ["clean:static:common"], function() {
return copyStatic(globStaticCommon, '/common');
});
gulp.task('static:commonFonts', ["clean:static:commonFonts"], function() {
return copyStatic(globStaticCommonFonts, '/common/fonts');
});
gulp.task('static:commonImages', ["clean:static:commonImages"], function() {
return copyStatic(globStaticCommonImages, '/common/images');
});
gulp.task('static:releasenotes', ["clean:static:releasenotes"], function() {
return copyStatic(globStaticReleaseNotes, '/release-notes');
});
gulp.task('static:locales', ["clean:static:locales"], function() {
return copyStatic(globStaticLocales, '/_locales');
});
gulp.task('copyStaticManifest', function() {
return copyStatic(globStaticManifest, '');
});
gulp.task('modifyBuildFilesForCrossBrowser', function() {
if (DEBUG) {
return merge (
gulp.src([buildDirDebugFirefox + '/manifest.json'])
.pipe(replace(/(.*"version": ")(.*)(",.*)/g, '$1$2beta$3'))
.pipe(replace(/(.*"version_name": ")(.*)(",.*)/g, ''))
.pipe(gulp.dest(buildDirDebugFirefox)),
gulp.src([buildDirDebugChrome + '/manifest.json'])
.pipe(replace(/(.*"version_name": ")(.*)(",.*)/g, '$1$2 Beta$3'))
.pipe(replace(/(,[\s]*?)"applications": ([\S\s]*?}){2}/g, ''))
// hack to workaround https://github.com/mozilla/webextension-polyfill/issues/70 :
.pipe(replace(/(.*"clipboardWrite",)(.*)/g, ''))
.pipe(replace(/(.*"clipboardRead",)(.*)/g, ''))
.pipe(gulp.dest(buildDirDebugChrome))
);
} else {
return merge (
gulp.src([buildDirProdFirefox + '/manifest.json'])
.pipe(replace(/(.*"version_name": ")(.*)(",.*)/g, ''))
.pipe(replace(/(.*"update_url": ")(.*)(",.*)/g, ''))
.pipe(gulp.dest(buildDirProdFirefox)),
gulp.src([buildDirProdChrome + '/manifest.json'])
.pipe(replace(/(,[\s]*?)"applications": ([\S\s]*?}){2}/g, ''))
// hack to workaround https://github.com/mozilla/webextension-polyfill/issues/70 :
.pipe(replace(/(.*"clipboardWrite",)(.*)/g, ''))
.pipe(replace(/(.*"clipboardRead",)(.*)/g, ''))
.pipe(gulp.dest(buildDirProdChrome))
)
}
});
gulp.task('static:manifest', ["clean:static:manifest"], function(done) {
sequence("copyStaticManifest", "modifyBuildFilesForCrossBrowser", done);
});
gulp.task('static', ['static:popup','static:panels','static:page','static:background',
'static:settings','static:dialogs','static:common','static:commonFonts',
'static:commonImages','static:releasenotes','static:locales',
'static:manifest', 'static:lib']);
/********** WATCHING FOR CHANGES TO SOURCE FILES **********/
gulp.task('watch', ['compilets', 'static'], function() {
gulp.watch([globTSCommon], ['compilets:common']);
gulp.watch([globTSPopup], ['compilets:popup']);
gulp.watch([globTSPanels], ['compilets:panels']);
gulp.watch([globTSPage], ['compilets:page']);
gulp.watch([globTSBackground], ['compilets:background']);
gulp.watch([globTSSettings], ['compilets:settings']);
gulp.watch([globTSDialogs], ['compilets:dialogs']);
gulp.watch([globStaticPopup], ['static:popup']);
gulp.watch([globStaticPanels], ['static:panels']);
gulp.watch([globStaticPage], ['static:page']);
gulp.watch([globStaticBackground], ['static:background']);
gulp.watch([globStaticSettings], ['static:settings']);
gulp.watch([globStaticDialogs], ['static:dialogs']);
gulp.watch([globStaticLib], ['static:lib']);
gulp.watch([globStaticCommon], ['static:common']);
gulp.watch([globStaticCommonFonts], ['static:commonFonts']);
gulp.watch([globStaticCommonImages], ['static:commonImages']);
gulp.watch([globStaticReleaseNotes], ['static:releasenotes']);
gulp.watch([globStaticLocales], ['static:locales']);
gulp.watch([globStaticManifest], ['static:manifest']);
});
/********** PACKAGING **********/
gulp.task('zip', function() {
var manifest = require('./manifest'),
distFileName = manifest.name + '-v' + manifest.version + '.zip';
return gulp.src([buildDirProdChrome + '/**'])
.pipe(zip(distFileName))
.pipe(gulp.dest('dist'));
});
gulp.task('xpi', function() {
var manifest = require('./manifest'),
distFileName = manifest.name + '-v' + manifest.version + '.xpi';
return gulp.src([buildDirProdFirefox + '/**'])
.pipe(zip(distFileName))
.pipe(gulp.dest('dist'));
});
gulp.task('zip:debug', function() {
var manifest = require('./manifest'),
distFileName = manifest.name + '-v' + manifest.version + '-debug.zip';
return gulp.src([buildDirDebugChrome + '/**'])
.pipe(zip(distFileName))
.pipe(gulp.dest('dist'));
});
gulp.task('xpi:debug', function() {
var manifest = require('./manifest'),
distFileName = manifest.name + '-v' + manifest.version + '-debug.xpi';
return gulp.src([buildDirDebugFirefox + '/**'])
.pipe(zip(distFileName))
.pipe(gulp.dest('dist'));
});
gulp.task('package:prod', function (done) {
DEBUG = false;
sequence(['compilets', 'static'], ['xpi','zip'], done);
});
gulp.task('package:debug', function (done) {
sequence(['compilets', 'static'], ['xpi:debug','zip:debug'], done);
});
/********** CLEANING **********/
// Assumes inclusions and exclusions are the same for all browsers
var deleteBuildFiles = function (includeGlobs, excludeGlobs) {
var globs = [];
for (let g of includeGlobs) {
if (DEBUG) {
globs.push(buildDirDebug + '/' + g);
globs.push(buildDirDebugFirefox + '/' + g);
globs.push(buildDirDebugChrome + '/' + g);
} else {
globs.push(buildDirProd + '/' + g);
globs.push(buildDirProdFirefox + '/' + g);
globs.push(buildDirProdChrome + '/' + g);
}
}
if (excludeGlobs) for (let g of excludeGlobs) {
if (DEBUG) {
globs.push('!' + buildDirDebug + '/' + g);
globs.push('!' + buildDirDebugFirefox + '/' + g);
globs.push('!' + buildDirDebugChrome + '/' + g);
} else {
globs.push('!' + buildDirProd + '/' + g);
globs.push('!' + buildDirProdFirefox + '/' + g);
globs.push('!' + buildDirProdChrome + '/' + g);
}
}
return del(globs);
};
gulp.task('clean:ts:dialogs', function () {
return deleteBuildFiles([globTSDialogsOut, globTSDialogsOut + '.map'], ['dialogs']);
});
gulp.task('clean:ts:settings', function () {
return deleteBuildFiles([globTSSettingsOut, globTSSettingsOut + '.map']);
});
gulp.task('clean:ts:background', function () {
return deleteBuildFiles([globTSBackgroundOut, globTSBackgroundOut + '.map']);
});
gulp.task('clean:ts:page', function () {
return deleteBuildFiles([globTSPageOut, globTSPageOut + '.map']);
});
gulp.task('clean:ts:panels', function () {
return deleteBuildFiles([globTSPanelsOut, globTSPanelsOut + '.map']);
});
gulp.task('clean:ts:popup', function () {
return deleteBuildFiles([globTSPopupOut, globTSPopupOut + '.map']);
});
gulp.task('clean:ts:common', function () {
return deleteBuildFiles([globTSCommonOut, globTSCommonOut + '.map']);
});
gulp.task('clean:static:popup', function () {
return deleteBuildFiles([globStaticPopup]);
});
gulp.task('clean:static:panels', function () {
return deleteBuildFiles([globStaticPanels]);
});
gulp.task('clean:static:page', function () {
return deleteBuildFiles([globStaticPage]);
});
gulp.task('clean:static:background', function () {
return deleteBuildFiles([globStaticBackground]);
});
gulp.task('clean:static:settings', function () {
return deleteBuildFiles([globStaticSettings]);
});
gulp.task('clean:static:dialogs', function () {
return deleteBuildFiles([globStaticDialogs]);
});
gulp.task('clean:static:lib', function () {
return deleteBuildFiles([globStaticLib]);
});
gulp.task('clean:static:common', function () {
return deleteBuildFiles([globStaticCommon]);
});
gulp.task('clean:static:commonImages', function () {
return deleteBuildFiles([globStaticCommonImages]);
});
gulp.task('clean:static:commonFonts', function () {
return deleteBuildFiles([globStaticCommonFonts]);
});
gulp.task('clean:static:releasenotes', function () {
return deleteBuildFiles([globStaticReleaseNotes]);
});
gulp.task('clean:static:locales', function () {
return deleteBuildFiles([globStaticLocales]);
});
gulp.task('clean:static:manifest', function () {
return deleteBuildFiles([globStaticManifest]);
});
gulp.task('clean', [
'clean:static:manifest', 'clean:static:locales', 'clean:static:releasenotes',
'clean:static:commonFonts', 'clean:static:commonImages',
'clean:static:common', 'clean:static:dialogs', 'clean:static:settings',
'clean:static:background', 'clean:static:page', 'clean:static:panels',
'clean:static:popup', 'clean:ts:common', 'clean:ts:popup', 'clean:ts:panels',
'clean:ts:page', 'clean:ts:background', 'clean:ts:settings',
'clean:ts:dialogs'
]);
gulp.task('sign', function () {
const manifest = require('./manifest');
const distFileName = manifest.name + '-v' + manifest.version + '-debug.xpi';
//TODO: If API output is suitable, derive these file names from that in case Mozilla change file naming conventions one day
fs.writeFileSync('.signedKeeXPI', 'kee-' + manifest.version + 'beta-an+fx.xpi');
fs.writeFileSync('.downloadLinkKeeXPI', 'https://github.com/kee-org/browser-addon/releases/download/'
+ manifest.version + '/kee-' + manifest.version + 'beta-an+fx.xpi');
signAddon({
xpiPath: 'dist/' + distFileName,
version: manifest.version + 'beta',
apiKey: process.env.AMO_API_KEY,
apiSecret: process.env.AMO_API_SECRET,
id: '[email protected]',
downloadDir: 'dist/signed/',
channel: 'unlisted'
})
.then(function(result) {
if (result.success) {
console.log("The following signed files were downloaded:");
console.log(result.downloadedFiles);
console.log("Reported file name: ");
if (result.downloadedFiles && result.downloadedFiles.length > 0) console.log(result.downloadedFiles[0]);
} else {
console.error("add-on could not be signed!");
console.error("Check the console for details.");
}
console.log(result.success ? "SUCCESS" : "FAIL");
})
.catch(function(error) {
console.error("Signing error:", error);
});
});