-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgulpfile.js
executable file
·145 lines (129 loc) · 4.16 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
const path = require('node:path');
const {exec} = require('node:child_process');
const {
lstatSync,
readdirSync,
readFileSync,
writeFileSync,
rmSync
} = require('node:fs');
const {series, parallel, src, dest} = require('gulp');
const gulpif = require('gulp-if');
const jsonMerge = require('gulp-merge-json');
const jsonmin = require('gulp-jsonmin');
const imagemin = require('gulp-imagemin');
const {ensureDirSync} = require('fs-extra');
const sharp = require('sharp');
const targetEnv = process.env.TARGET_ENV || 'chrome';
const isProduction = process.env.NODE_ENV === 'production';
const distDir = path.join(__dirname, 'dist', targetEnv);
function initEnv() {
process.env.BROWSERSLIST_ENV = targetEnv;
}
function init(done) {
initEnv();
rmSync(distDir, {recursive: true, force: true});
ensureDirSync(distDir);
done();
}
async function images(done) {
ensureDirSync(path.join(distDir, 'src/assets/icons/app'));
const appIconSvg = readFileSync('src/assets/icons/app/icon.svg');
const appIconSizes = [16, 19, 24, 32, 38, 48, 64, 96, 128];
for (const size of appIconSizes) {
await sharp(appIconSvg, {density: (72 * size) / 24})
.resize(size)
.toFile(path.join(distDir, `src/assets/icons/app/icon-${size}.png`));
}
// Chrome Web Store does not correctly display optimized icons
if (isProduction && targetEnv !== 'chrome') {
await new Promise(resolve => {
src(path.join(distDir, 'src/assets/icons/app/*.png'), {base: '.'})
.pipe(imagemin())
.pipe(dest('.'))
.on('error', done)
.on('finish', resolve);
});
}
}
async function locale(done) {
const localesRootDir = path.join(__dirname, 'src/assets/locales');
const localeDirs = readdirSync(localesRootDir).filter(function (file) {
return lstatSync(path.join(localesRootDir, file)).isDirectory();
});
for (const localeDir of localeDirs) {
const localePath = path.join(localesRootDir, localeDir);
await new Promise(resolve => {
src(
[
path.join(localePath, 'messages.json'),
path.join(localePath, `messages-${targetEnv}.json`)
],
{allowEmpty: true}
)
.pipe(
jsonMerge({
fileName: 'messages.json',
edit: (parsedJson, file) => {
if (isProduction) {
for (let [key, value] of Object.entries(parsedJson)) {
if (value.hasOwnProperty('description')) {
delete parsedJson[key].description;
}
}
}
return parsedJson;
}
})
)
.pipe(gulpif(isProduction, jsonmin()))
.pipe(dest(path.join(distDir, '_locales', localeDir)))
.on('error', done)
.on('finish', resolve);
});
}
}
function manifest() {
return src(`src/assets/manifest/${targetEnv}.json`)
.pipe(
jsonMerge({
fileName: 'manifest.json',
edit: (parsedJson, file) => {
parsedJson.version = require('./package.json').version;
parsedJson.chrome_settings_overrides.search_provider.suggest_url = parsedJson.chrome_settings_overrides.search_provider.suggest_url.replace(
'{client}',
targetEnv
);
return parsedJson;
}
})
)
.pipe(gulpif(isProduction, jsonmin()))
.pipe(dest(distDir));
}
function license() {
let year = '2017';
const currentYear = new Date().getFullYear().toString();
if (year !== currentYear) {
year = `${year}-${currentYear}`;
}
const notice = `Search on Google US
Copyright (c) ${year} Armin Sebastian
This software is released under the terms of the GNU General Public License v3.0.
See the LICENSE file for further information.
`;
writeFileSync(path.join(distDir, 'NOTICE'), notice);
return src('LICENSE').pipe(dest(distDir));
}
function zip(done) {
exec(
`web-ext build -s dist/${targetEnv} -a artifacts/${targetEnv} -n "{name}-{version}-${targetEnv}.zip" --overwrite-dest`,
function (err, stdout, stderr) {
console.log(stdout);
console.log(stderr);
done(err);
}
);
}
exports.build = series(init, parallel(images, locale, manifest, license));
exports.zip = zip;