-
Notifications
You must be signed in to change notification settings - Fork 45
/
gulpfile.mjs
395 lines (327 loc) · 12.4 KB
/
gulpfile.mjs
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
import yargs from 'yargs';
import fs from 'fs'; // Node.js File System module
import path from 'path'; // Node.js Path System module
import del from 'del'; // Delete files/folders using globs.
import jsonfile from 'jsonfile'; // Easily read/write JSON files.
import mergeStream from 'merge-stream'; // Create a stream that emits events from multiple other streams.
import through from 'through2';
import gulp from 'gulp'; // The streaming build system.
import less from 'gulp-less'; // A LESS plugin for Gulp
import stripDebug from 'gulp-strip-debug'; // Strip console and debugger statements from JavaScript code.
import ts from 'gulp-typescript'; // Using typescript compiler for gulp.js
import sourcemaps from 'gulp-sourcemaps'; // Using gulp source map plugin
import zip from 'gulp-zip'
// =============================================================================
// Global variables
// =============================================================================
// Output folders for *.crx and *.xpi files
var src = path.normalize(process.cwd() + '/src/');
var dist = path.normalize(process.cwd() + '/dist/');
var config = {
distDir: dist,
keepDebug: false,
keepSources: false
};
const argv = yargs(process.argv).argv;
if (argv.newversion) {
config.version = argv.newversion;
}
if (argv.distDir) {
config.distDir = argv.distDir + '/';
}
if (argv.keepDebug) {
config.keepDebug = argv.keepDebug;
}
if (argv.keepSources) {
config.keepSources = argv.keepSources;
}
// detect visual studio
if (process.env['VSAPPIDNAME']) {
config.keepDebug = true;
config.keepSources = true;
}
var distDir = config.distDir;
var chromeDir = distDir + 'chrome/';
var chromeUnpackedDir = chromeDir + 'unpacked/';
var firefoxDir = distDir + 'firefox/';
var firefoxUnpackedDir = firefoxDir + 'unpacked/';
var safariUnpackedDir = distDir + 'safari/';
console.log('Start build');
console.log(JSON.stringify(config, null, 2));
var files = {
common: [
'src/background/storage.js',
'src/background/webToolDescriptions.js',
'src/background/webToolManager.js',
'src/background/ajaxClient.js',
'src/background/oidcClient.js',
'src/background/contentScriptsRegistrator.js',
'src/background/serverConnection.js',
'src/background/signalRHubProxy.js',
'src/background/signalRConnection.js',
'src/css/*.css',
'src/in-page-scripts/integrations/*.js',
'src/in-page-scripts/integrationService.js',
'src/in-page-scripts/authorizationCode.js',
'src/in-page-scripts/page.js',
'src/in-page-scripts/init.js',
'src/in-page-scripts/topmostPage.js',
'src/in-page-scripts/version.js',
'src/in-page-scripts/utils.js',
'src/lib/jquery.min.js',
'src/lib/signalr.min.js',
'src/lib/select2/select2.full.min.js',
'src/lib/select2/select2.min.css',
'src/images/integrations/**',
'src/images/*.*',
'src/popup/popup.html',
'src/popup/popupController.js',
'src/popup/pagePopupController.js',
'src/popup/popupActivator.js',
'src/permissions/check.html',
'src/permissions/check.js',
'src/permissions/permissionManager.js',
'src/permissions/permissions.html',
'src/permissions/permissions.js',
'src/settings/settings.html',
'src/settings/settingsController.js',
'src/background/backgroundBase.js',
'src/background/extensionBase.js',
'src/background/simpleEvent.js'
],
chrome: [
'src/manifest.json',
'src/unified-ext.js',
'src/chrome-background-bundle.js',
'src/background/chromeExtension.js',
],
firefox: [
'src/unified-ext.js',
'src/background/firefoxExtension.js'
],
safari: [
'src/unified-ext.js',
'src/background/safariExtension.js',
'src/safari/**'
]
};
// common operations
function replaceInFile(file, find, replace) {
var text = fs.readFileSync(file) + '';
if (text) {
text = text.replace(find, replace);
fs.writeFileSync(file, text);
}
}
function stripDebugCommon(folder) {
if (config.keepDebug) {
return Promise.resolve();
}
return gulp
.src(
[ folder + '**/*.js', '!' + folder + 'lib/**/*.js'],
{ base: folder, encoding: false }
)
.pipe(stripDebug())
.pipe(gulp.dest(folder));
}
function modifyFile(transform = null) {
return through.obj(function (file, encoding, callback) {
file = file.clone();
if (!file.isBuffer()) {
return reject(new Error('Invalid file: ' + e.message));
}
let fileContent = file.contents.toString(encoding);
if (transform) {
fileContent = transform(fileContent);
}
file.contents = Buffer.from(fileContent);
callback(null, file);
});
}
function modifyFileJSON(transform) {
return modifyFile(text => JSON.stringify(transform(JSON.parse(text)), null, ' '));
}
// =============================================================================
// Common tasks (used for all extensions)
// =============================================================================
gulp.task('version', (callback) => {
var version = config.version;
if (version) {
[
'package.json',
src + 'manifest.json',
src + 'manifest-v2/manifest.json',
src + 'in-page-scripts/version.ts'
].forEach(file => replaceInFile(
file,
/(["']?version["']?: ["'])([\d\.]+)(["'])/,
(match, left, oldVersion, right) => (left + version + right)));
// set app store version
replaceInFile(
src + 'safari/TMetric for Safari.xcodeproj/project.pbxproj',
/(MARKETING_VERSION = )([\d\.]+)(;)/g,
(match, left, oldVersion, right) => (left + version + right));
}
callback();
});
// clean
function clean(input) {
return del(input, { force: true });
}
gulp.task('clean:sources', () => {
return clean([
'src/**/*.map',
'src/background/*.js',
'src/css/*.css',
'src/in-page-scripts/**/*.js',
'src/lib/*',
'src/popup/*.js',
'src/settings/*.js'
]);
});
gulp.task('clean:dist', () => {
return clean([distDir + '**']);
});
gulp.task('clean', gulp.parallel('clean:sources', 'clean:dist'));
// lib
gulp.task('lib', async () => {
const lib = src + 'lib/';
const jquery = gulp
.src('node_modules/jquery/dist/jquery.min.js', { encoding: false })
.pipe(modifyFile())
.pipe(gulp.dest(lib));
const signalr = gulp
.src('node_modules/@microsoft/signalr/dist/webworker/signalr.min.js', { encoding: false })
.pipe(modifyFile(text => text.replace(/\/\/\s*#\s*sourceMappingURL.+?\.map/, '')))
.pipe(gulp.dest(lib));
const select2 = gulp
.src(['node_modules/select2/dist/js/select2.full.min.js'], { encoding: false })
.pipe(modifyFile())
.pipe(gulp.dest(lib + 'select2/'));
const select2css = gulp
.src(['node_modules/select2/dist/css/select2.min.css'], { encoding: false })
.pipe(modifyFile())
.pipe(gulp.dest(lib + 'select2/'));
return mergeStream(jquery, signalr, select2, select2css);
});
// compile
gulp.task('compile:ts', () => {
var project = ts.createProject('./src/tsconfig.json');
var files = project.config.files.map(path => src + path);
let task = gulp.src(files, { base: src, encoding: false });
if (config.keepSources) {
task = task.pipe(sourcemaps.init());
}
task = task.pipe(project());
if (config.keepSources) {
task = task.pipe(sourcemaps.write({ sourceRoot: '/', includeContent: true }))
}
task = task.pipe(gulp.dest(src));
return task;
});
gulp.task('compile:less', () => {
return gulp
.src('src/css/*.less', { encoding: false })
.pipe(less())
.pipe(gulp.dest(src + 'css/'));
});
gulp.task('compile', gulp.parallel('compile:ts', 'compile:less'));
// =============================================================================
// Tasks for building Chrome extension
// =============================================================================
function copyFilesChrome() {
return gulp
.src(files.common.concat(files.chrome), { base: src, encoding: false })
.pipe(gulp.dest(chromeUnpackedDir));
}
function stripDebugChrome() {
return stripDebugCommon(chromeUnpackedDir);
}
gulp.task('prepackage:chrome', gulp.series(copyFilesChrome, stripDebugChrome));
function packageChrome() {
var manifest = jsonfile.readFileSync(chromeUnpackedDir + 'manifest.json');
return gulp
.src(chromeUnpackedDir + '**/*', { encoding: false })
.pipe(zip(manifest.short_name.toLowerCase() + '-' + manifest.version + '.zip'))
.pipe(gulp.dest(chromeDir));
}
gulp.task('package:chrome', gulp.series('prepackage:chrome', packageChrome));
// =============================================================================
// Tasks for building Firefox addon
// =============================================================================
function copyFilesFirefox() {
return gulp
.src(files.common.concat(files.firefox), { base: src, encoding: false })
.pipe(gulp.dest(firefoxUnpackedDir));
}
function copyManifestFirefox() {
return gulp
.src(['src/manifest-v2/manifest.json'], { encoding: false })
.pipe(gulp.dest(firefoxUnpackedDir));
}
function modifyManifestFirefox() {
return gulp
.src(firefoxUnpackedDir + '/manifest.json', { encoding: false })
.pipe(modifyFileJSON(json => ({ applications: { gecko: { id: '@tmetric' } }, ...json })))
.pipe(gulp.dest(firefoxUnpackedDir));
}
function stripDebugFirefox() {
return stripDebugCommon(firefoxUnpackedDir);
}
gulp.task(
'prepackage:firefox',
gulp.series(copyFilesFirefox, copyManifestFirefox, stripDebugFirefox, modifyManifestFirefox));
function packageFirefox() {
var manifest = jsonfile.readFileSync(firefoxUnpackedDir + 'manifest.json');
return gulp
.src(firefoxUnpackedDir + '**/*', { encoding: false })
.pipe(zip(manifest.short_name.toLowerCase() + '-' + manifest.version + '.xpi'))
.pipe(gulp.dest(firefoxDir));
}
gulp.task('package:firefox', gulp.series('prepackage:firefox', packageFirefox));
// =============================================================================
// Tasks for building Safari extension
// =============================================================================
function copyFilesSafari() {
return gulp
.src(files.common.concat(files.safari), { base: src, encoding: false })
.pipe(gulp.dest(safariUnpackedDir));
}
function stripDebugSafari() {
return stripDebugCommon(safariUnpackedDir);
}
function copyManifestSafari() {
return gulp
.src(['src/manifest-v2/manifest.json'], { encoding: false })
.pipe(gulp.dest(safariUnpackedDir));
}
function modifyManifestSafari() {
return gulp
.src(safariUnpackedDir + '/manifest.json', { encoding: false })
.pipe(modifyFile(text => text
.replace('/firefoxExtension.js', '/safariExtension.js')
.replace(/(?<="name":\s*")[^"]+(?=")/, 'TMetric – Time & Productivity Tracker')
))
.pipe(gulp.dest(safariUnpackedDir));
}
gulp.task(
'package:safari',
gulp.series(copyFilesSafari, copyManifestSafari, stripDebugSafari, modifyManifestSafari));
// =============================================================================
// Task for building addons
// =============================================================================
gulp.task('build', gulp.series(
'clean', 'lib', 'compile', 'version',
gulp.parallel('package:chrome', 'package:firefox', 'package:safari')
));
// =============================================================================
// Task for building safari addon
// =============================================================================
gulp.task('build:safari', gulp.series(
'clean', 'compile', 'package:safari'
));
// =============================================================================
// Default Task
// =============================================================================
gulp.task('default', gulp.series('build'));