forked from jxnblk/mdx-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·153 lines (134 loc) · 3.09 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env node
const path = require('path')
const meow = require('meow')
const chalk = require('chalk')
const open = require('react-dev-utils/openBrowser')
const findUp = require('find-up')
const name = 'mdx-go'
const config = require('pkg-conf').sync(name)
const { pkg } = require('read-pkg-up').sync()
const log = (...msg) => {
console.log(
chalk.green(`[${name}]`),
...msg
)
}
log.error = (...msg) => {
console.log(
chalk.red('[err]'),
...msg
)
}
const cli = meow(`
${chalk.gray('Usage')}
${chalk.gray('$')} ${chalk.green(name + ' docs')}
${chalk.gray('$')} ${chalk.green(name + ' build docs')}
${chalk.gray('Options')}
-p --port Port for dev server
--no-open Disable opening in default browser
--fullscreen Disable default centered layout
--no-keyboard Disable keyboard shortcuts
-d --out-dir Output directory for static export
--basename Base path for routing
--static Export HTML without JS bundle
--webpack Path to custom webpack config
`, {
description: chalk.green(name) + ' Lightning fast MDX-based dev server',
flags: {
help: {
type: 'boolean',
alias: 'h'
},
version: {
type: 'boolean',
alias: 'v'
},
port: {
type: 'string',
alias: 'p',
default: '8080'
},
open: {
type: 'boolean',
alias: 'o',
default: true
},
fullscreen: {
type: 'boolean'
},
keyboard: {
type: 'boolean',
default: true
},
outDir: {
type: 'string',
alias: 'd',
default: 'dist'
},
basename: {
type: 'string'
},
static: {
type: 'boolean'
},
webpack: {
type: 'string'
},
debug: {
type: 'boolean'
}
}
})
require('update-notifier')({ pkg: cli.pkg }).notify()
const [ cmd, input ] = cli.input
if (!cmd && !input) {
cli.showHelp(0)
}
const opts = Object.assign({
pkg,
name,
dirname: path.resolve(input || cmd),
basename: '',
webpack: findUp.sync('webpack.config.js'),
}, config, cli.flags)
opts.outDir = path.resolve(opts.outDir)
if (!opts.cssLibrary && pkg && pkg.dependencies) {
const deps = Object.assign({}, pkg.devDependencies, pkg.dependencies)
if (deps['styled-components']) {
opts.cssLibrary = 'styled-components'
} else if (deps['emotion']) {
opts.cssLibrary = 'emotion'
}
}
if (opts.webpack) {
opts.webpack = require(path.resolve(opts.webpack))
}
switch (cmd) {
case 'build':
log('building...')
const build = require('./lib/build')
build(opts)
.catch(err => {
log.error(err)
process.exit(1)
})
.then(stats => {
log('built', chalk.gray(opts.outDir))
})
break
case 'dev':
default:
log('starting dev server...')
const dev = require('./lib/dev')
dev(opts)
.then(server => {
const { port } = server.address()
const url = `http://localhost:${port}`
log('listening on', chalk.green(url))
if (opts.open) open(url)
})
.catch(err => {
log.error(err)
process.exit(1)
})
}