-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgulpfile.js
150 lines (130 loc) · 4.78 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
/* eslint-disable no-undef */
const{src, dest, series} = require("gulp");
const mergeJson = require("gulp-merge-json");
const rename = require("gulp-rename");
const fs = require("fs-extra");
const{join} = require("path");
const{exec} = require("child_process");
const outputDir = "dist";
const safariOutputDir = "xcode/Turn-Off-the-Lights-Safari-extension/Shared (Extension)/Resources";
const packageDef = require("./package.json");
const buildExtension = (browser) => {
// Set destination based on browser type
const destination = browser === "safari" ? safariOutputDir : `${outputDir}/${browser}`;
// Manifest file
const commonManifest = "./src/manifests/common.json";
const browserManifest = `./src/manifests/${browser}.json`;
// Helper function to merge content_scripts
const mergeContentScripts = (commonScripts, browserScripts) => {
const mergedScripts = [...commonScripts];
browserScripts.forEach((browserScript) => {
const matchExists = commonScripts.some((commonScript) =>
JSON.stringify(commonScript.matches) === JSON.stringify(browserScript.matches) &&
JSON.stringify(commonScript.js || []) === JSON.stringify(browserScript.js || []) &&
JSON.stringify(commonScript.css || []) === JSON.stringify(browserScript.css || [])
);
if(!matchExists){
mergedScripts.push(browserScript);
}
});
return mergedScripts;
};
// Get the version number from package.json and add it to manifest.json
src([commonManifest, browserManifest])
.pipe(mergeJson({
fileName: "manifest.json",
edit: (json) => {
json.version = packageDef.version;
// Merge content_scripts if present
if(json.content_scripts && Array.isArray(json.content_scripts)){
const commonContentScripts = require(commonManifest).content_scripts || [];
const browserContentScripts = require(browserManifest).content_scripts || [];
json.content_scripts = mergeContentScripts(commonContentScripts, browserContentScripts);
}
return json;
}
}))
.pipe(dest(`${destination}/`));
// Constants file
const browserConstantFile = `./src/constants/${browser}/constants.js`;
const destinationConstantFile = join(destination, "scripts", "constants.js");
fs.mkdirSync(join(destination, "scripts"), {recursive: true});
fs.copyFileSync(browserConstantFile, destinationConstantFile);
// HTML, scripts, styles, and other necessary files
const sourceFiles = src([
"./src/schema.json",
"!**/index.js",
"./src/LICENSE",
"./src/**/*.html",
"./src/scripts/**/*.js",
"./src/styles/**/*.css"
]);
const processedFiles = sourceFiles.pipe(rename((file) => {
if(file.extname === ".html" || file.basename === "LICENSE"){
file.dirname = "";
}else if(file.dirname.includes("styles") || file.dirname.includes("scripts")){
file.dirname = file.dirname.split("/").pop();
}
}));
processedFiles.pipe(dest(destination));
// Images
const imageExtensions = ["png", "webp", "svg", "webm", "mp4"];
imageExtensions.forEach((extension) => {
const files = fs.readdirSync("./src/images", {withFileTypes: true});
files.forEach((file) => {
if(file.isFile() && file.name.endsWith(`.${extension}`)){
const destPath = join(destination, "images", file.name);
fs.ensureDirSync(join(destination, "images"));
fs.copySync(join("./src/images", file.name), destPath);
}
});
});
// Localization files
const commonFiles = src(["./src/_locales/**/*.json"])
.pipe(dest(`${destination}/_locales`));
return commonFiles;
};
// Helper function to run npm scripts for zipping
const runZipCommand = (browser) => {
return(cb) => {
exec(`npm run ${browser}-create-zip`, (err, stdout, stderr) => {
if(err){
console.error(`Error during ${browser} zipping:`, stderr);
}else{
console.log(`Zipped ${browser} successfully:`, stdout);
}
cb(err);
});
};
};
// Build tasks for each browser
exports.chrome = () => buildExtension("chrome");
exports.firefox = () => buildExtension("firefox");
exports.opera = () => buildExtension("opera");
exports.edge = () => buildExtension("edge");
exports.safari = () => buildExtension("safari");
exports.whale = () => buildExtension("whale");
exports.samsung = () => buildExtension("samsung");
// Zipping tasks for each browser
exports["chrome-zip"] = runZipCommand("chrome");
exports["firefox-zip"] = runZipCommand("firefox");
exports["opera-zip"] = runZipCommand("opera");
exports["edge-zip"] = runZipCommand("edge");
exports["whale-zip"] = runZipCommand("whale");
exports["samsung-zip"] = runZipCommand("samsung");
// Main task to build and zip all browsers sequentially
exports.browserzip = series(
exports.chrome,
exports["chrome-zip"],
exports.firefox,
exports["firefox-zip"],
exports.opera,
exports["opera-zip"],
exports.edge,
exports["edge-zip"],
exports.whale,
exports["whale-zip"],
exports.samsung,
exports["samsung-zip"],
exports.safari,
);