1 | #!/usr/bin/env node
|
2 |
|
3 |
|
4 | import Liftoff from 'liftoff';
|
5 | import program from 'commander';
|
6 | import createDebug from 'debug';
|
7 | import { jsVariants } from 'interpret';
|
8 | import packageJson from '../package.json';
|
9 | import { createCompiler } from './compiler';
|
10 | import startServer from './cli/server';
|
11 | import buildFrontend from './cli/builder';
|
12 |
|
13 | const { version } = packageJson;
|
14 | const debug = createDebug('cyboard.cli');
|
15 |
|
16 | if (!process.env.NODE_ENV) {
|
17 | process.env.NODE_ENV = 'production';
|
18 | }
|
19 |
|
20 | const cli = new Liftoff({
|
21 | name: 'Cyboard',
|
22 | extensions: {
|
23 | ...jsVariants,
|
24 | '.jsx': 'babel-register',
|
25 | },
|
26 | });
|
27 |
|
28 | function validateConfigPath(env) {
|
29 | if (!env.configPath) {
|
30 | console.log(`No ${cli.configName} found.`);
|
31 | return false;
|
32 | }
|
33 | debug(`Using Cyboardfile from ${env.configPath}`);
|
34 | return true;
|
35 | }
|
36 |
|
37 | cli.on('require', name => debug('Requiring external module %s', name));
|
38 | cli.on('requireFail', name => debug('Loading module %s failed', name));
|
39 | cli.on('respawn', (flags, child) => {
|
40 | console.log('Detected node flags:', flags);
|
41 | console.log('Respawned to PID:', child.pid);
|
42 | });
|
43 |
|
44 | program.version(version)
|
45 | .option('--cwd [path]', 'Sets the current working directory')
|
46 | .option('--cache [path]', 'Set the directory for caching', './.cyboard')
|
47 | .option('--require [path]', 'Require additional modules to run the server');
|
48 |
|
49 | program.command('start [cyboardfile]')
|
50 | .description('Starts a cyboard server')
|
51 | .option('-p, --port [number]', 'Sets the used port number for catalog server', '3000')
|
52 | .option('-c, --credentials [path]', 'Sets the path to the credentials file', './credentials.json')
|
53 | .option('-d, --dev', 'Start the server in devmode, which runs an internal webpack devserver')
|
54 | .action((configPath, { port, credentials, dev, parent: { cache, cwd, require } }) => {
|
55 | cli.launch(
|
56 | { cwd, configPath, require },
|
57 | env => validateConfigPath(env) && startServer({ ...env, cache, credentials, port, isDev: !!dev }),
|
58 | );
|
59 | });
|
60 |
|
61 | program.command('warmup [cyboardfile]')
|
62 | .description('Warm up cache with static frontend build')
|
63 | .action((configPath, { parent: { cache, cwd, require } }) => {
|
64 | cli.launch(
|
65 | { cwd, configPath, require },
|
66 | env => validateConfigPath(env) && buildFrontend({ ...env, cache }),
|
67 | );
|
68 | });
|
69 |
|
70 | program.parse(process.argv);
|
71 |
|
72 | if (!program.args.length) program.help();
|