-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcli.js
executable file
·48 lines (39 loc) · 1.03 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
#!/usr/bin/env node
const unified = require('unified')
const markdown = require('remark-parse')
const inspect = require('unist-util-inspect')
const fs = require('fs')
const path = require('path')
const chalk = require('chalk')
const compiler = require('./lib/compiler')
function stringify() {
this.Compiler = compiler
}
const processor = unified()
.use(markdown)
.use(stringify)
const args = process.argv
// expect stdin if no arg is provided
if (args.length <= 2) {
let MD = ''
process.stdin.resume()
process.stdin.setEncoding('utf8')
process.stdin.on('data', function(chunk) {
MD += chunk
})
process.stdin.on('end', function() {
process.stdout.write(processor.processSync(MD).toString())
})
} else {
const filepath = args[2]
try {
const MD = fs.readFileSync(path.resolve(process.cwd(), filepath))
console.log(processor.processSync(MD).toString())
} catch (e) {
if (e.code === 'ENOENT') {
console.error(chalk.bold.bgRed.white('ERROR') + ' File not found')
} else {
throw e
}
}
}