js-yaml.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/usr/bin/env node
  2. 'use strict';
  3. /*eslint-disable no-console*/
  4. // stdlib
  5. var fs = require('fs');
  6. // 3rd-party
  7. var argparse = require('argparse');
  8. // internal
  9. var yaml = require('..');
  10. ////////////////////////////////////////////////////////////////////////////////
  11. var cli = new argparse.ArgumentParser({
  12. prog: 'js-yaml',
  13. version: require('../package.json').version,
  14. addHelp: true
  15. });
  16. cli.addArgument([ '-c', '--compact' ], {
  17. help: 'Display errors in compact mode',
  18. action: 'storeTrue'
  19. });
  20. // deprecated (not needed after we removed output colors)
  21. // option suppressed, but not completely removed for compatibility
  22. cli.addArgument([ '-j', '--to-json' ], {
  23. help: argparse.Const.SUPPRESS,
  24. dest: 'json',
  25. action: 'storeTrue'
  26. });
  27. cli.addArgument([ '-t', '--trace' ], {
  28. help: 'Show stack trace on error',
  29. action: 'storeTrue'
  30. });
  31. cli.addArgument([ 'file' ], {
  32. help: 'File to read, utf-8 encoded without BOM',
  33. nargs: '?',
  34. defaultValue: '-'
  35. });
  36. ////////////////////////////////////////////////////////////////////////////////
  37. var options = cli.parseArgs();
  38. ////////////////////////////////////////////////////////////////////////////////
  39. function readFile(filename, encoding, callback) {
  40. if (options.file === '-') {
  41. // read from stdin
  42. var chunks = [];
  43. process.stdin.on('data', function (chunk) {
  44. chunks.push(chunk);
  45. });
  46. process.stdin.on('end', function () {
  47. return callback(null, Buffer.concat(chunks).toString(encoding));
  48. });
  49. } else {
  50. fs.readFile(filename, encoding, callback);
  51. }
  52. }
  53. readFile(options.file, 'utf8', function (error, input) {
  54. var output, isYaml;
  55. if (error) {
  56. if (error.code === 'ENOENT') {
  57. console.error('File not found: ' + options.file);
  58. process.exit(2);
  59. }
  60. console.error(
  61. options.trace && error.stack ||
  62. error.message ||
  63. String(error));
  64. process.exit(1);
  65. }
  66. try {
  67. output = JSON.parse(input);
  68. isYaml = false;
  69. } catch (err) {
  70. if (err instanceof SyntaxError) {
  71. try {
  72. output = [];
  73. yaml.loadAll(input, function (doc) { output.push(doc); }, {});
  74. isYaml = true;
  75. if (output.length === 0) output = null;
  76. else if (output.length === 1) output = output[0];
  77. } catch (e) {
  78. if (options.trace && err.stack) console.error(e.stack);
  79. else console.error(e.toString(options.compact));
  80. process.exit(1);
  81. }
  82. } else {
  83. console.error(
  84. options.trace && err.stack ||
  85. err.message ||
  86. String(err));
  87. process.exit(1);
  88. }
  89. }
  90. if (isYaml) console.log(JSON.stringify(output, null, ' '));
  91. else console.log(yaml.dump(output));
  92. });