UNPKG

3.3 kBJavaScriptView Raw
1import path from 'path';
2import * as colors from 'kleur/colors';
3import yargs from 'yargs-parser';
4import { command as buildCommand } from './commands/build';
5import { command as devCommand } from './commands/dev';
6import { addCommand, rmCommand } from './commands/add-rm';
7import { command as installCommand } from './commands/install';
8import { loadAndValidateConfig } from './config.js';
9import { readLockfile, clearCache } from './util.js';
10export { createConfiguration } from './config.js';
11export { install as unstable_installCommand } from './commands/install';
12const cwd = process.cwd();
13function 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}
33export async function cli(args) {
34 // parse CLI flags
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 // Load the current package manifest
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 // Set this early -- before config loading -- so that plugins see it.
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}