1 | #!/usr/bin/env node
|
2 |
|
3 | var sg = require('sgsg');
|
4 | var _ = sg._;
|
5 | var getStdin = require('get-stdin');
|
6 | var parseString = require('xml2js').parseString;
|
7 | var path = require('path');
|
8 |
|
9 | var ARGV = sg.ARGV();
|
10 |
|
11 | var main = function() {
|
12 | var cmd, mod, script, modName;
|
13 |
|
14 | var cmd = ARGV.args.shift();
|
15 | if (lib[cmd]) {
|
16 | return lib[cmd](ARGV);
|
17 | } else if (cmd === 'run') {
|
18 |
|
19 | modName = ARGV.args.shift();
|
20 | mod = require(path.join(process.cwd(), modName));
|
21 | script = ARGV.args.shift();
|
22 |
|
23 | if (!mod[script]) {
|
24 | console.error('No such script ' + script + ' in ' + modName);
|
25 | process.exit(1);
|
26 | return;
|
27 | }
|
28 |
|
29 |
|
30 | return mod[script](ARGV, {}, function(err) {
|
31 | if (err) {
|
32 | console.error(err);
|
33 | process.exit(1);
|
34 | return;
|
35 | }
|
36 |
|
37 |
|
38 | console.log(sg.inspect(_.rest(arguments)));
|
39 | process.exit(0);
|
40 | });
|
41 | }
|
42 |
|
43 | console.error('Error: unknown');
|
44 | process.exit(1);
|
45 | };
|
46 |
|
47 | var lib = {};
|
48 |
|
49 | lib.xml2json = lib.xj = function(argv) {
|
50 | return getStdin().then(function(xml) {
|
51 | return parseString(xml, function (err, result) {
|
52 | process.stdout.write(JSON.stringify(result)+'\n');
|
53 | });
|
54 | });
|
55 | };
|
56 |
|
57 | main();
|
58 |
|