This repository has been archived by the owner on Jun 15, 2019. It is now read-only.
forked from zaach/json2jison
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
74 lines (57 loc) · 1.71 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
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
#!/usr/bin/env node
// Converts json grammar format to Jison grammar format
var version = require('./package.json').version;
var path = require('path');
var fs = require('fs');
var json2jison = require('./json2jison');
var opts = require('@gerhobbelt/nomnom')
.unknownOptionTreatment(false) // do not accept unknown options!
.script('json2jison')
.option('file', {
flag: true,
position: 0,
help: 'file containing a grammar in JSON format'
})
.option('outfile', {
abbr: 'o',
metavar: 'FILE',
help: 'Filename and base module name of the generated jison grammar file'
})
.option('version', {
abbr: 'V',
flag: true,
help: 'print version and exit',
callback: function() {
return version;
}
});
exports.main = function main (opts) {
if (opts.file) {
var raw = fs.readFileSync(path.normalize(opts.file), 'utf8');
var outpath = (opts.outfile || opts.file);
var name = path.basename(outpath).replace(/\..*$/g, '');
outpath = path.dirname(outpath);
var outfile = path.resolve(outpath, name + '.jison');
console.log('RAW:', raw);
fs.writeFileSync(outfile, json2jison.convert(raw), 'utf8');
console.log(`JISON grammar has been written to file "${outfile}"`);
} else {
input(function (raw) {
console.log(json2jison.convert(raw));
});
}
};
function input (cb) {
var stdin = process.openStdin(),
data = '';
stdin.setEncoding('utf8');
stdin.addListener('data', function (chunk) {
data += chunk;
});
stdin.addListener('end', function () {
cb(data);
});
}
if (require.main === module) {
exports.main(opts.parse());
}