-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin.js
executable file
·160 lines (153 loc) · 3.93 KB
/
bin.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
#!/usr/bin/env node
if (module.parent) {
module.exports = bin
} else {
bin(
process.stdin,
process.stdout,
process.stderr,
process.argv.slice(2),
status => process.exit(status)
)
}
function bin (stdin, stdout, stderr, argv, done) {
require('yargs')
.scriptName('commonform-commonmark')
.command(
'parse [file]',
'parse CommonMark to Common Form JSON',
yargs => yargs
.positional('file', {
describe: 'CommonMark file',
type: 'string',
normalize: true
})
.option('o', {
alias: 'only',
describe: 'limit output',
choices: ['form', 'directions']
}),
args => {
readInput(args, buffer => {
let parsed
try {
parsed = require('./').parse(buffer.toString())
} catch (error) {
return fail(error)
}
let output = parsed
if (args.only) output = parsed[args.only]
stdout.write(JSON.stringify(output) + '\n')
done(0)
})
}
)
.command(
'stringify [file]',
'stringify Common Form JSON to CommonMark',
yargs => yargs
.positional('file', {
describe: 'JSON file',
type: 'string',
normalize: true
})
.option('title', {
alias: 't',
describe: 'form title',
type: 'string'
})
.option('form-version', {
alias: 'e',
describe: 'form version',
type: 'string'
})
.option('values', {
alias: 'v',
describe: 'JSON file with blank values',
coerce: readJSON,
demandOption: false
})
.option('directions', {
alias: 'd',
describe: 'JSON file with directions',
coerce: readJSON,
demandOption: false
})
.implies('directions', 'values')
.option('ordered', {
alias: 'o',
describe: 'output ordered lists',
default: false,
type: 'boolean'
})
.option('ids', {
alias: 'i',
describe: 'output explicit heading IDs',
default: false,
type: 'boolean'
})
.option('front-matter', {
alias: 'm',
describe: 'output YAML front matter',
default: false,
type: 'boolean'
}),
args => {
readInput(args, input => {
let blanks
if (args.values) {
try {
blanks = require('commonform-prepare-blanks')(
args.values, args.directions
)
} catch (error) {
return fail(error)
}
} else {
blanks = []
}
const options = {}
if (args.title) options.title = args.title
if (args['form-version']) options.version = args['form-version']
if (args.ordered) options.ordered = true
if (args['front-matter']) options.frontMatter = true
if (args.ids) options.ids = true
let json, stringified
try {
json = JSON.parse(input)
stringified = require('./')
.stringify(json, blanks, options)
} catch (error) {
return fail(error)
}
stdout.write(stringified)
done(0)
})
}
)
.version()
.help()
.alias('h', 'help')
.demandCommand()
.parse(argv)
function readInput (args, callback) {
const input = args.file
? require('fs').createReadStream(args.file)
: stdin
require('simple-concat')(input, (error, buffer) => {
if (error) return fail(error)
callback(buffer)
})
}
function fail (error) {
stderr.write(error.toString() + '\n')
done(1)
}
}
function readJSON (file) {
return JSON.parse(
require('fs').readFileSync(
require('path').normalize(file)
)
)
}