-
Notifications
You must be signed in to change notification settings - Fork 3
/
cli.js
executable file
·80 lines (68 loc) · 2.02 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
#!/usr/bin/env node
var fs = require('fs')
var path = require('path')
var opn = require('opn')
var chokidar = require('chokidar')
var convert = require('./')
var server = require('./server')
var argv = require('minimist')(process.argv.slice(2), {
alias: {
's': 'server',
'p': 'port',
'w': 'watch',
'o': 'output',
'h': 'help'
},
boolean: ['server', 'watch']
})
if (!argv._.length || argv.help) {
var usage = '' +
'Usage: github-markdown-preview <markdown-file> [options]\n\n' +
'outputs html of markdown with github styles to stdout\n\n' +
'Options:\n\n' +
' -h, --help output usage information\n' +
' -s, --server watch file and server changes. This overrides -w and -o.\n' +
' -w, --watch watch markdown file and convert on changes\n' +
' -p, --port optional TCP port to start the server at, defaults to 9999\n' +
' -o, --output optional file path for output. stdout is used by default.\n' +
' required when using --watch.'
return console.log(usage)
}
if (argv.watch && !argv.output) {
return console.log('You must specify --output path when using --watch')
}
var markdownPath = path.resolve(argv._[0])
if (argv.server) {
chokidar
.watch(markdownPath, {persistent: true})
.on('change', updateServerHTML)
var port = argv.port || 9999
return updateServerHTML(function() {
server.listen(port)
console.log('Preview now being served at http://localhost:' + port)
opn('http://localhost:' + port)
})
}
if (argv.watch) {
chokidar
.watch(markdownPath, {persistent: true})
.on('change', logHTML)
}
logHTML()
function generateHTML(callback) {
var md = fs.readFileSync(markdownPath)
convert(md, callback)
}
function logHTML() {
generateHTML(function(err, html) {
if (err) throw err
if (argv.o) return fs.writeFileSync(path.resolve(argv.o), html)
console.log(html)
})
}
function updateServerHTML(cb) {
generateHTML(function(err, html) {
server.update(html)
if (typeof cb === 'function') cb()
})
}