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