1 | import path from 'path';
|
2 | import * as colors from 'kleur/colors';
|
3 | import yargs from 'yargs-parser';
|
4 | import { command as buildCommand } from './commands/build';
|
5 | import { command as devCommand } from './commands/dev';
|
6 | import { addCommand, rmCommand } from './commands/add-rm';
|
7 | import { command as installCommand } from './commands/install';
|
8 | import { loadAndValidateConfig } from './config.js';
|
9 | import { readLockfile, clearCache } from './util.js';
|
10 | export { createConfiguration } from './config.js';
|
11 | export { install as unstable_installCommand } from './commands/install';
|
12 | const cwd = process.cwd();
|
13 | function printHelp() {
|
14 | console.log(`
|
15 | ${colors.bold(`snowpack`)} - A faster build system for the modern web.
|
16 |
|
17 | Snowpack is best configured via config file.
|
18 | But, most configuration can also be passed via CLI flags.
|
19 | 📖 ${colors.dim('https://www.snowpack.dev/#configuration')}
|
20 |
|
21 | ${colors.bold('Commands:')}
|
22 | snowpack dev Develop your app locally.
|
23 | snowpack build Build your app for production.
|
24 | snowpack install (Advanced) Install web-ready dependencies.
|
25 |
|
26 | ${colors.bold('Flags:')}
|
27 | --config [path] Set the location of your project config file.
|
28 | --help Show this help message.
|
29 | --version Show the current version.
|
30 | --reload Clear Snowpack's local cache (troubleshooting).
|
31 | `.trim());
|
32 | }
|
33 | export async function cli(args) {
|
34 |
|
35 | const cliFlags = yargs(args, {
|
36 | array: ['install', 'env', 'exclude', 'externalPackage'],
|
37 | });
|
38 | if (cliFlags.help) {
|
39 | printHelp();
|
40 | process.exit(0);
|
41 | }
|
42 | if (cliFlags.version) {
|
43 | console.log(require('../package.json').version);
|
44 | process.exit(0);
|
45 | }
|
46 | if (cliFlags.reload) {
|
47 | console.log(colors.yellow('! clearing cache...'));
|
48 | await clearCache();
|
49 | }
|
50 |
|
51 | let pkgManifest;
|
52 | try {
|
53 | pkgManifest = require(path.join(cwd, 'package.json'));
|
54 | }
|
55 | catch (err) {
|
56 | console.log(colors.red('[ERROR] package.json required but no file was found.'));
|
57 | process.exit(1);
|
58 | }
|
59 | const cmd = cliFlags['_'][2];
|
60 |
|
61 | if (cmd === 'build') {
|
62 | process.env.NODE_ENV = process.env.NODE_ENV || 'production';
|
63 | }
|
64 | if (cmd === 'dev') {
|
65 | process.env.NODE_ENV = process.env.NODE_ENV || 'development';
|
66 | }
|
67 | const commandOptions = {
|
68 | cwd,
|
69 | config: loadAndValidateConfig(cliFlags, pkgManifest),
|
70 | lockfile: await readLockfile(cwd),
|
71 | pkgManifest,
|
72 | };
|
73 | if (cmd === 'add') {
|
74 | await addCommand(cliFlags['_'][3], commandOptions);
|
75 | return;
|
76 | }
|
77 | if (cmd === 'rm') {
|
78 | await rmCommand(cliFlags['_'][3], commandOptions);
|
79 | return;
|
80 | }
|
81 | if (cliFlags['_'].length > 3) {
|
82 | console.log(`Unexpected multiple commands`);
|
83 | process.exit(1);
|
84 | }
|
85 | if (cmd === 'build') {
|
86 | await buildCommand(commandOptions);
|
87 | return;
|
88 | }
|
89 | if (cmd === 'dev') {
|
90 | await devCommand(commandOptions);
|
91 | return;
|
92 | }
|
93 | if (cmd === 'install' || !cmd) {
|
94 | await installCommand(commandOptions);
|
95 | return;
|
96 | }
|
97 | console.log(`Unrecognized command: ${cmd}`);
|
98 | process.exit(1);
|
99 | }
|