1 | const chalk = require('chalk')
|
2 | const yargs = require('yargs')
|
3 | const loudRejection = require('loud-rejection')
|
4 | const update = require('update-notifier')
|
5 | const pkg = require('../package.json')
|
6 |
|
7 | loudRejection()
|
8 |
|
9 | update({ pkg }).notify()
|
10 |
|
11 | function getOpts(argv, mode) {
|
12 | const opts = Object.keys(argv).reduce((curr, next) => {
|
13 | if (typeof argv[next] !== 'undefined' && next !== 'mode') {
|
14 | curr[next] = argv[next]
|
15 | }
|
16 | return curr
|
17 | }, {})
|
18 | const cmd = argv._[0]
|
19 | let entry
|
20 | if (['build', 'dev', 'watch', 'test'].indexOf(cmd) > -1) {
|
21 | entry = argv._.slice(1)
|
22 | } else {
|
23 | entry = argv._
|
24 | }
|
25 | if (entry.length > 0) {
|
26 | opts.entry = entry
|
27 | }
|
28 | return Object.assign({ mode }, opts)
|
29 | }
|
30 |
|
31 | function createHandler(mode) {
|
32 | return argv => {
|
33 | const run = require('./run')
|
34 | run(getOpts(argv, mode)).catch(run.handleError)
|
35 | }
|
36 | }
|
37 |
|
38 | const sharedOptions = {
|
39 | dist: {
|
40 | alias: 'd',
|
41 | desc: 'Output directory'
|
42 | },
|
43 | config: {
|
44 | alias: 'c',
|
45 | desc: 'Use custom path to config file'
|
46 | },
|
47 | templateCompiler: {
|
48 | desc: 'Use full build of Vue'
|
49 | },
|
50 | noClear: {
|
51 | desc: 'Do not clear screen'
|
52 | },
|
53 | inspectOptions: {
|
54 | desc: 'Output final options'
|
55 | }
|
56 | }
|
57 |
|
58 | yargs
|
59 | .usage(`\n${chalk.yellow('poi')} [command] [options]`)
|
60 | .command(['build'], 'Build app in production mode', cli => {
|
61 | cli.options(Object.assign({}, sharedOptions, {
|
62 | generateStats: {
|
63 | desc: 'Generate webpack stats for the bundle file'
|
64 | }
|
65 | }))
|
66 | }, createHandler('production'))
|
67 | .command(['*', 'dev'], 'Run app in development mode', cli => {
|
68 | cli.options(Object.assign({}, sharedOptions, {
|
69 | port: {
|
70 | desc: 'Custom dev server port',
|
71 | type: 'number'
|
72 | },
|
73 | host: {
|
74 | desc: 'Custom dev server hostname',
|
75 | type: 'string'
|
76 | },
|
77 | proxy: {
|
78 | desc: 'Proxy API request',
|
79 | type: 'string'
|
80 | },
|
81 | open: {
|
82 | alias: 'o',
|
83 | desc: 'Open App after compiling'
|
84 | }
|
85 | }))
|
86 | }, createHandler('development'))
|
87 | .command('watch', 'Run app in watch mode', () => {}, createHandler('watch'))
|
88 | .command('test', 'Run app in test mode', () => {}, createHandler('test'))
|
89 | .version(pkg.version)
|
90 | .alias('version', 'v')
|
91 | .alias('help', 'h')
|
92 | .epilogue('for more information, find our manual at https://poi.js.org')
|
93 | .help()
|
94 | .argv
|