forked from SunriseFox/webpack-web-ext-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
125 lines (111 loc) · 3.54 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
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
117
118
119
120
121
122
123
124
125
const webExt = require("web-ext").default;
const inquirer = require("inquirer");
const pluginName = "web-ext-plugin";
class WebExtWebpackPlugin {
constructor(CmdRunParams = {}, CmdRunOptions = {}, PluginOptions = {}) {
this.logger = console.log.bind(console);
this.CmdRunParams = { ...CmdRunParams, noReload: true, noInput: true };
this.CmdRunOptions = { ...CmdRunOptions, shouldExitProgram: false };
this.verbose = PluginOptions.verbose || false;
this.timeout = PluginOptions.timeout || 5000;
}
androidSetup() {
let shouldTimeoutReject = true;
this.androidSetupPromise = new Promise((resolve, reject) => {
setTimeout(
() =>
shouldTimeoutReject && reject(new Error("No android device found.")),
this.timeout
);
webExt.util.logger.consoleStream.write = (...args) => {
const raw = args[0].msg;
const message = (raw || "").trim().split("\n");
if (message[0] !== "Android devices found:") {
return this.verbose && this.logger(raw);
}
shouldTimeoutReject = false;
const devices = message.slice(1).map((s) => s.substr(3));
if (devices.length === 1) {
this.logger("Selecting the only device " + devices[0]);
this.CmdRunParams.adbDevice = devices[0];
return resolve();
}
inquirer
.prompt({
type: "list",
choices: devices,
name: "device",
message: "Select one device to continue",
})
.then((answer) => {
webExt.util.logger.consoleStream.write = (e) =>
this.verbose && this.logger(e);
this.CmdRunParams.adbDevice = answer.device;
})
.then(resolve);
};
});
}
async androidQueryDevice() {
if (this.CmdRunParams.adbDevice) return;
await webExt.cmd.run(this.CmdRunParams, this.CmdRunOptions).catch(() => {});
await this.androidSetupPromise.catch((e) => {
this.error(e);
throw e;
});
}
async watchRun() {
this.watchMode = true;
}
async afterEmit() {
if (!this.watchMode) return;
if (this.runner) {
this.logger("Webpack emitted, reloading...");
let reloadOK = await Promise.race([
this.runner
.reloadAllExtensions()
.then(() => true)
.catch(this.error),
new Promise((resolve) => setTimeout(resolve, this.timeout, false)),
]);
if (reloadOK) {
this.logger("Extension reloaded.");
return;
}
}
const runner = await webExt.cmd
.run(this.CmdRunParams, this.CmdRunOptions)
.catch(this.error);
this.runner = runner;
// Fix web-ext's bad
process.stdin.setRawMode(false);
runner.registerCleanup(() => {
this.runner = null;
});
}
apply(compiler) {
const logger = compiler.getInfrastructureLogger(pluginName);
this.logger = logger.info.bind(logger);
this.error = logger.error.bind(logger);
if (
this.CmdRunParams.target === "firefox-android" &&
!this.CmdRunParams.adbDevice
) {
this.androidSetup.bind(this)();
this.androidQueryDeviceResult = this.androidQueryDevice.bind(this)();
compiler.hooks.beforeCompile.tapPromise(
{ name: pluginName },
() => this.androidQueryDeviceResult
);
}
compiler.hooks.done.tapPromise(
{ name: pluginName },
this.afterEmit.bind(this)
);
compiler.hooks.watchRun.tapPromise(
{ name: pluginName },
this.watchRun.bind(this)
);
}
}
module.exports = WebExtWebpackPlugin;