UNPKG

2.69 kBJavaScriptView Raw
1#!/usr/bin/env node
2const cac = require('cac')
3const chalk = require('chalk')
4const merge = require('lodash.merge')
5const logger = require('../lib/logger')
6
7const major = parseInt(process.versions.node.split('.')[0], 10)
8if (major < 8) {
9 // eslint-disable-next-line import/no-unassigned-import
10 require('async-to-gen/register')
11}
12
13const cli = cac()
14
15const logUrl = app => {
16 let ip
17 app.on('renderer-ready', () => {
18 const { host, port } = app.config.server
19 const isDefaultRoute = host === '0.0.0.0'
20 logger.log(
21 `\n App running at:\n\n - Local: ${chalk.bold(
22 `http://${isDefaultRoute ? 'localhost' : host}:${port}`
23 )}\n${
24 isDefaultRoute
25 ? chalk.dim(
26 ` - Network: http://${ip ||
27 (ip = require('internal-ip').v4.sync())}:${port}`
28 )
29 : ''
30 }`
31 )
32 })
33}
34
35const getOptions = (command, input, flags) => {
36 const dev = command === 'dev'
37 const options = Object.assign({}, flags, {
38 dev,
39 baseDir: input[0]
40 })
41 const config = {}
42 if (command === 'dev' || command === 'start') {
43 if (flags.host) {
44 config.server = merge(config.server, { host: flags.host })
45 }
46 if (flags.port) {
47 config.server = merge(config.server, { port: flags.port })
48 }
49 }
50 return {
51 options,
52 config
53 }
54}
55
56cli.command('build', 'Build your application', (input, flags) => {
57 const { options, config } = getOptions('build', input, flags)
58 const app = require('../lib')(options, config)
59 return app.build()
60})
61
62cli
63 .command('dev', 'Develop your application', (input, flags) => {
64 const { options, config } = getOptions('dev', input, flags)
65 const app = require('../lib')(options, config)
66 logUrl(app)
67 return app.start()
68 })
69 .option('host', 'Server host (default: 0.0.0.0)')
70 .option('port', 'Server port (default: 4000)')
71
72cli
73 .command('start', 'Start your application in production', (input, flags) => {
74 const { options, config } = getOptions('start', input, flags)
75 const app = require('../lib')(options, config)
76 logUrl(app)
77 return app.start()
78 })
79 .option('host', 'Server host (default: 0.0.0.0)')
80 .option('port', 'Server port (default: 4000)')
81
82cli.command('generate', 'Generate static html files', (input, flags) => {
83 const { options, config } = getOptions('generate', input, flags)
84 const app = require('../lib')(options, config)
85 return app.generate()
86})
87
88cli
89 .option('config', {
90 alias: 'c',
91 desc: 'Load a config file (default: ream.config.js)'
92 })
93 .option('debug', 'Print debug logs')
94 .option('inspectWebpack', 'Print webpack config as string')
95 .option('progress', 'Toggle progress bar')
96
97cli.parse()