-
Notifications
You must be signed in to change notification settings - Fork 23
/
cli.js
executable file
·83 lines (72 loc) · 1.49 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
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
const meow = require('meow')
const open = require('react-dev-utils/openBrowser')
const chalk = require('chalk')
// const log = require('./lib/log')
const log = (...args) => {
console.log(
chalk.cyan('[mdx]'),
...args
)
}
log.error = (...args) => {
console.log(
chalk.red('[err]'),
...args
)
}
const config = require('pkg-conf').sync('ok-mdx')
const cli = meow(`
Usage:
$ mdx docs
Options:
-o --open Opens development server in default browser
-p --port Port for development server
--vim Enable editor Vim mode
`, {
flags: {
open: {
type: 'boolean',
alias: 'o'
},
port: {
type: 'string',
alias: 'p'
},
vim: {
type: 'boolean',
},
}
})
const [ cmd, dir ] = cli.input
if (!cmd) {
cli.showHelp(0)
}
const dirname = path.resolve(dir || cmd)
const opts = Object.assign({
dirname,
port: 8080,
title: 'ok-mdx',
}, config, cli.flags)
switch (cmd) {
case 'dev':
default:
const dev = require('./lib/dev')
log(`starting dev server (${dir || cmd})`)
dev(opts)
.then(({ server }) => {
const { port } = server.options
const url = `http://localhost:${port}`
log('dev server listening on', chalk.cyan(url))
if (opts.open) open(url)
})
.catch(err => {
log.error(err)
process.exit(1)
})
}
require('update-notifier')({
pkg: cli.pkg
}).notify()