UNPKG

4.44 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3if (!module.parent) {
4 // eslint-disable-next-line global-require
5 const { register } = require('./global');
6
7 register();
8}
9
10const debug = require('debug')('webpack-command');
11const importLocal = require('import-local'); // eslint-disable-line import/order
12const weblog = require('webpack-log');
13
14// Prefer the local installation of webpack-command
15/* istanbul ignore if */
16if (importLocal(__filename)) {
17 debug('Using local install of webpack-command');
18} else {
19 run();
20}
21
22function run() {
23 /* eslint-disable global-require */
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 /* eslint-enable global-require */
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 /* istanbul ignore else */
105 if (process.env.CLI_TEST === 'true') {
106 throw new Error(message);
107 } else {
108 // citing: http://www.tldp.org/LDP/abs/html/exitcodes.html
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 // NOTE: none of these conditionals need coverage. the catch has coverage
124 // and that's what is important
125
126 /* istanbul ignore if */
127 if (!(error instanceof Error)) {
128 error = new Error(
129 `webpack-command failed with a value of: ${inspect(error)}`
130 );
131 }
132
133 // Assume that errors are generated by webpack first: e.g. ModuleNotFoundError
134 // If it's a WebpackCommandError then we goofed somehow.
135 // If it's a generic Error it's likely that webpack is throwing a poorly
136 // managed error, or a plugin has done something naughty.
137 /* istanbul ignore if|else */
138 if (errorType === 'WebpackCommandError') {
139 preamble = 'An error occured while running webpack-command';
140 } else if (errorType === 'Error') {
141 /* istanbul ignore next */
142 preamble = 'An error occured while running webpack';
143 } else {
144 logStack = false;
145 }
146
147 log.error(preamble + epilogue);
148
149 /* istanbul ignore if */
150 if (logStack) {
151 log.error(error.stack);
152 } else {
153 log.error(error.message);
154 }
155
156 /* istanbul ignore if */
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}