This repository has been archived by the owner on Jul 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathgulpfile.js
119 lines (99 loc) · 3.14 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
/*
* This Source Code is subject to the terms of the Mozilla Public License
* version 2.0 (the "License"). You can obtain a copy of the License at
* http://mozilla.org/MPL/2.0/.
*/
"use strict";
import fs from "fs";
import path from "path";
import del from "del";
import gulp from "gulp";
import eslint from "gulp-eslint";
import zip from "gulp-zip";
import * as utils from "./gulp-utils.js";
const VERSION = JSON.parse(fs.readFileSync("./manifest.json")).version;
const sources = ["manifest.json", "data/**/*", "_locales/**/*", "icon*.png", "LICENSE.txt"];
function getBuildFileName(extension)
{
let filename = utils.readArg("--outfile=");
if (!filename)
{
filename = "searchlinkfix-" + VERSION + "." + extension;
}
let dir = "";
if (path.isAbsolute(filename))
{
dir = path.dirname(filename);
filename = path.basename(filename);
}
return [dir, filename];
}
function modifyManifest(modifier)
{
return utils.transform((filepath, contents) =>
{
let manifest = JSON.parse(contents);
manifest = modifier(manifest) || manifest;
return utils.download("https://www.google.com/supported_domains").then(data =>
{
let additionalDomains = data.trim().split(/\s+/).map(domain => `*://*${domain}/*`);
additionalDomains.sort();
manifest.content_scripts[0].matches.unshift(...additionalDomains);
return [filepath, JSON.stringify(manifest, null, 2)];
});
}, {files: ["manifest.json"]});
}
function modifyCRXManifest(manifestData)
{
delete manifestData.applications;
}
function buildZIP(filename, manifestModifier)
{
return gulp.src(sources, {cwdbase: true})
.pipe(modifyManifest(manifestModifier))
.pipe(zip(filename));
}
gulp.task("eslint", function()
{
return gulp.src(["*.js", "data/**/*.js", "testhelper/**/*.js", "tests/**/*.js"])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
gulp.task("validate", gulp.parallel("eslint"));
gulp.task("xpi", gulp.series("validate", function buildXPI()
{
let [dir, filename] = getBuildFileName("xpi");
return buildZIP(filename, function(manifestData)
{
delete manifestData.minimum_chrome_version;
delete manifestData.minimum_opera_version;
}).pipe(gulp.dest(dir || process.cwd()));
}));
gulp.task("crx", gulp.series("validate", function buildCRX()
{
let [dir, filename] = getBuildFileName("zip");
return buildZIP(filename, modifyCRXManifest).pipe(gulp.dest(dir || process.cwd()));
}));
gulp.task("unpacked-crx", gulp.series("validate", function buildUnpackedCRX()
{
return gulp.src(sources, {cwdbase: true})
.pipe(modifyManifest(modifyCRXManifest))
.pipe(gulp.dest("crx-unpacked"));
}));
gulp.task("test", gulp.series("unpacked-crx", function runTests()
{
let testFile = utils.readArg("--test=");
if (!testFile)
testFile = "**/*.js";
else if (!testFile.endsWith(".js"))
testFile += ".js";
return gulp.src("test/" + testFile)
.pipe(utils.runTests());
}));
gulp.task("clean", function()
{
return del(["crx-unpacked", "*.xpi", "*.zip", "*.crx"]);
});
gulp.task("all", gulp.parallel("xpi", "crx"));
gulp.task("default", gulp.parallel("all"));