1 | #!/usr/bin/env node
|
2 |
|
3 | if (!module.parent) {
|
4 |
|
5 | const { register } = require('./global');
|
6 |
|
7 | register();
|
8 | }
|
9 |
|
10 | const debug = require('debug')('webpack-command');
|
11 | const importLocal = require('import-local');
|
12 | const weblog = require('webpack-log');
|
13 |
|
14 |
|
15 |
|
16 | if (importLocal(__filename)) {
|
17 | debug('Using local install of webpack-command');
|
18 | } else {
|
19 | run();
|
20 | }
|
21 |
|
22 | function run() {
|
23 |
|
24 | const { existsSync: exists, statSync: stat } = require('fs');
|
25 | const { sep } = require('path');
|
26 | const { inspect } = require('util');
|
27 |
|
28 | const chalk = require('chalk');
|
29 | const meow = require('meow');
|
30 |
|
31 | const woof = require('./');
|
32 | const { help: commandHelp, load: getCommands } = require('./commands');
|
33 | const { help: flagHelp, opts } = require('./flags');
|
34 |
|
35 |
|
36 | const flagOpts = { flags: opts() };
|
37 | const log = weblog({ name: 'command', id: 'webpack-command-forced' });
|
38 | const cli = meow(
|
39 | chalk`
|
40 | {underline Usage}
|
41 | $ webpack [<config>, ...options]
|
42 | $ webpack <entry-file> [...<entry-file>] <output-file>
|
43 |
|
44 | {underline Options}
|
45 | ${flagHelp()}
|
46 |
|
47 | For further documentation, visit {blue https://webpack.js.org/api/cli}
|
48 |
|
49 | {underline Commands}
|
50 | ${commandHelp()}
|
51 |
|
52 | Type \`webpack help <command>\` for more information
|
53 |
|
54 | {underline Examples}
|
55 | $ webpack
|
56 | $ webpack --help
|
57 | $ webpack entry.js
|
58 | $ webpack --config ../webpack.config.js
|
59 | `,
|
60 | flagOpts
|
61 | );
|
62 |
|
63 | const commands = getCommands();
|
64 | const [command] = cli.input;
|
65 |
|
66 | cli.argv = cli.flags;
|
67 | cli.commands = commands;
|
68 | cli.entries = [];
|
69 |
|
70 | const cmd = cli.commands[command];
|
71 |
|
72 | if (cmd) {
|
73 | try {
|
74 | cmd.run(cli);
|
75 | } catch (e) {
|
76 | log.error(chalk`The {bold \`${command}\`} command threw an error:`);
|
77 | throw e;
|
78 | }
|
79 | } else {
|
80 | if (cli.input.length) {
|
81 | const problems = [];
|
82 | const isDir = (path) => stat(path).isDirectory();
|
83 | const entries = [];
|
84 |
|
85 | for (let file of cli.input) {
|
86 | if (!exists(file)) {
|
87 | problems.push(file);
|
88 | } else {
|
89 | if (isDir(file)) {
|
90 | file += sep;
|
91 | }
|
92 |
|
93 | entries.push(file);
|
94 | }
|
95 | }
|
96 |
|
97 | if (problems.length) {
|
98 | const prefix =
|
99 | problems.length === cli.input.length ? 'The' : 'Some of the';
|
100 | const message = `${prefix} input provided did not match any known commands or existing files:
|
101 | ${problems.join(' ')}`;
|
102 | log.error(message);
|
103 |
|
104 |
|
105 | if (process.env.CLI_TEST === 'true') {
|
106 | throw new Error(message);
|
107 | } else {
|
108 |
|
109 | process.exit(127);
|
110 | }
|
111 | }
|
112 |
|
113 | cli.entries = entries;
|
114 | }
|
115 |
|
116 | woof(cli).catch((e) => {
|
117 | let error = e;
|
118 | const errorType = error.constructor.name;
|
119 | const epilogue = chalk`: {dim use --log-level=debug for more error detail}`;
|
120 | let preamble = 'A webpack error occured while building';
|
121 | let logStack = true;
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 | if (!(error instanceof Error)) {
|
128 | error = new Error(
|
129 | `webpack-command failed with a value of: ${inspect(error)}`
|
130 | );
|
131 | }
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 | if (errorType === 'WebpackCommandError') {
|
139 | preamble = 'An error occured while running webpack-command';
|
140 | } else if (errorType === 'Error') {
|
141 |
|
142 | preamble = 'An error occured while running webpack';
|
143 | } else {
|
144 | logStack = false;
|
145 | }
|
146 |
|
147 | log.error(preamble + epilogue);
|
148 |
|
149 |
|
150 | if (logStack) {
|
151 | log.error(error.stack);
|
152 | } else {
|
153 | log.error(error.message);
|
154 | }
|
155 |
|
156 |
|
157 | if (cli.flags.logLevel === 'debug') {
|
158 | log.level = cli.flags.logLevel;
|
159 | }
|
160 |
|
161 | log.debug(inspect(error));
|
162 |
|
163 | process.exitCode = 1;
|
164 | });
|
165 | }
|
166 | }
|