-
Notifications
You must be signed in to change notification settings - Fork 127
/
build.js
381 lines (345 loc) · 13.2 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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
(async () => {
"use strict";
/* eslint-disable no-console */
/* global Func, path */
global.modulePath = __dirname + "/node_modules/";
try {
require("../node.js_Build/funcs");
} catch (e) {
if (e.code !== "MODULE_NOT_FOUND") {
throw e;
}
console.log(e);
console.error("Build script is missing. Please download from https://github.com/Kiuryy/node.js_Build");
process.exit(1);
}
/**
*
* @type {Object}
*/
let packageJson = {};
/**
* Starts building the application
*/
const Build = async () => {
const start = +new Date();
console.log("Building release...\n");
// Prepare
await Promise.all([
Func.cleanPre(),
loadPackageJson(),
updateMinimumChromeVersion(),
eslintCheck()
]);
// Build
await Promise.all([
remoteJs(),
js(),
css(),
img(),
json(),
html()
]);
// Package
await zip();
await Func.cleanPost();
console.log("\nRelease built successfully\t[" + (+new Date() - start) + " ms]");
};
/*
* ################################
* BUILD FUNCTIONS
* ################################
*/
/**
* Read the package.json of the project and parse its JSON content into an object
*
* @returns {Promise}
*/
const loadPackageJson = () => {
return Func.measureTime((resolve) => {
const fs = require("fs");
const rawData = fs.readFileSync("package.json");
const parsedData = JSON.parse(rawData);
if (parsedData) {
packageJson = parsedData;
packageJson.preamble = `(c) ${packageJson.author} under ${packageJson.license}`;
resolve();
} else {
console.error("Could not load package.json");
process.exit(1);
}
}, "Loaded package.json");
};
/**
* Copies the images to the dist directory
*
* @returns {Promise}
*/
const img = () => {
return Func.measureTime(async (resolve) => {
await Func.copy([path.src + "img/**/*"], [path.src + "**/*.xcf", path.src + "img/icon/dev/**"], path.dist, false);
resolve();
}, "Moved image files to dist directory");
};
/**
* Parses the scss files and copies the css files to the dist directory
*
* @returns {Promise}
*/
const css = () => {
return Func.measureTime(async (resolve) => {
await Func.minify([ // parse scss files
path.src + "scss/**/*.scss"
], path.dist, false, packageJson.preamble);
resolve();
}, "Moved css files to dist directory");
};
/**
* Parses the js files and copies them to the dist directory
*
* @returns {Promise}
*/
const js = () => {
return Func.measureTime(async (resolve) => {
await Promise.all([
// concat extension javascripts
Func.concat(
[
path.src + "js/webcomponents-bundle.js",
path.src + "js/lib/jsu.js",
path.src + "js/opts.js",
path.src + "js/helper/**/*.js",
path.src + "js/extension.js"
],
path.tmp + "extension-merged.js"
),
// concat background javascripts
Func.concat(
[
path.src + "js/lib/jsu.js",
path.src + "js/opts.js",
path.src + "js/background/**/*.js",
path.src + "js/background.js",
],
path.tmp + "background-merged.js"
),
// concat settings javascripts
Func.concat(
[
path.src + "js/lib/jsu.js",
path.src + "js/lib/colorpicker.js",
path.src + "js/opts.js",
path.src + "js/helper/i18n.js",
path.src + "js/helper/font.js",
path.src + "js/helper/stylesheet.js",
path.src + "js/helper/template.js",
path.src + "js/helper/utility.js",
path.src + "js/helper/model.js",
path.src + "js/helper/checkbox.js",
path.src + "js/settings/*.js",
path.src + "js/settings.js"
],
path.tmp + "settings-merged.js"
),
// concat newtab javascripts
Func.concat(
[
path.src + "js/lib/jsu.js",
path.src + "js/opts.js",
path.src + "js/helper/i18n.js",
path.src + "js/helper/font.js",
path.src + "js/helper/stylesheet.js",
path.src + "js/helper/utility.js",
path.src + "js/helper/checkbox.js",
path.src + "js/helper/template.js",
path.src + "js/helper/model.js",
path.src + "js/helper/entry.js",
path.src + "js/newtab/*.js",
path.src + "js/newtab.js"
],
path.tmp + "newtab-merged.js"
),
// concat sidepanel javascripts
Func.concat(
[
path.src + "js/lib/jsu.js",
path.src + "js/opts.js",
path.src + "js/sidepanel.js"
],
path.tmp + "sidepanel-merged.js"
),
// concat onboarding javascripts
Func.concat(
[
path.src + "js/lib/jsu.js",
path.src + "js/opts.js",
path.src + "js/helper/i18n.js",
path.src + "js/helper/font.js",
path.src + "js/helper/stylesheet.js",
path.src + "js/helper/template.js",
path.src + "js/helper/utility.js",
path.src + "js/helper/model.js",
path.src + "js/onboarding.js"
],
path.tmp + "onboarding-merged.js"
)
]);
await Func.replace({
[path.tmp + "background-merged.js"]: path.tmp + "background-merged-2.js",
}, [
[/^/, "self.window = self;self.document = {};"],
[/importScripts\([^)]*\);?/mig, ""],
]);
await Func.replace({
[path.tmp + "extension-merged.js"]: path.tmp + "extension.js",
[path.tmp + "background-merged-2.js"]: path.tmp + "background.js",
[path.tmp + "settings-merged.js"]: path.tmp + "settings.js",
[path.tmp + "newtab-merged.js"]: path.tmp + "newtab.js",
[path.tmp + "sidepanel-merged.js"]: path.tmp + "sidepanel.js",
[path.tmp + "onboarding-merged.js"]: path.tmp + "onboarding.js"
}, [
[/importScripts\([^)]*\);?/mig, ""],
[/}\)\(jsu\);[\s\S]*?\(\$\s*=>\s*{[\s\S]*?"use strict";/mig, ""]
]);
// delay execution, so the files are created properly before being used to minify
await new Promise((rslv) => {
setTimeout(rslv, 1000);
});
await Promise.all([
Func.minify([
path.tmp + "extension.js",
path.tmp + "settings.js",
path.tmp + "background.js",
path.tmp + "newtab.js",
path.tmp + "sidepanel.js",
path.tmp + "onboarding.js"
], path.dist + "js/", true, packageJson.preamble),
Func.minify([
path.src + "js/newtab/preflight.js",
], path.dist + "js/newtab/", true, packageJson.preamble),
]);
resolve();
}, "Moved js files to dist directory");
};
/**
* Retrieves the newest versions of the lib js files from Github
*
* @returns {Promise}
*/
const remoteJs = async () => {
const files = [
{
file: "colorpicker.js",
urlPath: "colorpicker.js/master/src/js/colorpicker.js"
},
{
file: "jsu.js",
urlPath: "jsu.js/master/src/js/jsu.js"
}
];
for (const file of files) {
await Func.measureTime(async (resolve) => {
const content = await Func.getRemoteContent("https://raw.githubusercontent.com/Kiuryy/" + file.urlPath);
await Func.createFile(path.src + "js/lib/" + file.file, content);
resolve();
}, "Fetched " + file.file + " from Github");
}
};
/**
* Parses the html files and copies them to the dist directory
*
* @returns {Promise}
*/
const html = () => {
return Func.measureTime(async (resolve) => {
await Func.replace({
[path.src + "html/settings.html"]: path.tmp + "html/settings.html",
[path.src + "html/newtab.html"]: path.tmp + "html/newtab.html",
[path.src + "html/intro.html"]: path.tmp + "html/intro.html"
}, [
[/<\!\-\-\s*\[START\sREMOVE\]\s*\-\->[\s\S]*?<\!\-\-\s*\[END\sREMOVE\]\s*\-\->/mig, ""]
]);
// minify in dist directory
await Func.minify([path.src + "html/**/*.html"], path.dist, false);
await Func.minify([path.tmp + "html/**/*.html"], path.dist + "html/");
resolve();
}, "Moved html files to dist directory");
};
/**
* Generate zip file from dist directory
*
* @returns {Promise}
*/
const zip = () => {
return Func.measureTime(async (resolve) => {
await Func.zipDirectory(path.dist, packageJson.name + "_" + packageJson.version + ".zip");
resolve();
}, "Created zip file from dist directory");
};
/**
* Parses the json files and copies them to the dist directory
*
* @returns {Promise}
*/
const json = () => {
return Func.measureTime(async (resolve) => {
await Func.replace({ // parse manifest.json
[path.src + "manifest.json"]: path.tmp + "manifest.json"
}, [
[/("content_scripts":[\s\S]*?"js":\s?\[)([\s\S]*?)(\])/mig, "$1\"js/extension.js\"$3"],
[/("version":[\s]*")[^"]*("[\s]*,)/ig, "$1" + packageJson.version + "$2"],
[/("version_name":[\s]*")[^"]*("[\s]*,)/ig, "$1" + packageJson.version + "$2"],
[/(img\/icon\/)dev\/(.*\.png)/ig, "$1$2"]
]);
// minify in dist directory
await Func.minify([path.tmp + "manifest.json", path.src + "_locales/**/*.json"], path.dist, false);
resolve();
}, "Moved json files to dist directory");
};
/**
* Updates the mininum Chrome version in the manifest.json to the current version - 10
*
* @returns {Promise}
*/
const updateMinimumChromeVersion = () => {
return Func.measureTime(async (resolve) => {
const content = await Func.getRemoteContent("https://versionhistory.googleapis.com/v1/chrome/platforms/win64/channels/stable/versions");
const result = JSON.parse(content);
const currentVersion = +result.versions[0].version.replace(/(\d+)\..*$/, "$1");
if (!currentVersion) {
console.error("Could not determine current Chrome version");
process.exit(1);
} else {
const minVersion = currentVersion - 10;
await Func.replace({ // update the min version in the manifest
[path.src + "manifest.json"]: path.src + "manifest.json"
}, [
[/("minimum_chrome_version":[\s]*")[^"]*("[\s]*,)/ig, "$1" + (minVersion) + "$2"]
]);
resolve("v" + minVersion);
}
}, "Updated minimum chromium version");
};
/**
* Performs eslint checks for the build and src/js directories
*
* @returns {Promise}
*/
const eslintCheck = async () => {
for (const files of ["build.js", path.src + "js/**/*.js"]) {
await Func.measureTime(async (resolve) => {
Func.cmd("eslint --fix " + files).then((obj) => {
if (obj.stdout && obj.stdout.trim().length > 0) {
console.error(obj.stdout);
process.exit(1);
}
resolve();
});
}, "Performed eslint check for " + files);
}
};
//
//
//
await Build();
})();