- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcli.js
More file actions
Latest commit
executable file
·80 lines (68 loc) · 2.02 KB
/
Copy pathcli.js
File metadata and controls
executable file
·80 lines (68 loc) · 2.02 KB
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
varfs=require('fs')
varpath=require('path')
varopn=require('opn')
varchokidar=require('chokidar')
varconvert=require('./')
varserver=require('./server')
varargv=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){
varusage=''+
'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.'
returnconsole.log(usage)
}
if(argv.watch&&!argv.output){
returnconsole.log('You must specify --output path when using --watch')
}
varmarkdownPath=path.resolve(argv._[0])
if(argv.server){
chokidar
.watch(markdownPath,{persistent: true})
.on('change',updateServerHTML)
varport=argv.port||9999
returnupdateServerHTML(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()
functiongenerateHTML(callback){
varmd=fs.readFileSync(markdownPath)
convert(md,callback)
}
functionlogHTML(){
generateHTML(function(err,html){
if(err)throwerr
if(argv.o)returnfs.writeFileSync(path.resolve(argv.o),html)
console.log(html)
})
}
functionupdateServerHTML(cb){
generateHTML(function(err,html){
server.update(html)
if(typeofcb==='function')cb()
})
}