-
Notifications
You must be signed in to change notification settings - Fork 5
/
cli.js
executable file
·34 lines (31 loc) · 736 Bytes
/
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
#!/usr/bin/env node
"use strict";
const {readFile} = require('fs');
const {yamlDump, yamlParse} = require('.');
function main(filePath) {
return readFile(filePath, 'utf8', (err, text) => {
if (err) {
console.log(`Error reading ${filePath}: ${err}`);
return;
}
let isYaml, data;
try {
data = JSON.parse(text);
isYaml = false;
} catch (e) {
data = yamlParse(text);
isYaml = true;
}
console.log(isYaml ?
JSON.stringify(data, null, 2) :
yamlDump(data));
});
}
if (require.main === module) {
if (process.argv.length !== 3) {
console.log(`Usage: ${process.argv[1]} <JSON-or-YAML-file>`);
process.exit(1);
} else {
main(process.argv[2]);
}
}