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-serve');
|
18 | } else {
|
19 | run();
|
20 | }
|
21 |
|
22 | function run() {
|
23 |
|
24 | const { existsSync: exists, statSync: stat } = require('fs');
|
25 | const { sep } = require('path');
|
26 |
|
27 | const chalk = require('chalk');
|
28 | const meow = require('meow');
|
29 |
|
30 | const woof = require('./');
|
31 | const { help: commandHelp, load: getCommands } = require('./commands');
|
32 | const { help: flagHelp, opts } = require('./flags');
|
33 |
|
34 |
|
35 | const flagOpts = { flags: opts() };
|
36 | const log = weblog({ name: 'command', id: 'webpack-command-forced' });
|
37 | const cli = meow(
|
38 | chalk`
|
39 | {underline Usage}
|
40 | $ webpack [<config>, ...options]
|
41 | $ webpack <entry-file> [...<entry-file>] <output-file>
|
42 |
|
43 | {underline Options}
|
44 | ${flagHelp()}
|
45 |
|
46 | For further documentation, visit {blue https://webpack.js.org/api/cli}
|
47 |
|
48 | {underline Commands}
|
49 | ${commandHelp()}
|
50 |
|
51 | Type \`webpack help <command>\` for more information
|
52 |
|
53 | {underline Examples}
|
54 | $ webpack
|
55 | $ webpack --help
|
56 | $ webpack entry.js
|
57 | $ webpack --config ../webpack.config.js
|
58 | `,
|
59 | flagOpts
|
60 | );
|
61 |
|
62 | const commands = getCommands();
|
63 | const [command] = cli.input;
|
64 |
|
65 | cli.argv = cli.flags;
|
66 | cli.commands = commands;
|
67 | cli.entries = [];
|
68 |
|
69 | const cmd = cli.commands[command];
|
70 |
|
71 | if (cmd) {
|
72 | try {
|
73 | cmd.run(cli);
|
74 | } catch (e) {
|
75 | log.error(chalk`The {bold \`${command}\`} command threw an error:`);
|
76 | throw e;
|
77 | }
|
78 | } else {
|
79 | if (cli.input.length) {
|
80 | const problems = [];
|
81 | const isDir = (path) => stat(path).isDirectory();
|
82 | const entries = [];
|
83 |
|
84 | for (let file of cli.input) {
|
85 | if (!exists(file)) {
|
86 | problems.push(file);
|
87 | } else {
|
88 | if (isDir(file)) {
|
89 | file += sep;
|
90 | }
|
91 |
|
92 | entries.push(file);
|
93 | }
|
94 | }
|
95 |
|
96 | if (problems.length) {
|
97 | const prefix =
|
98 | 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 |
|
104 | if (process.env.CLI_TEST === 'true') {
|
105 | throw new Error(message);
|
106 | } else {
|
107 | process.exit(problems.length);
|
108 | }
|
109 | }
|
110 |
|
111 | cli.entries = entries;
|
112 | }
|
113 |
|
114 | woof(cli);
|
115 | }
|
116 | }
|