parser 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env node
  2. (function() {
  3. var fs = require('fs');
  4. var path = require('path');
  5. var parse = require('../parser').parse;
  6. var jsesc = require('jsesc');
  7. var regexes = process.argv.splice(2);
  8. var first = regexes[0];
  9. var data;
  10. var log = console.log;
  11. var flags = '';
  12. var main = function() {
  13. if (/^(?:-h|--help|undefined)$/.test(first)) {
  14. log([
  15. '\nUsage:\n',
  16. '\tregjsparser [regex ...]',
  17. '\tregjsparser [-f | --flags] u [regex ...]',
  18. '\tregjsparser [-v | --version]',
  19. '\tregjsparser [-h | --help]',
  20. '\nExamples:\n',
  21. '\tregjsparser \'^foo.bar$\'',
  22. '\tregjsparser \'[a-zA-Z0-9]\''
  23. ].join('\n'));
  24. return process.exit(1);
  25. }
  26. if (/^(?:-v|--version)$/.test(first)) {
  27. log('v%s', require(path.resolve(__dirname, '../package.json')).version);
  28. return process.exit(1);
  29. }
  30. if (/^(?:-f|--flags)$/.test(first)) {
  31. flags = regexes[1];
  32. regexes = regexes.slice(2);
  33. }
  34. regexes.forEach(function(snippet) {
  35. try {
  36. result = parse(snippet, flags);
  37. log(jsesc(result, {
  38. 'json': true,
  39. 'compact': false,
  40. 'indent': '\t'
  41. }));
  42. } catch(error) {
  43. log(error.message + '\n');
  44. log('Error: failed to parse. Make sure the regular expression is valid.');
  45. log('If you think this is a bug in regjsparser, please report it:');
  46. log('\thttps://github.com/jviereck/regjsparser/issues/new');
  47. log('\nStack trace:\n');
  48. log(error.stack);
  49. return process.exit(1);
  50. }
  51. });
  52. // Return with exit status 0 outside of the `forEach` loop, in case
  53. // multiple regular expressions were passed in.
  54. return process.exit(0);
  55. };
  56. main();
  57. }());