UNPKG

3.14 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 process.exit(problems.length);
109 }
110 }
111
112 cli.entries = entries;
113 }
114
115 woof(cli).catch((e) => {
116 let error = e;
117 // eslint-disable-next-line no-shadow
118 const { error: log } = console;
119
120 if (!(error instanceof Error)) {
121 error = new Error(
122 `webpack-command failed with a value of: ${inspect(error)}`
123 );
124 }
125
126 log(error.stack);
127 process.exitCode = 1;
128 });
129 }
130}