-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
83 lines (75 loc) · 2.56 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
const JSZip = require('./dev_modules/jszip.min.js');
const fs = require('fs');
const path = require('path');
const args = process.argv;
// handle fx flag
let fxFlag = false;
const manifestPath = './src/manifest.json';
if (args.length > 3) {
printUsage();
} else if (args.length === 3) {
switch (args[2]) {
case 'firefox':
fxFlag = true;
break;
case 'chromium':
fxFlag = false;
break;
default:
printUsage();
}
}
fs.copyFileSync(manifestPath, manifestPath + '.bk');
const manifest = JSON.parse(fs.readFileSync(manifestPath, {encoding: 'utf8'}));
if (fxFlag === true) {
// remove permission *management*
manifest.permissions.splice(manifest.permissions.indexOf('management', 1));
fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 4));
}else{
// remove firefox entry
delete manifest.browser_specific_settings;
}
// archive
let zip = new JSZip();
let ignoreRules = ['.git', 'dev_modules', 'build.js', 'manifest.json.bk', 'MyToolBox.zip', 'MyToolBox.fx.zip'];
const archiveRoot = path.join(process.cwd(), 'src');
archive('', zip);
zip.generateAsync({ type: 'nodebuffer' }).then(data => {
if (fxFlag) {
fs.writeFileSync('MyToolBox.fx.zip', data);
fs.unlinkSync(manifestPath);
fs.renameSync(manifestPath + '.bk', manifestPath);
} else {
fs.writeFileSync('MyToolBox.zip', data);
}
});
function printUsage() {
console.log(
'Usage:\n node build.js chromium "build chromium extension"\n node build.js firefox "build firefox addon"'
);
process.exit(1);
}
function archive(relPath, zip) {
const fullPath = path.join(archiveRoot, relPath);
const files = fs.readdirSync(fullPath, { withFileTypes: true });
itr_files: for (item of files) {
// filter files by ignoreRules
for (rule of ignoreRules) {
if (item.name.match(rule)) continue itr_files;
}
// recursive iteration
const newFullPath = path.join(fullPath, item.name);
const newRelPath = path.join(relPath, item.name);
if (item.isDirectory()) {
archive(newRelPath, zip);
} else if (item.isFile()) {
// use *nix style path for better compatibility
const unixRelPath = newRelPath.replace(/\\/g, '/');
const data = fs.readFileSync(newFullPath);
zip.file(unixRelPath, data);
console.log('added file to: ' + unixRelPath);
} else {
console.warn(newFullPath, ' is neither a dir nor a file.');
}
}
}