UNPKG

4.41 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 = problems.length === cli.input.length ? 'The' : 'Some of the';
99 const message = `${prefix} input provided did not match any known commands or existing files:
100 ${problems.join(' ')}`;
101 log.error(message);
102
103 /* istanbul ignore else */
104 if (process.env.CLI_TEST === 'true') {
105 throw new Error(message);
106 } else {
107 // citing: http://www.tldp.org/LDP/abs/html/exitcodes.html
108 process.exit(127);
109 }
110 }
111
112 cli.entries = entries;
113 }
114
115 woof(cli).catch((e) => {
116 let error = e;
117 const errorType = error.constructor.name;
118 const epilogue = chalk`: {dim use --log-level=debug for more error detail}`;
119 let preamble = 'A webpack error occured while building';
120 let logStack = true;
121
122 // NOTE: none of these conditionals need coverage. the catch has coverage
123 // and that's what is important
124
125 /* istanbul ignore if */
126 if (!(error instanceof Error)) {
127 error = new Error(`webpack-command failed with a value of: ${inspect(error)}`);
128 }
129
130 // Assume that errors are generated by webpack first: e.g. ModuleNotFoundError
131 // If it's a WebpackCommandError then we goofed somehow.
132 // If it's a generic Error it's likely that webpack is throwing a poorly
133 // managed error, or a plugin has done something naughty.
134 /* istanbul ignore if|else */
135 if (errorType === 'WebpackCommandError') {
136 preamble = 'An error occured while running webpack-command';
137 } else if (errorType === 'Error') {
138 /* istanbul ignore next */
139 preamble = 'An error occured while running webpack';
140 } else {
141 logStack = false;
142 }
143
144 log.error(preamble + epilogue);
145
146 /* istanbul ignore if */
147 if (logStack) {
148 log.error(error.stack);
149 } else {
150 log.error(error.message);
151 }
152
153 /* istanbul ignore if */
154 if (cli.flags.logLevel === 'debug') {
155 log.level = cli.flags.logLevel;
156 }
157
158 log.debug(inspect(error));
159
160 process.exitCode = 1;
161 });
162 }
163}