This repository has been archived by the owner on Aug 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
272 lines (249 loc) · 7.42 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
* Copyright (C) 2018 ExE Boss
*
* 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 <http://www.gnu.org/licenses/>.
*/
"use strict"; // eslint-disable-line
/* eslint-env node */
const fse = require("fs-extra");
const path = require("path");
const gulp = require("gulp");
const mergeStream = require("merge-stream");
const {default: webExt} = require("web-ext");
const eslint = require("gulp-eslint");
const jsonEdit = require("gulp-json-editor");
const pkgJson = require("./package.json");
const replace = require("gulp-replace");
const hyperHTML = require("viperhtml");
const args = require("yargs")
.option("firefox", {
alias: "f",
description: "The path to the Firefox executable",
requiresArg: true,
type: "string",
})
.alias("help", ["h", "?"])
.alias("version", "v")
.argv;
const testPrefs = require("./.extprefrc.js");
/* Building */
const ARCHIVES_DIR = "./dist/";
const BUILD_DIR = "./build/";
const SOURCE_DIR = "./src/";
const VENDOR_BUILD_DIR = `${BUILD_DIR}vendor/`;
const VENDOR_SOURCE_DIR = "./node_modules/";
gulp.task("clean", () => {
try {
fse.emptyDirSync(BUILD_DIR);
} catch (e) {
console.error(e);
}
});
{
/** @typedef {object} Vendor
* @property {string} [src]
* @property {string} [file]
*/
/** @type {{[name: string]: Vendor}} */
const vendorData = {
};
/**
* @param {string} name
* @return {{dest:string,file:string,src:string}}
*/
const transformPackageInternal = (name) => {
const path = name.split("/");
const isFile = path[path.length-1].indexOf(".") >= 0;
const vendor = path[0].toLowerCase();
let src = `${VENDOR_SOURCE_DIR}${vendor}/`;
let dest = name;
let file = "index.js";
if (typeof vendorData[vendor] === "object") {
const data = vendorData[vendor];
if ("src" in data) {
[dest] = path;
src += `${data.src}/`;
}
if ("srcFiles" in data) {
[dest] = path;
src = data.srcFiles.map(v =>`${src}${v}`);
} else {
src += "**";
}
if ("file" in data) {
({file} = data);
}
}
return {
dest: `${VENDOR_BUILD_DIR}${path[0]}/`,
file: isFile ? dest : `/vendor/${dest}/${file}`,
src: src,
};
};
/**
* @param {string} name The package name.
* @return {string} The transformed path as a JS file.
*/
const transformPackage = (name) => {
name = name.replace(/\\/g,"/");
// Test if the path is a relative or absolute URI module specifier
if (name.startsWith(".") || name.startsWith("/") ||
/^[a-zA-Z0-9.+-]:/.test(name)) {
return name;
}
const {file} = transformPackageInternal(name);
return file;
};
const transformPackageRegexp = /^(import[ \t]+(?:(?:\{[^}]+\} |.*[ \t]+)?from[ \t]+)?")((?:[^./"][^"]*)?)(";?(?:[ \t]*\/\/.*)?)$/mg;
/**
* @param {string} match The matched substring
* @param {string} prefix The import statement (eg. `import {…} from "`)
* @param {string} name The name of the package
* @param {string} suffix The end of the import statement (eg. `";` and optionally a comment)
* @return {string}
*/
const transformPackageCallback = (match, prefix, name, suffix) =>
`${prefix}${transformPackage(name)}${suffix}`;
module.exports.transformPackage = Object.assign((name) => transformPackage(name), {
internal: transformPackageInternal,
regexp: transformPackageRegexp,
callback: transformPackageCallback,
});
const VENDOR_HEADER = "| Folder\t| Version\t| License\t| NPM ID\t|\n| ------\t| -------\t| -------\t| ------\t|";
const vendorToLine = (vendor) => {
try {
const pkg = require(`${vendor}/package.json`);
return `\n| ${vendor}\t| ${pkg.version}\t| ${pkg.license}\t| [\`${pkg.name}\`](https://https://www.npmjs.com/package/${pkg.name})\t|`;
} catch (_) {
return "";
}
};
/**
* @param {string[]} vendors The vendors
* @return {IMergedStream} The stream
*/
// eslint-disable-next-line
const copyVendors = (...vendors) => {
return mergeStream([
...(vendors.map(vendor => {
const {dest, src} = transformPackageInternal(vendor);
return gulp.src(src)
.pipe(gulp.dest(dest));
})),
gulp.src([
`${SOURCE_DIR}vendor/README.md`,
], {dot: true})
.pipe(replace(
/^{#vendorfiles}$/mg,
() => {
return VENDOR_HEADER + vendors.map(vendorToLine).join("");
}
))
.pipe(gulp.dest(`${BUILD_DIR}vendor`)),
]);
};
const build = () => {
const animationStopReload_style = String(fse.readFileSync(`${SOURCE_DIR}icons/style/transition.css`));
const animationStopReload_script = String(fse.readFileSync(`${SOURCE_DIR}icons/style/transition.js`));
const animationStopReload = hyperHTML.wire(null, "svg")`
<style type="text/css">
${animationStopReload_style}
</style>
<script type="application/javacript">
${animationStopReload_script}
</script>`;
return mergeStream(
gulp.src([
`${SOURCE_DIR}**`,
`!${SOURCE_DIR}**/*.js`,
`!${SOURCE_DIR}icons/**`,
`!${SOURCE_DIR}manifest.json`,
`!${SOURCE_DIR}vendor/README.md`,
], {dot: true})
.pipe(gulp.dest(BUILD_DIR)),
gulp.src([
`${SOURCE_DIR}**/*.js`,
`!${SOURCE_DIR}icons/style/*`,
], {dot: true})
.pipe(replace(
transformPackageRegexp,
transformPackageCallback,
))
.pipe(gulp.dest(BUILD_DIR)),
gulp.src([
`${SOURCE_DIR}icons/**`,
`!${SOURCE_DIR}icons/style`,
`!${SOURCE_DIR}icons/style/*`,
], {dot: true})
.pipe(replace(/<\?xml-stylesheet[^>]*\?>\n?/g, ""))
.pipe(replace(/<script(?: type="[^"]+")? href="\.\.\/style\/transition.js"\/>/, animationStopReload))
.pipe(gulp.dest(`${BUILD_DIR}icons/`)),
gulp.src(`${SOURCE_DIR}manifest.json`)
.pipe(jsonEdit({
version: pkgJson.version,
}))
.pipe(gulp.dest(BUILD_DIR)),
);
};
gulp.task("build-unclean", build);
gulp.task("build", ["clean"], build);
}
gulp.task("lint", ["build"], () => {
webExt.cmd.lint({
sourceDir: BUILD_DIR,
ignoreFiles: [
"vendor/*",
],
}, {shouldExitProgram: false});
return gulp.src([
"*.js",
".*.js",
`${BUILD_DIR}**/*.js`,
`!${BUILD_DIR}vendor/*`,
])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
gulp.task("dist", ["build"], () => {
webExt.cmd.build({
sourceDir: BUILD_DIR,
artifactsDir: ARCHIVES_DIR,
overwriteDest: true,
}, {shouldExitProgram: false});
});
const parsePrefs = () => {
const webExtPrefs = {};
if (testPrefs && testPrefs.firefox_prefs) {
for (const pref in testPrefs.firefox_prefs) {
webExtPrefs[pref] = testPrefs.firefox_prefs[pref];
}
}
return webExtPrefs;
};
gulp.task("run", ["build"], () => {
let firefox = args.firefox || process.env.WEB_EXT_FIREFOX;
if (typeof firefox === "undefined" || firefox === "aurora") {
firefox = "firefoxdeveloperedition";
}
const watcher = gulp.watch([SOURCE_DIR, `${SOURCE_DIR}**`], ["build-unclean"]);
return webExt.cmd.run({
pref: parsePrefs(),
firefox: firefox,
sourceDir: path.resolve(BUILD_DIR),
browserConsole: true,
}, {shouldExitProgram: true}).then(({extensionRunners: [extRunner]}) => {
extRunner.registerCleanup(() => watcher.end());
});
});