This repository has been archived by the owner on Apr 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
pack.js
85 lines (71 loc) · 2.22 KB
/
pack.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
const { execSync } = require('child_process');
const { existsSync, unlinkSync, readFileSync } = require('fs');
const { resolve } = require('path');
const AdmZip = require('adm-zip');
const from = 'dist';
const outputTo = 'sanitizer.zip';
const optionsBundle = 'options.js';
const pageBundle = 'sanitizer.js';
/**
* Checks if regeneratorRuntime present in the code
* @param {string} pathToFile path to js file to be checked
*/
function checkRegenrationRuntime(pathToFile) {
process.stdout.write(`start checking presence of the regenration runtime for ${pathToFile}...`);
const isFileExists = existsSync(pathToFile);
if (!isFileExists) {
throw new Error(`File ${pathToFile} not exists`);
}
const data = readFileSync(pathToFile, { encoding: 'utf-8' });
const isRegenrationRuntimeExists = /regeneratorRuntime/i.test(data);
if (isRegenrationRuntimeExists) {
throw new Error('Regeneration runtime found in build, aborting');
}
process.stdout.write('OK \n');
}
/**
* Removes specified folder
* @param {string} output Path to folder to clean
*/
function cleanup(output) {
console.log('starting cleanup');
if (existsSync(output)) {
unlinkSync(output);
console.log('cleanup is done');
} else {
console.log('nothing to cleanup');
}
}
function buildCode() {
console.log('start building');
execSync(`npm run build`);
console.log('building is done');
}
/**
* Packs files into archive for publishing
* @param {{from:string, to: string, filesToCheck:string[]}} p
*/
function pack(from, to) {
console.log('start packing');
var zip = new AdmZip();
zip.addLocalFolder(from);
zip.writeZip(to, (err) => {
if (err) {
console.log('something went wrong: ', err);
} else {
console.log('packing is done');
console.log(`don't forget to check in Chrome and FireFox :)`);
}
});
}
/**
* Tests final build
* @param {string[]} filesToCheck Files to be checked
*/
function testBuildFiles(filesToCheck) {
filesToCheck.map((fileName) => resolve(from, fileName)).forEach(checkRegenrationRuntime);
}
cleanup(outputTo);
buildCode();
testBuildFiles([optionsBundle, pageBundle]);
pack(from, outputTo);