-
Notifications
You must be signed in to change notification settings - Fork 364
/
webpack-dashboard.js
executable file
·108 lines (88 loc) · 2.56 KB
/
webpack-dashboard.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
#!/usr/bin/env node
"use strict";
const commander = require("commander");
const spawn = require("cross-spawn");
const Dashboard = require("../dashboard/index");
const SocketIO = require("socket.io");
const DEFAULT_PORT = 9838;
const program = new commander.Command("webpack-dashboard");
const pkg = require("../package.json");
// Wrap up side effects in a script.
// eslint-disable-next-line max-statements, complexity
const main = (module.exports = opts => {
opts = opts || {};
const argv = typeof opts.argv === "undefined" ? process.argv : opts.argv;
const isWindows = process.platform === "win32";
program.version(pkg.version);
program.option("-c, --color [color]", "Dashboard color");
program.option("-m, --minimal", "Minimal mode");
program.option("-t, --title [title]", "Terminal window title");
program.option("-p, --port [port]", "Socket listener port");
program.usage("[options] -- [script] [arguments]");
program.parse(argv);
let logFromChild = true;
let child;
if (!program.args.length) {
logFromChild = false;
}
if (logFromChild) {
const command = program.args[0];
const args = program.args.slice(1);
const env = process.env;
env.FORCE_COLOR = true;
child = spawn(command, args, {
env,
stdio: [null, null, null, null],
detached: !isWindows
});
}
const dashboard = new Dashboard({
color: program.color || "green",
minimal: program.minimal || false,
title: program.title || null
});
const port = program.port || DEFAULT_PORT;
const server = opts.server || new SocketIO(port);
server.on("error", err => {
// eslint-disable-next-line no-console
console.log(err);
});
if (logFromChild) {
server.on("connection", socket => {
socket.emit("mode", { minimal: program.minimal || false });
socket.on("message", (message, ack) => {
if (message.type !== "log") {
dashboard.setData(message, ack);
}
});
});
child.stdout.on("data", data => {
dashboard.setData([
{
type: "log",
value: data.toString("utf8")
}
]);
});
child.stderr.on("data", data => {
dashboard.setData([
{
type: "log",
value: data.toString("utf8")
}
]);
});
process.on("exit", () => {
process.kill(isWindows ? child.pid : -child.pid);
});
} else {
server.on("connection", socket => {
socket.on("message", (message, ack) => {
dashboard.setData(message, ack);
});
});
}
});
if (require.main === module) {
main();
}