forked from jxnblk/mdx-deck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·182 lines (161 loc) · 3.97 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env node
const path = require('path')
const meow = require('meow')
const findup = require('find-up')
const open = require('react-dev-utils/openBrowser')
const chalk = require('chalk')
const remark = {
emoji: require('remark-emoji'),
unwrapImages: require('remark-unwrap-images')
}
const pkg = require('./package.json')
const config = require('pkg-conf').sync('mdx-deck')
const log = require('./lib/log')
const cli = meow(`
${chalk.gray('Usage')}
$ ${chalk.magenta('mdx-deck deck.mdx')}
$ ${chalk.magenta('mdx-deck build deck.mdx')}
$ ${chalk.magenta('mdx-deck pdf deck.mdx')}
$ ${chalk.magenta('mdx-deck screenshot deck.mdx')}
${chalk.gray('Options')}
--webpack Path to webpack config file
${chalk.gray('Dev server options')}
-h --host Dev server host
-p --port Dev server port
--no-open Prevent from opening in default browser
${chalk.gray('Build options')}
-d --out-dir Output directory for exporting
--no-html Disable static HTML rendering
${chalk.gray('Export options')}
--out-file Filename for screenshot or PDF export
--width Width in pixels
--height Height in pixels
`, {
description: chalk.magenta('[mdx-deck] ') + chalk.gray(pkg.description),
flags: {
port: {
type: 'string',
alias: 'p'
},
host: {
type: 'string',
alias: 'h'
},
open: {
type: 'boolean',
alias: 'o',
default: true
},
outDir: {
type: 'string',
alias: 'd'
},
outFile: {
type: 'string',
},
html: {
type: 'boolean',
default: true
},
webpack: {
type: 'string',
}
}
})
const [ cmd, file ] = cli.input
const doc = file || cmd
if (!doc) cli.showHelp(0)
const opts = Object.assign({
dirname: path.dirname(path.resolve(doc)),
globals: {
FILENAME: JSON.stringify(path.resolve(doc))
},
host: 'localhost',
port: 8080,
outDir: 'dist',
}, config, cli.flags)
opts.outDir = path.resolve(opts.outDir)
if (opts.webpack) {
opts.webpack = require(path.resolve(opts.webpack))
} else {
const webpackConfig = findup.sync('webpack.config.js', { cwd: opts.dirname })
if (webpackConfig) opts.webpack = require(webpackConfig)
}
let dev
switch (cmd) {
case 'build':
log('building')
const build = require('./lib/build')
build(opts)
.then(res => {
log('done')
process.exit(0)
})
.catch(err => {
log.error(err)
process.exit(1)
})
break
case 'pdf':
log('exporting to PDF')
const pdf = require('./lib/pdf')
dev = require('./lib/dev')
dev(opts)
.then(({ server }) => {
log('rendering PDF')
pdf(opts)
.then(filename => {
server.close()
log('done', filename)
process.exit(0)
})
.catch(err => {
log.error(err)
process.exit(1)
})
})
.catch(err => {
log.error(err)
process.exit(1)
})
break
case 'screenshot':
log('exporting to PNG')
const screenshot = require('./lib/screenshot')
dev = require('./lib/dev')
dev(opts)
.then(({ server }) => {
log('rendering screenshot')
screenshot(opts)
.then(filename => {
server.close()
log('done', filename)
process.exit(0)
})
.catch(err => {
log.error(err)
process.exit(1)
})
})
.catch(err => {
log.error(err)
process.exit(1)
})
break
case 'dev':
default:
log('starting dev server')
dev = require('./lib/dev')
dev(opts)
.then(res => {
const { address, port } = res.server.address()
const url = 'http://' + address + ':' + res.port
if (opts.open) open(url)
log('listening on', chalk.magenta(url))
})
.catch(err => {
log.error(err)
process.exit(1)
})
break
}