Initial commit
[yaffs-website] / node_modules / js-yaml / bin / js-yaml.js
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 (err) {
105     if (err instanceof SyntaxError) {
106       try {
107         output = [];
108         yaml.loadAll(input, function (doc) { output.push(doc); }, {});
109         isYaml = true;
110
111         if (output.length === 0) output = null;
112         else if (output.length === 1) output = output[0];
113
114       } catch (e) {
115         if (options.trace && err.stack) console.error(e.stack);
116         else console.error(e.toString(options.compact));
117
118         process.exit(1);
119       }
120     } else {
121       console.error(
122         options.trace && err.stack ||
123         err.message ||
124         String(err));
125
126       process.exit(1);
127     }
128   }
129
130   if (isYaml) console.log(JSON.stringify(output, null, '  '));
131   else console.log(yaml.dump(output));
132 });