-
Notifications
You must be signed in to change notification settings - Fork 494
/
gulpfile.js
184 lines (168 loc) · 4.98 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
const fs = require('fs').promises;
const gulp = require('gulp');
const del = require('del');
const log = require('fancy-log');
const plumber = require('gulp-plumber');
const Sharp = require('sharp');
const spawn = require('cross-spawn');
const i18n = require('./scripts/i18n');
const { getVersion, isBeta } = require('./scripts/version-helper');
const { buildManifest } = require('./scripts/manifest-helper');
const pkg = require('./package.json');
const DIST = 'dist';
const paths = {
manifest: 'src/manifest.yml',
locales: [
'src/_locales/**',
],
templates: [
'src/**/*.@(js|html|json|yml|vue)',
],
};
function clean() {
return del(DIST);
}
function watch() {
gulp.watch(paths.manifest, manifest);
gulp.watch(paths.locales.concat(paths.templates), copyI18n);
}
async function jsDev() {
return runCommand('webpack-cli', ['-w', '--config', 'scripts/webpack.conf.js']);
}
async function jsProd() {
return runCommand('webpack-cli', ['--config', 'scripts/webpack.conf.js']);
}
function runCommand(command, args) {
return new Promise((resolve, reject) => {
const child = spawn(command, args, {
stdio: 'inherit',
});
child.on('close', (code, signal) => {
(code ? reject : resolve)(signal);
});
});
}
/**
* manifest is already handled in ListBackgroundScriptsPlugin
*
* This task is only used to tweak dist/manifest.json without rebuilding
*/
async function manifest() {
const base = JSON.parse(await fs.readFile(`${DIST}/manifest.json`, 'utf8'));
const data = await buildManifest(base);
await fs.mkdir(DIST).catch(() => {});
await fs.writeFile(`${DIST}/manifest.json`, JSON.stringify(data), 'utf8');
}
async function createIcons() {
const ALPHA = 0.5;
const dist = `${DIST}/public/images`;
await fs.mkdir(dist, { recursive: true });
const icon = Sharp(`src/resources/icon${isBeta() ? '-beta' : ''}.png`);
const gray = icon.clone().grayscale();
const transparent = icon.clone().composite([{
input: Buffer.from([255, 255, 255, 256 * ALPHA]),
raw: { width: 1, height: 1, channels: 4 },
tile: true,
blend: 'dest-in',
}]);
const types = [
['', icon],
['b', gray],
['w', transparent],
];
const handle = (size, type = '', image = icon) => {
let res = image.clone().resize({ width: size });
if (size < 48) res = res.sharpen(size < 32 ? 0.5 : 0.25);
return res.toFile(`${dist}/icon${size}${type}.png`);
};
const darkenOuterEdge = async img => img.composite([{
input: await img.toBuffer(),
blend: 'over',
}]);
const handle16 = async ([type, image]) => {
const res = image.clone()
.resize({ width: 18 })
.sharpen(0.5, 0)
.extract({ left: 1, top: 2, width: 16, height: 16 });
return (type === 'w' ? res : await darkenOuterEdge(res))
.toFile(`${dist}/icon16${type}.png`);
};
return Promise.all([
handle(128),
...types.map(handle16),
// 32px dashboard icon (recycled) 2xDPI browser_action desktop
// 38px dashboard icon (normal) 1.5xDPI browser_action Android
// 48px 2xDPI browser_action Android
...[32, 38, 48, 64].flatMap(size => types.map(t => handle(size, ...t))),
]);
}
/**
* Bump `beta` in `package.json` to release a new beta version.
*/
async function bump() {
if (process.argv.includes('--reset')) {
delete pkg.beta;
} else {
pkg.beta = ( pkg.beta || 0) 1;
}
await fs.writeFile('package.json', JSON.stringify(pkg, null, 2) '\n', 'utf8');
if (process.argv.includes('--commit')) {
const version = `v${getVersion()}`;
spawn.sync('git', ['commit', '-am', version]);
spawn.sync('git', ['tag', '-m', version, version]);
}
}
function checkI18n() {
return i18n.read({
base: 'src/_locales',
extension: '.json',
});
}
function copyI18n() {
return i18n.read({
base: 'src/_locales',
touchedOnly: true,
useDefaultLang: true,
markUntouched: false,
extension: '.json',
stripDescriptions: true,
})
.pipe(gulp.dest(`${DIST}/_locales`));
}
/**
* Load locale files (src/_locales/<lang>/message.[json|yml]), and
* update them with keys in template files, then store in `message.yml`.
*/
function updateI18n() {
return gulp.src(paths.templates)
.pipe(plumber(logError))
.pipe(i18n.extract({
base: 'src/_locales',
manifest: 'src/manifest.yml',
touchedOnly: false,
useDefaultLang: false,
markUntouched: true,
extension: '.yml',
}))
.pipe(gulp.dest('src/_locales'));
}
function logError(err) {
log(err.toString());
return this.emit('end');
}
function copyZip() {
return gulp.src([
'node_modules/@zip.js/zip.js/dist/zip-no-worker.min.js',
'node_modules/@zip.js/zip.js/dist/z-worker.js',
])
.pipe(gulp.dest(`${DIST}/public/lib`));
}
const pack = gulp.parallel(createIcons, copyI18n, copyZip);
exports.clean = clean;
exports.manifest = manifest;
exports.dev = gulp.parallel(gulp.series(pack, watch), jsDev);
exports.build = gulp.series(clean, gulp.parallel(pack, jsProd));
exports.i18n = updateI18n;
exports.check = checkI18n;
exports.copyI18n = copyI18n;
exports.bump = bump;