This repository has been archived by the owner on Feb 28, 2022. It is now read-only.
/
build.js
162 lines (143 loc) · 4.75 KB
/
build.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
#!/usr/bin/env node
/* eslint-disable no-console */
const fs = require("fs");
const { exec, execSync } = require("child_process");
const rimraf = require("rimraf");
// Args
const [,, ...args] = process.argv;
// Files to copy
let files = {
".": ["options.html"],
"third_party": [
"simpleNotification.min.css",
"simpleNotification.min.js"
],
"scripts": {
".": ["defaultOptions.js"],
"minified": [
"myMangaDex.js",
"optionsManager.js",
"sharedFunctions.js"
]
},
"icons": ["128.png", "96.png", "48.png"],
"css": ["mymangadex.css", "options.css"]
};
// What browser to bundle for
let browser;
if (args[0] == "firefox" || args[0] == "f") {
browser = "firefox";
} else if (args[0] == "chrome" || args[0] == "c") {
browser = "chrome";
files.third_party.push("chrome-extension-async.js");
}
// Options
let minify = true;
let webExt = true;
// If we don't minify we don't use the "minified" subfolder for scripts
if (args.includes("--no-minify")) {
minify = false;
files.scripts = {
".": [
"defaultOptions.js",
"myMangaDex.js",
"optionsManager.js",
"sharedFunctions.js",
]
};
}
// Don't build the web-ext artifact
if (args.includes("--no-web-ext")) {
webExt = false;
}
// Used to merge manifests
function deepMerge(first, toMerge) {
for (let property in toMerge) {
if (first[property] !== undefined && Array.isArray(first[property])) {
toMerge[property].forEach((value, key) => {
if (typeof value === "object") {
deepMerge(first[property][key], value);
} else {
first[property].push(value);
}
});
} else if (first[property] !== undefined && typeof first[property] === "object") {
deepMerge(first[property], toMerge[property]);
} else {
first[property] = toMerge[property];
}
}
}
// Used to import files
function deepFileCopy(files, destFolder, baseFolder="") {
let currentBase = baseFolder;
let currentDest = destFolder;
for (let file in files) {
if (file == ".") {
currentBase = baseFolder;
currentDest = destFolder;
} else {
currentBase = baseFolder + file + "/";
currentDest = destFolder + file + "/";
}
if (Array.isArray(files[file])) {
if (!fs.existsSync(currentDest)) {
fs.mkdirSync(currentDest);
}
files[file].forEach(element => {
console.log("Copying '%s' into '%s'", currentBase + element, currentDest + element);
fs.copyFileSync(currentBase + element, currentDest + element);
});
} else if (typeof files[file] === "object") {
deepFileCopy(files[file], currentDest, currentBase);
}
}
}
if (["firefox", "chrome"].includes(browser)) {
// Create temp folder for the bundle
console.log("Creating temp directory");
let makeFolder = browser + "Build";
if (fs.existsSync(makeFolder)) {
rimraf.sync(makeFolder);
}
fs.mkdirSync(makeFolder);
// Merge manifests
console.log("Merging manifests");
let mainManifest = fs.readFileSync("manifests/manifest.json", "utf-8");
mainManifest = JSON.parse(mainManifest);
let browserManifest = fs.readFileSync("manifests/" + browser + ".json");
browserManifest = JSON.parse(browserManifest);
deepMerge(mainManifest, browserManifest);
console.log("Building version %s", mainManifest.version);
// Write new manifest
let bundleManifestStream = fs.createWriteStream(makeFolder + "/manifest.json", {flags: "w+"});
bundleManifestStream.write(JSON.stringify(mainManifest));
bundleManifestStream.cork();
bundleManifestStream.end();
// Minify script
if (minify) {
console.log("Minifying scripts");
execSync("minify");
}
// Copy files
console.log("Copying files");
deepFileCopy(files, makeFolder + "/", "");
if (webExt) {
exec("web-ext build", {cwd: makeFolder}, (error, stdout, stderr) => {
if (error) {
console.error(`Build error: ${error}`);
return;
}
if (!fs.existsSync("builds")) {
fs.mkdirSync("builds");
}
console.log("Moving zip archive to 'builds'");
fs.renameSync(makeFolder + "/web-ext-artifacts/mymangadex-" + mainManifest.version + ".zip", "builds/mymangadex-" + mainManifest.version + "_" + browser + ".zip");
console.log("Done");
});
} else {
console.log("Done");
}
} else {
console.error("Unrecognized browser to bundle for.");
}