-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
mdv.js
77 lines (67 loc) · 1.93 KB
/
mdv.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
#!/usr/bin/env node
// @ts-check
'use strict';
const fs = require('fs');
const util = require('util');
const yaml = require('yaml');
const glob = require("glob");
const validator = require('./index.js');
const argv = require('yargs')
.boolean('debug')
.alias('d','debug')
.describe('debug','enable debug mode')
.help('help')
.alias('h','help')
.string('outfile')
.alias('o','outfile')
.boolean('save')
.alias('s','save')
.describe('save','save intermediary html output')
.boolean('warnings')
.alias('w','warnings')
.describe('warnings','enable warnings')
.boolean('yaml')
.alias('y','yaml')
.describe('yaml','output in YAML not JSON')
.require(1)
.strict()
.argv;
let exitCode = 0;
const options = argv;
for (let a of argv._) {
const files = glob.sync(a, { debug: !!options.debug });
for (let file of files) {
const s = fs.readFileSync(file,'utf8');
options.source = file;
const result = validator.validate(s,options);
if (options.save) {
fs.writeFileSync(file+'.html',options.html,'utf8');
delete options.html;
}
let ok = true;
for (let p in result) {
if (typeof result[p] == 'number') {
if (result[p]) ok = false
else delete result[p];
}
else if (Array.isArray(result[p])) {
if (result[p].length) ok = false
else delete result[p];
}
else if (typeof result[p] == 'object') {
if (Object.keys(result[p]).length) ok = false
else delete result[p];
}
}
if (!ok) {
if (argv.yaml) {
console.log(yaml.stringify(result));
}
else {
console.log(util.inspect(result,{depth:null}));
}
exitCode = 1;
}
}
}
process.exit(exitCode);