forked from rpl/webpack-webext-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
82 lines (72 loc) · 2.15 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
82
"use strict";
var path = require('path');
var spawn = require('child_process').spawn;
function WebpackWebExt(opts) {
this.argv = opts.argv;
this.maxRetries = opts.maxRetries;
this.retries = 0;
this.runOnce = "runOnce" in opts ? opts.runOnce : true;
this.webExtPath = opts.webExtPath;
}
module.exports = WebpackWebExt;
WebpackWebExt.prototype.runWebExtCommand = function() {
var retry = res => {
if (res instanceof Error) {
console.error("WebExtRun: command error", this.argv, " - ", res);
} else {
console.log("WebExtRun: command exited", this.argv);
}
if (this.retries < this.maxRetries) {
this.retries += 1;
console.log("WebExtRun: retry", this.retries);
return this.runWebExtCommand();
} else {
if (this.maxRetries > 0) {
var err = new Error("WebExtRun: Max Retries reached.");
return Promise.reject(err);
} else {
return (res instanceof Error) ? Promise.reject(err) : Promise.resolve();
}
}
};
return new Promise((resolve, reject) => {
const webExtChild = spawn(this.webExtPath || "web-ext",
this.argv);
webExtChild.stdout.on('data', (data) => {
printLogLines("WebExtRun - stdout:", data);
});
webExtChild.stderr.on('data', (data) => {
printLogLines("WebExtRun - stderr:", data, true);
});
webExtChild.on('close', (code) => {
if (code !== 0) {
reject(new Error(`WebExtRun: child process exited with code ${code}`));
} else {
resolve();
}
});
}).then(retry, retry).catch((err) => {
console.error(err);
});
};
WebpackWebExt.prototype.apply = function(compiler) {
compiler.plugin('emit', (compilation, callback) => {
if (this.runOnce && !this.webExtRunPromise) {
this.webExtRunPromise = this.runWebExtCommand();
} else if (!this.webExtRunPromise){
this.runWebExtCommand();
}
callback();
});
};
function printLogLines(prefix, data, isError) {
var lines = String(data).split("\n");
for (var line of lines) {
var msg = `${prefix} ${line}`;
if (isError) {
console.error(msg);
} {
console.log(msg);
}
}
}