You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
2.7 KiB

4 years ago
  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. });