Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80684 views
1
#!/usr/bin/env node
2
3
4
'use strict';
5
6
/*eslint-disable no-console*/
7
8
9
// stdlib
10
var fs = require('fs');
11
12
13
// 3rd-party
14
var argparse = require('argparse');
15
16
17
// internal
18
var yaml = require('..');
19
20
21
////////////////////////////////////////////////////////////////////////////////
22
23
24
var cli = new argparse.ArgumentParser({
25
prog: 'js-yaml',
26
version: require('../package.json').version,
27
addHelp: true
28
});
29
30
31
cli.addArgument([ '-c', '--compact' ], {
32
help: 'Display errors in compact mode',
33
action: 'storeTrue'
34
});
35
36
37
// deprecated (not needed after we removed output colors)
38
// option suppressed, but not completely removed for compatibility
39
cli.addArgument([ '-j', '--to-json' ], {
40
help: argparse.Const.SUPPRESS,
41
dest: 'json',
42
action: 'storeTrue'
43
});
44
45
46
cli.addArgument([ '-t', '--trace' ], {
47
help: 'Show stack trace on error',
48
action: 'storeTrue'
49
});
50
51
cli.addArgument([ 'file' ], {
52
help: 'File to read, utf-8 encoded without BOM',
53
nargs: '?',
54
defaultValue: '-'
55
});
56
57
58
////////////////////////////////////////////////////////////////////////////////
59
60
61
var options = cli.parseArgs();
62
63
64
////////////////////////////////////////////////////////////////////////////////
65
66
function readFile(filename, encoding, callback) {
67
if (options.file === '-') {
68
// read from stdin
69
70
var chunks = [];
71
72
process.stdin.on('data', function (chunk) {
73
chunks.push(chunk);
74
});
75
76
process.stdin.on('end', function () {
77
return callback(null, Buffer.concat(chunks).toString(encoding));
78
});
79
} else {
80
fs.readFile(filename, encoding, callback);
81
}
82
}
83
84
readFile(options.file, 'utf8', function (error, input) {
85
var output, isYaml;
86
87
if (error) {
88
if (error.code === 'ENOENT') {
89
console.error('File not found: ' + options.file);
90
process.exit(2);
91
}
92
93
console.error(
94
options.trace && error.stack ||
95
error.message ||
96
String(error));
97
98
process.exit(1);
99
}
100
101
try {
102
output = JSON.parse(input);
103
isYaml = false;
104
} catch (error) {
105
if (error instanceof SyntaxError) {
106
try {
107
output = [];
108
yaml.loadAll(input, function (doc) { output.push(doc); }, {});
109
isYaml = true;
110
111
if (0 === output.length) {
112
output = null;
113
} else if (1 === output.length) {
114
output = output[0];
115
}
116
} catch (error) {
117
if (options.trace && error.stack) {
118
console.error(error.stack);
119
} else {
120
console.error(error.toString(options.compact));
121
}
122
123
process.exit(1);
124
}
125
} else {
126
console.error(
127
options.trace && error.stack ||
128
error.message ||
129
String(error));
130
131
process.exit(1);
132
}
133
}
134
135
if (isYaml) {
136
console.log(JSON.stringify(output, null, ' '));
137
} else {
138
console.log(yaml.dump(output));
139
}
140
141
process.exit(0);
142
});
143
144