-
Notifications
You must be signed in to change notification settings - Fork 5
/
build.js
116 lines (110 loc) · 3.03 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
const fs = require("fs");
const path = require("path");
const ncp = require("ncp").ncp;
const formatJSON = require("json-format");
const rimraf = require("rimraf");
const JSZip = require("jszip");
const args = process.argv;
const isFirefox = args.includes("firefox");
const isZip = args.includes("zip");
const chromePath = path.join(__dirname, "chrome");
const firefoxPath = path.join(__dirname, "firefox");
const buildPath = isFirefox ? firefoxPath : chromePath;
const src = path.join(__dirname, "src");
ncp.limit = 16;
// convert callback to promises
const promisify = (fn, args) => {
return new Promise((resolve, reject) => {
try {
args.push((...arg) => resolve(arg));
fn.apply(this, args);
} catch (e) {
reject(e);
}
});
};
// remove existing directory
promisify(rimraf, [buildPath])
.then(() => {
// make directory
return promisify(fs.mkdir, [buildPath]);
})
.then(() => {
// copy all files/folders recursively to destination
return promisify(ncp, [src, buildPath]);
})
.then(() => {
// read manifest.json
return promisify(fs.readFile, [
path.join(buildPath, "manifest.json"),
"utf8"
]);
})
.then(res => {
// modify JSON if firefox
if (res[0]) throw Error("unable to read manifest");
const fileJson = JSON.parse(res[1]);
if (isFirefox) {
fileJson.applications = {
gecko: {
id: "permissionInspector@web-ext-labs",
strict_min_version: "55.0"
}
};
}
return promisify(fs.writeFile, [
path.join(buildPath, "manifest.json"),
formatJSON(fileJson)
]);
})
.then(() => {
// delete chrome/firefox as per build
const toDelete = path.join(
buildPath,
isFirefox ? "chrome.js" : "firefox.js"
);
return promisify(fs.unlink, [toDelete]);
})
.then(() => {
// read index.html
return promisify(fs.readFile, [path.join(buildPath, "index.html"), "utf8"]);
})
.then(res => {
// remove chrome/firefox script as per build
if (res[0]) throw Error("unable to read index file");
const lines = res[1].split("\n");
const matcher = isFirefox ? "chrome" : "firefox";
const finalArr = lines.filter(l => !l.includes(matcher));
return promisify(fs.writeFile, [
path.join(buildPath, "index.html"),
finalArr.join("\n")
]);
}).then(() => {
return promisify(fs.unlink, [`${buildPath}.zip`])
})
.then(() => {
if (isZip) {
const zip = new JSZip();
const build = isFirefox ? "firefox" : "chrome";
const readAll = function (dir, filelist = []) {
fs.readdirSync(dir).forEach(function (file) {
if (fs.statSync(path.join(dir, file)).isDirectory())
filelist = readAll(path.join(dir, file), filelist);
else
filelist.push(path.join(dir, file));
});
return filelist;
};
readAll(build).forEach(f =>
zip.file(f.replace(`${build}`, ""), fs.readFileSync(f))
);
zip
.generateNodeStream({ type: "nodebuffer", streamFiles: true })
.pipe(fs.createWriteStream(`${build}.zip`))
.on("finish", () => {
console.log(`${build}.zip written`);
promisify(rimraf, [buildPath]);
});
}
})
.catch(e => console.log(`Schaize: ${e}`));