cli.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
  5. }) : (function(o, m, k, k2) {
  6. if (k2 === undefined) k2 = k;
  7. o[k2] = m[k];
  8. }));
  9. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  10. Object.defineProperty(o, "default", { enumerable: true, value: v });
  11. }) : function(o, v) {
  12. o["default"] = v;
  13. });
  14. var __importStar = (this && this.__importStar) || function (mod) {
  15. if (mod && mod.__esModule) return mod;
  16. var result = {};
  17. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  18. __setModuleDefault(result, mod);
  19. return result;
  20. };
  21. var __importDefault = (this && this.__importDefault) || function (mod) {
  22. return (mod && mod.__esModule) ? mod : { "default": mod };
  23. };
  24. Object.defineProperty(exports, "__esModule", { value: true });
  25. var fs = __importStar(require("mz/fs"));
  26. var path = __importStar(require("path"));
  27. var yargs_1 = __importDefault(require("yargs"));
  28. var _1 = require(".");
  29. var theme_1 = require("./theme");
  30. yargs_1.default
  31. .option('theme', {
  32. alias: 't',
  33. nargs: 1,
  34. description: 'Use a theme defined in a JSON file',
  35. })
  36. .usage(['', 'Usage: highlight [options] [file]', '', 'Outputs a file or STDIN input with syntax highlighting'].join('\n'))
  37. .option('language', {
  38. alias: 'l',
  39. nargs: 1,
  40. description: 'Set the langugage explicitely\nIf omitted will try to auto-detect',
  41. })
  42. .version()
  43. .help('help')
  44. .alias('help', 'h')
  45. .alias('version', 'v');
  46. var argv = yargs_1.default.argv;
  47. var file = argv._[0];
  48. var codePromise;
  49. if (!file && !process.stdin.isTTY) {
  50. // Input from STDIN
  51. process.stdin.setEncoding('utf8');
  52. var code_1 = '';
  53. process.stdin.on('readable', function () {
  54. var chunk = process.stdin.read();
  55. if (chunk !== null) {
  56. code_1 += chunk;
  57. }
  58. });
  59. codePromise = new Promise(function (resolve) {
  60. process.stdin.on('end', function () {
  61. var chunk = process.stdin.read();
  62. if (chunk !== null) {
  63. code_1 += chunk;
  64. }
  65. resolve(code_1);
  66. });
  67. });
  68. }
  69. else if (file) {
  70. // Read file
  71. codePromise = fs.readFile(file, 'utf-8');
  72. }
  73. else {
  74. yargs_1.default.showHelp();
  75. process.exit(1);
  76. }
  77. Promise.all([codePromise, argv.theme ? fs.readFile(argv.theme, 'utf8') : undefined])
  78. .then(function (_a) {
  79. var code = _a[0], theme = _a[1];
  80. var options = {
  81. ignoreIllegals: true,
  82. theme: (theme && theme_1.parse(theme)) || undefined,
  83. };
  84. if (file) {
  85. var extension = path.extname(file).slice(1);
  86. if (extension && _1.supportsLanguage(extension)) {
  87. options.language = extension;
  88. }
  89. }
  90. options.language = argv.language;
  91. return new Promise(function (resolve, reject) {
  92. return process.stdout.write(_1.highlight(code, options), function (error) { return (error ? reject(error) : resolve()); });
  93. });
  94. })
  95. .then(function () {
  96. process.exit(0);
  97. })
  98. .catch(function (error) {
  99. console.error(error);
  100. process.exit(1);
  101. });
  102. //# sourceMappingURL=cli.js.map