-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
256 lines (235 loc) · 5.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
/**
* @license
*
* UpFish - dynamically making fun of your movies
* Copyright (C) 2021 Joey Parrish
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
const gulp = require('gulp');
const del = require('del');
const eslint = require('gulp-eslint');
const rename = require('gulp-rename');
const {rollup} = require('rollup');
const svg2img = require('svg2img');
const transform = require('gulp-transform');
const zip = require('gulp-zip');
/**
* Remove everything that we built.
*
* @return {!Promise}
*/
function clean() {
return del(['dist']);
}
/**
* Lint the entire codebase.
*
* @return {!Stream}
*/
function lint() {
return gulp.src([
'*.js',
'src/**/*.js',
'extension/*.js',
])
.pipe(eslint())
// Without this, eslint errors are not shown.
.pipe(eslint.format())
// Without this, eslint errors will not fail this gulp task.
.pipe(eslint.failAfterError());
}
/**
* Create a bundle for UpFish.
*
* When UpFish is loaded by the extension, it is done as a "content script" in
* the context of the page. This does not allow us to load an ES module, so
* the entire UpFish library must be bundled into a single IIFE that exports
* UpFish to |window|.
*
* @return {!Promise}
*/
async function bundleUpFish() {
const bundle = await rollup({
input: 'src/upfish.js',
});
await bundle.write({
file: 'dist/upfish.bundle.js',
format: 'iife',
name: 'UpFish',
});
}
/**
* Create a bundle for UpFish's service worker.
*
* When the service worker is loaded by the extension, it is not loaded as an
* ES module. So the service worker and its imports must be bundled together.
*
* @return {!Promise}
*/
async function bundleServiceWorker() {
await new Promise((resolve, reject) => {
gulp.src([
'extension/service-worker.js',
])
.pipe(gulp.dest('.staging/'))
.on('finish', resolve)
.on('error', reject);
});
const bundle = await rollup({
input: '.staging/service-worker.js',
});
await bundle.write({
file: 'dist/service-worker.js',
format: 'iife',
name: 'ServiceWorker',
});
}
/**
* Copy all extension files to the output folder.
*
* @return {!Stream}
*/
function copyExtensionFiles() {
return gulp.src([
'src/lib/karaoke-worklet.js',
'extension/*',
'!extension/service-worker.js',
'README.md',
'LICENSE.md',
])
.pipe(gulp.dest('dist/'));
}
/**
* Copy all built-in config files to the output folder.
*
* @return {!Stream}
*/
function copyConfigs() {
return gulp.src([
'configs/*',
])
.pipe(gulp.dest('dist/configs/'));
}
/**
* Convert an SVG to a PNG.
*
* @param {!Buffer} svg
* @param {{width: number, height: number}} size
* @return {!Promise<QBuffer>}
*/
function svgToPng(svg, size) {
return new Promise((resolve, reject) => {
svg2img(svg, size, (error, png) => {
if (error) {
reject(error);
} else {
resolve(png);
}
});
});
}
/**
* Generate a PNG version of our SVG logo.
*
* The SVG logo can't be used as the icon in the Chrome extension, so it must
* be converted to PNG.
*
* @return {!Stream}
*/
function generatePng() {
return gulp.src([
'upfish.svg',
])
.pipe(transform(null, (content, file) => {
return svgToPng(content, {width: 128, height: 128});
}))
.pipe(rename('upfish.png'))
.pipe(gulp.dest('dist/'));
}
/**
* Generate a PNG version of our SVG logo for when the extension is active.
*
* This swaps the colors in our SVG logo, then converts it to PNG.
*
* @return {!Stream}
*/
function generateActivePng() {
return gulp.src([
'upfish.svg',
])
.pipe(transform(null, (content, file) => {
// Swap the body and eye colors.
content = Buffer.from(
content.toString('utf8')
.replace('cornflowerblue', 'BODYCOLOR')
.replace('lightsalmon', 'cornflowerblue')
.replace('BODYCOLOR', 'lightsalmon'));
return svgToPng(content, {width: 128, height: 128});
}))
.pipe(rename('upfish.active.png'))
.pipe(gulp.dest('dist/'));
}
/**
* Package the Chrome extension as a zip file.
*
* @return {!Stream}
*/
function packageExtension() {
return gulp.src(['dist/**'])
.pipe(rename((path) => {
path.dirname = `upfish/${path.dirname}`;
}))
.pipe(zip('upfish.zip'))
.pipe(gulp.dest('./'));
}
/**
* The "build" task: clean, then execute all build steps in parallel.
*
* Only creates an "unpacked" extension in the "dist/" folder.
* Does not package the extension as a zip file.
*
* @type {!TaskFunction}
*/
const build = exports.build = gulp.series(
clean,
gulp.parallel(
bundleUpFish,
copyExtensionFiles,
copyConfigs,
bundleServiceWorker,
generatePng,
generateActivePng));
/**
* By default, lint and build in parallel.
* @type {!TaskFunction}
*/
exports.default = gulp.parallel(lint, build);
/**
* The "release" task: lint and build in parallel, then package the extension.
* @type {!TaskFunction}
*/
exports.release = gulp.series(
gulp.parallel(lint, build),
packageExtension);
/**
* The "clean" task: remove everything that we built.
* @type {!TaskFunction}
*/
exports.clean = clean;
/**
* The "lint" task: lint the entire codebase.
* @type {!TaskFunction}
*/
exports.lint = lint;