UNPKG

2.45 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3/* eslint no-console: 0 */
4import Liftoff from 'liftoff';
5import program from 'commander';
6import createDebug from 'debug';
7import { jsVariants } from 'interpret';
8import packageJson from '../package.json';
9import { createCompiler } from './compiler';
10import startServer from './cli/server';
11import buildFrontend from './cli/builder';
12
13const { version } = packageJson;
14const debug = createDebug('cyboard.cli');
15
16if (!process.env.NODE_ENV) {
17 process.env.NODE_ENV = 'production';
18}
19
20const cli = new Liftoff({
21 name: 'Cyboard',
22 extensions: {
23 ...jsVariants,
24 '.jsx': 'babel-register',
25 },
26});
27
28function 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
37cli.on('require', name => debug('Requiring external module %s', name));
38cli.on('requireFail', name => debug('Loading module %s failed', name));
39cli.on('respawn', (flags, child) => {
40 console.log('Detected node flags:', flags);
41 console.log('Respawned to PID:', child.pid);
42});
43
44program.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
49program.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
61program.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
70program.parse(process.argv);
71
72if (!program.args.length) program.help();