1 | #!/usr/bin/env node
|
2 |
|
3 | "use strict";
|
4 |
|
5 | const commander = require("commander");
|
6 | const spawn = require("cross-spawn");
|
7 | const Dashboard = require("../dashboard/index");
|
8 | const io = require("socket.io");
|
9 |
|
10 | const DEFAULT_PORT = 9838;
|
11 |
|
12 | const pkg = require("../package.json");
|
13 |
|
14 | const collect = (val, prev) => prev.concat([val]);
|
15 |
|
16 |
|
17 |
|
18 | const main = opts => {
|
19 | opts = opts || {};
|
20 | const argv = typeof opts.argv === "undefined" ? process.argv : opts.argv;
|
21 | const isWindows = process.platform === "win32";
|
22 |
|
23 | const program = new commander.Command("webpack-dashboard")
|
24 | .version(pkg.version)
|
25 | .option("-c, --color [color]", "Dashboard color")
|
26 | .option("-m, --minimal", "Minimal mode")
|
27 | .option("-t, --title [title]", "Terminal window title")
|
28 | .option("-p, --port [port]", "Socket listener port")
|
29 | .option("-a, --include-assets [string prefix]", "Asset names to limit to", collect, [])
|
30 | .usage("[options] -- [script] [arguments]")
|
31 | .parse(argv);
|
32 |
|
33 | const cliOpts = program.opts();
|
34 | const cliArgs = program.args;
|
35 |
|
36 | let logFromChild = true;
|
37 | let child;
|
38 |
|
39 | if (!cliArgs.length) {
|
40 | logFromChild = false;
|
41 | }
|
42 |
|
43 | if (logFromChild) {
|
44 | const command = cliArgs[0];
|
45 | const args = cliArgs.slice(1);
|
46 | const env = process.env;
|
47 |
|
48 | env.FORCE_COLOR = true;
|
49 |
|
50 | child = spawn(command, args, {
|
51 | env,
|
52 | stdio: [null, null, null, null],
|
53 | detached: !isWindows
|
54 | });
|
55 | }
|
56 |
|
57 | const dashboard = new Dashboard({
|
58 | color: cliOpts.color || "green",
|
59 | minimal: cliOpts.minimal || false,
|
60 | title: cliOpts.title || null
|
61 | });
|
62 |
|
63 | const port = parseInt(cliOpts.port || DEFAULT_PORT, 10);
|
64 | const server = opts.server || io(port);
|
65 |
|
66 | server.on("error", err => {
|
67 |
|
68 | console.log(err);
|
69 | });
|
70 |
|
71 | if (logFromChild) {
|
72 | server.on("connection", socket => {
|
73 | socket.emit("options", {
|
74 | minimal: cliOpts.minimal || false,
|
75 | includeAssets: cliOpts.includeAssets || []
|
76 | });
|
77 |
|
78 | socket.on("message", (message, ack) => {
|
79 | if (message.type !== "log") {
|
80 | dashboard.setData(message, ack);
|
81 | }
|
82 | });
|
83 | });
|
84 |
|
85 | child.stdout.on("data", data => {
|
86 | dashboard.setData([
|
87 | {
|
88 | type: "log",
|
89 | value: data.toString("utf8")
|
90 | }
|
91 | ]);
|
92 | });
|
93 |
|
94 | child.stderr.on("data", data => {
|
95 | dashboard.setData([
|
96 | {
|
97 | type: "log",
|
98 | value: data.toString("utf8")
|
99 | }
|
100 | ]);
|
101 | });
|
102 |
|
103 | process.on("exit", () => {
|
104 | process.kill(isWindows ? child.pid : -child.pid);
|
105 | });
|
106 | } else {
|
107 | server.on("connection", socket => {
|
108 | socket.on("message", (message, ack) => {
|
109 | dashboard.setData(message, ack);
|
110 | });
|
111 | });
|
112 | }
|
113 | };
|
114 |
|
115 | if (require.main === module) {
|
116 | main();
|
117 | }
|
118 |
|
119 | module.exports = main;
|