6 /*eslint-disable no-console*/
10 var fs = require('fs');
14 var argparse = require('argparse');
18 var yaml = require('..');
21 ////////////////////////////////////////////////////////////////////////////////
24 var cli = new argparse.ArgumentParser({
26 version: require('../package.json').version,
31 cli.addArgument([ '-c', '--compact' ], {
32 help: 'Display errors in compact mode',
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,
46 cli.addArgument([ '-t', '--trace' ], {
47 help: 'Show stack trace on error',
51 cli.addArgument([ 'file' ], {
52 help: 'File to read, utf-8 encoded without BOM',
58 ////////////////////////////////////////////////////////////////////////////////
61 var options = cli.parseArgs();
64 ////////////////////////////////////////////////////////////////////////////////
66 function readFile(filename, encoding, callback) {
67 if (options.file === '-') {
72 process.stdin.on('data', function (chunk) {
76 process.stdin.on('end', function () {
77 return callback(null, Buffer.concat(chunks).toString(encoding));
80 fs.readFile(filename, encoding, callback);
84 readFile(options.file, 'utf8', function (error, input) {
88 if (error.code === 'ENOENT') {
89 console.error('File not found: ' + options.file);
94 options.trace && error.stack ||
102 output = JSON.parse(input);
105 if (err instanceof SyntaxError) {
108 yaml.loadAll(input, function (doc) { output.push(doc); }, {});
111 if (output.length === 0) output = null;
112 else if (output.length === 1) output = output[0];
115 if (options.trace && err.stack) console.error(e.stack);
116 else console.error(e.toString(options.compact));
122 options.trace && err.stack ||
130 if (isYaml) console.log(JSON.stringify(output, null, ' '));
131 else console.log(yaml.dump(output));