forked from speced/respec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
respec2html.js
executable file
·129 lines (125 loc) · 2.83 KB
/
respec2html.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
#!/usr/bin/env node
/*jshint node: true, browser: false*/
"use strict";
const colors = require("colors");
const fetchAndWrite = require("./respecDocWriter").fetchAndWrite;
colors.setTheme({
data: "grey",
debug: "cyan",
error: "red",
help: "cyan",
important: "red",
info: "green",
input: "grey",
prompt: "grey",
verbose: "cyan",
warn: "yellow",
});
const commandLineArgs = require("command-line-args");
const getUsage = require("command-line-usage");
// Command line output
const optionList = [
{
alias: "h",
defaultValue: false,
description: "Display this usage guide.",
name: "help",
type: Boolean,
},
{
alias: "s",
defaultOption: true,
description: "URL to ReSpec source file.",
multiple: false,
name: "src",
type: String,
},
{
alias: "o",
defaultOption: false,
description: "Path to output file. When omitted, just stdout.",
multiple: false,
name: "out",
type: String,
},
{
alias: "t",
defaultValue: 10,
description: "How long to wait before timing out (in seconds).",
name: "timeout",
type: Number,
},
{
alias: "e",
default: false,
description: "Abort if the spec has any errors.",
name: "haltonerror",
type: Boolean,
},
{
alias: "w",
default: false,
description: "Abort if ReSpec generates warnings.",
name: "haltonwarn",
type: Boolean,
},
];
const usageSections = [
{
header: "respec2html",
content: "Converts a ReSpec source file to HTML and prints to std out.",
},
{
header: "Options",
optionList,
},
{
header: "Examples",
content: [
{
desc: "1. Output to a file. ",
example: "$ ./respec2html.js --src http://example.com/spec.html --out spec.html",
},
{
desc: "2. Halt on errors or warning ",
example: "$ ./respec2html.js -e -w --src http://example.com/spec.html --out spec.html",
},
],
},
{
content: "Project home: [underline]{https://github.com/w3c/respec}",
raw: true,
},
];
(async function run() {
let parsedArgs;
try {
parsedArgs = commandLineArgs(optionList);
} catch (err) {
console.info(getUsage(usageSections));
console.error(colors.error(err.message));
return process.exit(127);
}
if (!parsedArgs.src) {
console.info(getUsage(usageSections));
return process.exit(2);
}
if (parsedArgs.help) {
console.info(getUsage(usageSections));
return process.exit(0);
}
const src = parsedArgs.src;
const whenToHalt = {
haltOnError: parsedArgs.haltonerror,
haltOnWarn: parsedArgs.haltonwarn,
};
const timeout = parsedArgs.timeout;
const out = parsedArgs.out;
try {
await fetchAndWrite(src, out, whenToHalt, timeout);
} catch (err) {
console.error(colors.error(err.message));
return process.exit(1);
}
process.exit(0);
})();