-
Notifications
You must be signed in to change notification settings - Fork 23
/
index.js
81 lines (70 loc) · 2.49 KB
/
index.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
var fs = require('fs');
var path = require('path');
var join = path.join;
var mkdirp = require('mkdirp');
var ChromeExtension = require('crx');
function Plugin(options) {
this.options = options || {};
if (!this.options.updateUrl) {
this.options.updateUrl = "http://localhost:8000/";
}
if (!this.options.updateFilename) {
this.options.updateFilename = "updates.xml";
}
// remove trailing slash
this.options.updateUrl = this.options.updateUrl.replace(/\/$/, "");
// setup paths
this.context = path.dirname(module.parent.filename);
this.keyFile = path.isAbsolute(this.options.keyFile) ? this.options.keyFile : join(this.context, this.options.keyFile);
this.outputPath = path.isAbsolute(this.options.outputPath) ? this.options.outputPath : join(this.context, this.options.outputPath);
this.contentPath = path.isAbsolute(this.options.contentPath) ? this.options.contentPath : join(this.context, this.options.contentPath);
// set output info
this.crxName = this.options.name + ".crx";
this.crxFile = join(this.outputPath, this.crxName);
this.updateFile = join(this.outputPath, this.options.updateFilename);
this.updateUrl = this.options.updateUrl + "/" + this.options.updateFilename;
// initiate crx
this.crx = new ChromeExtension({
privateKey: fs.readFileSync(this.keyFile),
codebase: this.options.updateUrl + '/' + this.crxName
});
}
// hook into webpack
Plugin.prototype.apply = function(compiler) {
var self = this;
self.logger = compiler.getInfrastructureLogger('crx-webpack-plugin');
return compiler.plugin('done', function() {
self.package.call(self);
});
}
// package the extension
Plugin.prototype.package = function() {
var self = this;
self.crx.load(self.contentPath).then(function() {
self.crx.pack().then(function(buffer) {
mkdirp(self.outputPath, function(err) {
if (err)
{
self.logger.error(err);
throw err
}
var updateXML = self.crx.generateUpdateXML();
fs.writeFile(self.updateFile, updateXML, function(err) {
if (err) {
self.logger.error(err);
throw err
}
self.logger.info('wrote updateFile to ' + self.updateFile);
fs.writeFile(self.crxFile, buffer, function(err) {
if (err) {
self.logger.error(err);
throw err;
}
self.logger.info('wrote crxFile to ' + self.crxFile);
});
});
});
});
});
}
module.exports = Plugin;