forked from json5/json5
/
cli.js
executable file
·82 lines (69 loc) · 2.38 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
74
75
76
77
78
79
80
81
82
#!/usr/bin/env node
// cli.js
// JSON5 command-line interface.
var FS = require('fs');
var JSON5 = require('./json5');
var Path = require('path');
var Transform = require('stream').Transform;
var streams = require('./streams');
var yargs = require('yargs')
.help('h').alias('h', 'help')
.option('c', {
alias: 'compile',
describe: 'Compiles JSON5 files into sibling JSON files with the same basenames.',
type: 'boolean'
})
.option('stdin', {
describe: 'read JSON5 from STDIN, write JSON to STDOUT',
type: 'boolean'
})
.option('indent', {
describe: 'intentation level for pretty-printing JSON',
default: 4
})
.option('o', {
describe: 'optimize',
type: 'boolean',
})
.strict()
.check(function(argv, opts) {
if (argv.c && argv.stdin) {
throw new Error('conflicting options: -c and --stdin');
}
if (argv.c && argv._.length === 0) {
throw new Error('-c set, but no JSON5 files given');
}
if (!argv.c && !argv.stdin) {
throw new Error('not enough options');
}
return true;
});
var argv = yargs.argv;
function compileFiles(files, indent) {
var cwd = process.cwd();
// iterate over each file and convert JSON5 files to JSON:
files.forEach(function (file) {
var path = Path.resolve(cwd, file);
var basename = Path.basename(path, '.json5');
var dirname = Path.dirname(path);
var json5 = FS.readFileSync(path, 'utf8');
var obj = JSON5.parse(json5);
var json = JSON.stringify(obj, null, indent) + (indent ? "\n" : '');
path = Path.join(dirname, basename + '.json');
FS.writeFileSync(path, json, 'utf8');
});
}
function streamInOut(input, output, indent) {
var splitIntoDocs = new streams.CharacterGateStream('}]');
var transcoder = new streams.JSON5toJSONstream();
input.pipe(splitIntoDocs)
// .pipe(streams.LoggerStream('split'))
.pipe(transcoder)
// .pipe(streams.LoggerStream('json'))
.pipe(output);
}
function main() {
if (argv.c) return compileFiles(argv._, argv.indent);
if (argv.stdin) return streamInOut(process.stdin, process.stdout, argv.indent);
}
main();