UNPKG

1.32 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3var sg = require('sgsg');
4var _ = sg._;
5var getStdin = require('get-stdin');
6var parseString = require('xml2js').parseString;
7var path = require('path');
8
9var ARGV = sg.ARGV();
10
11var 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 /* otherwise */
30 return mod[script](ARGV, {}, function(err) {
31 if (err) {
32 console.error(err);
33 process.exit(1);
34 return;
35 }
36
37 /* otherwise -- success */
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
47var lib = {};
48
49lib.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
57main();
58