-
Notifications
You must be signed in to change notification settings - Fork 14
/
parse.js
executable file
·54 lines (46 loc) · 1.26 KB
/
parse.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
#!/usr/bin/env node
// Command-line tool that parses a type expression and dumps a JSON version of the parse tree.
const catharsis = require('../catharsis');
const path = require('path');
const util = require('util');
const command = path.basename(process.argv[1]);
const typeExpression = process.argv[2];
const opts = {
describe: false,
jsdoc: false,
};
let parsedType;
function usage() {
console.log(util.format('Usage:\n %s typeExpression [--jsdoc] [--describe]', command));
}
function done(err) {
// eslint-disable-next-line no-process-exit
process.exit(err === undefined ? 0 : err);
}
process.argv.slice(3).forEach((arg) => {
const parsedArg = arg.replace(/^-{2}/, '');
if (opts[parsedArg] !== undefined) {
opts[parsedArg] = true;
} else {
console.error('Unknown option "%s"', arg);
usage();
done(1);
}
});
if (!typeExpression) {
usage();
done(1);
} else {
try {
parsedType = catharsis.parse(typeExpression, opts);
if (opts.describe) {
parsedType = catharsis.describe(parsedType);
}
} catch (e) {
console.error(util.format('Unable to parse "%s" (exception follows):', typeExpression));
console.error(e.stack || e.message);
done(1);
}
console.log(JSON.stringify(parsedType, null, 2));
done();
}