parser 1.3 KB

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