-
Notifications
You must be signed in to change notification settings - Fork 35
/
gulpfile.js
231 lines (181 loc) · 5.24 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
'use strict';
/* global require */
const gulp = require('gulp');
const { series, parallel } = gulp;
// const babel = require('gulp-babel');
const del = require('del').sync;
const cp = require('child_process');
// const sourcemaps = require('gulp-sourcemaps');
const hb = require('gulp-hb');
const fs = require('fs');
// const gutil = require('gulp-util');
const rename = require('gulp-rename');
const merge = require('merge-stream');
let execDefaultOpts = {
stdio: 'inherit',
};
const PackageData = JSON.parse(fs.readFileSync('./package.json'));
const Chrome = {
ArtifactsPath: './artifacts/chrome',
BuildPath: './build/chrome',
BuildData: {
Chrome: true,
quad_version: PackageData.version.replace(/[^\d.]+/g, '.'),
},
};
const FireFox = {
ArtifactsPath: './artifacts/ff',
BuildPath: './build/ff',
BuildData: {
Firefox: true,
},
};
/** This is passed to all handlebar builds */
const GeneralBuildConfig = {};
/** Wrapper for child_process.spawnSync */
function exec(cmd, options = execDefaultOpts) {
// console.log(cmd);
// return cp.spawn('cmd.exe', ['/A', '/D', '/C', cmd], options);
return cp.spawn('bash', ['-c', '--', cmd], options);
}
const TaskGlobs = new Map(),
SpecialGlobs = new Map();
TaskGlobs.set('', [
'LICENSE',
]);
TaskGlobs.set('src', [
'src/**/*.js',
'!**/*Test*',
'!**/*JetBrains*',
'!**/*-compiled*',
'!**/*Playground*',
'!**/Dev*',
// ui
'src/**/*.htm*',
]);
TaskGlobs.set('src/lib', [
'node_modules/webextension-polyfill/dist/browser-polyfill.js',
]);
TaskGlobs.set('res', [
'res/**/*.png',
'!res/Marketing/*.png',
]);
SpecialGlobs.set('manifest', [
'src/templates/*.hbs',
'package.json',
]);
const watchOpts = {
delay: 500,
ignoreInitial: false,
};
/**
* General Building Tasks
*/
function buildTmp() {
del(['./build/tmp']);
return merge(...
Array.from(TaskGlobs.keys())
.map((key) =>
gulp.src(TaskGlobs.get(key))
.pipe(gulp.dest(`./build/tmp/${key}`))
)
);
}
let webext = (cmd, data) => `yarn run web-ext ${cmd} -s ${data.BuildPath} -a ${data.ArtifactsPath} `;
function buildFor({ BuildPath, BuildData }) {
del(BuildPath);
return merge(
gulp.src('./build/tmp/**')
.pipe(gulp.dest(BuildPath)),
gulp.src('src/templates/manifest.hbs')
.pipe(
hb()
.helpers(require('handlebars-cond'))
.data('./package.json')
.data(BuildData)
.data(GeneralBuildConfig)
)
.pipe(rename({
extname: '.json',
}))
.pipe(gulp.dest(BuildPath))
);
}
/*************************************************************************************************
* Chrome Building Tasks
************************************************************************************************/
function buildChrome() { return buildFor(Chrome); }
let PackageChrome = series(buildChrome, () => {
return exec(webext('build --overwrite-dest', Chrome));
});
let PublishChrome = series(PackageChrome, (done) => {
console.warn('Chrome Publishing is not yet supported.');
done();
});
/*************************************************************************************************
* Firefox Building Tasks
************************************************************************************************/
function buildFirefox() { return buildFor(FireFox); }
let PackageFirefox = series(buildFirefox, () => {
return exec(webext('build --overwrite-dest', FireFox));
});
/**
* NOTE: Using the web-ext sign will automatically upload for submission to AMO
*/
let PublishFirefox = series(PackageFirefox, () => {
let SecureDataFilepath = './insecure/Firefox/api-key.json';
if(!fs.existsSync(SecureDataFilepath)) {
console.error('Unable to sign Firefox extension, secret data not available.');
return;
}
let SecureData = JSON.parse(fs.readFileSync(SecureDataFilepath));
return exec(webext(`sign --channel listed --api-key ${SecureData.jwt_issuer} --api-secret ${SecureData.jwt_secret}`, FireFox));
});
//noinspection JSUnusedLocalSymbols
let LintFirefox = series(buildFirefox, () => {
return exec(webext('lint', FireFox));
});
/*************************************************************************************************
* Main Build Tasks
************************************************************************************************/
let buildAll = series(
buildTmp,
parallel(buildChrome, buildFirefox)
);
function watch() {
// Set DevBuild to true for 'gulp watch'
GeneralBuildConfig.DevBuild = true;
// Remove the '!**/Dev*' string from the src map for 'gulp watch'
let srcFiles = TaskGlobs.get('src');
srcFiles.splice(srcFiles.indexOf('!**/Dev*'), 1);
TaskGlobs.set('src', srcFiles);
function building(done) {
console.log('\nFiles Changed, building...');
done();
}
function built(done) {
fs.writeFileSync('./build/build.json', JSON.stringify({
buildVersion: PackageData.version,
buildTime: Date.now()
.toString(),
}));
done();
}
let WatchGlobs = Array.from([...TaskGlobs.values(), ...SpecialGlobs.values()])
.flat();
gulp.watch(WatchGlobs,
watchOpts,
series(building, buildAll, built));
}
module.exports = {
default: buildAll,
watch: watch,
package: series(
buildTmp,
parallel(PackageFirefox, PackageChrome)
),
publish: series(
buildTmp,
parallel(PublishFirefox, PublishChrome)
),
};