-
Notifications
You must be signed in to change notification settings - Fork 37
/
cli.js
executable file
·85 lines (67 loc) · 1.35 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
83
84
85
#!/usr/bin/env node
(function () {
"use strict";
var fs = require('fs')
, filename = process.argv[2]
, YAML = require('./index')
;
/*
*
* Begin real handler
*
*/
function printUsage() {
console.warn("Usages:");
console.warn("json2yaml example.json");
console.warn("cat example.json | json2yaml");
}
function handleInput(err, text) {
var data
;
if (err) {
printUsage();
return;
}
data = JSON.parse(text);
console.info(YAML.stringify(data, null, ' '));
}
/*
*
* End real handler
*
*/
readInput(handleInput, filename);
//
// this could (and probably should) be its own module
//
function readInput(cb, filename) {
function readFile() {
fs.readFile(filename, 'utf8', function (err, text) {
if (err) {
console.error("[ERROR] couldn't read from '" + filename + "':");
console.error(err.message);
return;
}
cb(err, text);
});
}
function readStdin() {
var text = ''
, stdin = process.stdin
;
stdin.resume();
stdin.on('data', function (chunk) {
text += chunk;
});
stdin.on('end', function () {
cb(null, text);
});
}
if (filename) {
readFile();
}
else {
readStdin();
}
}
}());