forked from EhTagTranslation/EhSyringe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crx-packet.js
58 lines (53 loc) · 1.98 KB
/
crx-packet.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
const fs = require("fs");
const path = require("path");
const keypair = require('keypair');
const ChromeExtension = require("crx");
class CrxPacket {
constructor(options) {
this.options = options || {};
this.tempKey = path.resolve(__dirname, "tempKey.pem");
this.outputFileName = `${options.name || "packed"}.crx`;
if (this.options.key) {
this.key = path.resolve(__dirname, options.key);
} else {
const pair = keypair();
fs.writeFileSync(this.tempKey, pair.private);
this.key = this.tempKey;
}
this.src = path.resolve(__dirname, this.options.src);
this.dest = path.resolve(__dirname, this.options.dest);
this.updateFile = path.resolve(__dirname, this.dest, options.updateFile || "update.xml");
this.crxFile = path.resolve(__dirname, path.join(this.dest, this.outputFileName));
this.version = options.version || 3;
}
apply(compiler) {
compiler.hooks.done.tap('CrxPacket', compilation => {
try {
this.pack();
}catch (err) {
console.error(err);
}
});
}
async pack() {
const privateKey = fs.readFileSync(this.key, 'utf8');
console.log('privateKey', privateKey);
const crx = new ChromeExtension({
codebase: `http://localhost:8000/${this.outputFileName}`,
privateKey: privateKey,
version: this.version
});
const crxBuffer = await crx.load(this.src).then(crx => crx.pack());
const updateXML = crx.generateUpdateXML();
if (!fs.existsSync(this.dest)) {
fs.mkdirSync(this.dest);
}
fs.writeFileSync(this.updateFile, updateXML);
console.info(`Saving CRX file to ${this.crxFile}`);
fs.writeFileSync(this.crxFile, crxBuffer);
if (fs.existsSync(this.tempKey)) {
fs.unlinkSync(this.tempKey);
}
}
}
module.exports = CrxPacket;