regexpu 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/usr/bin/env node
  2. (function() {
  3. var fs = require('fs');
  4. var jsesc = require('jsesc');
  5. var regexpu = require('../regexpu.js');
  6. var strings = process.argv.splice(2);
  7. var option = strings.shift();
  8. var stdin = process.stdin;
  9. var isFile = false;
  10. var data;
  11. var timeout;
  12. var options = {};
  13. var log = console.log;
  14. var main = function() {
  15. if (/^(?:-h|--help|undefined)$/.test(option)) {
  16. log(
  17. 'regexpu v%s - https://mths.be/regexpu',
  18. regexpu.version
  19. );
  20. log([
  21. '\nUsage:\n',
  22. '\tregexpu [-c | --code] [snippet ...]',
  23. '\tregexpu [-f | --file] [file ...]',
  24. '\tregexpu [-v | --version]',
  25. '\tregexpu [-h | --help]',
  26. '\nExamples:\n',
  27. '\tregexpu -f foo-es6.js > foo-es5.js',
  28. '\tregexpu -c \'var x = /foo.bar/u;\'',
  29. '\techo \'var x = /foo.bar/u;\' | regexpu -c'
  30. ].join('\n'));
  31. return process.exit(1);
  32. }
  33. if (/^(?:-v|--version)$/.test(option)) {
  34. log('v%s', regexpu.version);
  35. return process.exit(1);
  36. }
  37. if (/^(?:-f|--file)$/.test(option)) {
  38. isFile = true;
  39. } else if (!/^(?:-c|--code)$/.test(option)) {
  40. log('Unrecognized option `%s`.', option);
  41. log('Try `regexpu --help` for more information.');
  42. return process.exit(1);
  43. }
  44. if (!strings.length) {
  45. log('Error: option `%s` requires an argument.', option);
  46. log('Try `regexpu --help` for more information.');
  47. return process.exit(1);
  48. }
  49. strings.forEach(function(snippet) {
  50. var result;
  51. if (isFile) {
  52. try {
  53. snippet = fs.readFileSync(snippet, 'utf8');
  54. } catch (exception) {
  55. log('Error: no such file. (`%s`)', snippet);
  56. return process.exit(1);
  57. }
  58. }
  59. try {
  60. result = regexpu.transpileCode(snippet);
  61. log(result);
  62. } catch (exception) {
  63. log(exception.message + '\n');
  64. log('Error: failed to transpile. Make sure the JavaScript code is valid.');
  65. log('If you think this is a bug in regexpu, please report it:');
  66. log('https://github.com/mathiasbynens/regexpu/issues/new');
  67. log('\nStack trace using regexpu@%s:\n', regexpu.version);
  68. log(exception.stack);
  69. return process.exit(1);
  70. }
  71. });
  72. // Return with exit status 0 outside of the `forEach` loop, in case
  73. // multiple snippets or files were passed in.
  74. return process.exit(0);
  75. };
  76. if (stdin.isTTY) {
  77. // handle shell arguments
  78. main();
  79. } else {
  80. // Either the script is called from within a non-TTY context, or `stdin`
  81. // content is being piped in.
  82. if (!process.stdout.isTTY) {
  83. // The script was called from a non-TTY context. This is a rather uncommon
  84. // use case we don’t actively support. However, we don’t want the script
  85. // to wait forever in such cases, so…
  86. timeout = setTimeout(function() {
  87. // …if no piped data arrived after a whole minute, handle shell
  88. // arguments instead.
  89. main();
  90. }, 60000);
  91. }
  92. data = '';
  93. stdin.on('data', function(chunk) {
  94. clearTimeout(timeout);
  95. data += chunk;
  96. });
  97. stdin.on('end', function() {
  98. strings.push(data.trim());
  99. main();
  100. });
  101. stdin.resume();
  102. }
  103. }());