Переглянути джерело

Add xml output option to the CLI

Gaël Métais 9 роки тому
батько
коміт
1a444a481e
2 змінених файлів з 20 додано та 5 видалено
  1. 19 5
      bin/cli.js
  2. 1 0
      package.json

+ 19 - 5
bin/cli.js

@@ -1,10 +1,11 @@
 #!/usr/bin/env node
 
-var debug = require('debug')('ylt:cli');
-var meow = require('meow');
-var path = require('path');
+var debug       = require('debug')('ylt:cli');
+var meow        = require('meow');
+var path        = require('path');
+var jstoxml     = require('jstoxml');
 
-var ylt = require('../lib/index');
+var ylt         = require('../lib/index');
 
 var cli = meow({
     help: [
@@ -19,6 +20,7 @@ var cli = meow({
         '  --cookie             Adds a cookie on the main domain.',
         '  --auth-user          Basic HTTP authentication username.',
         '  --auth-pass          Basic HTTP authentication password.',
+        '  --reporter           The output format: "json" or "xml". Default is "json".',
         ''
     ].join('\n'),
     pkg: '../package.json'
@@ -67,6 +69,12 @@ options.cookie = cli.flags.cookie || null;
 options.authUser = cli.flags.authUser || null;
 options.authPass = cli.flags.authPass || null;
 
+// Output format
+if (cli.flags.reporter && cli.flags.reporter !== 'json' && cli.flags.reporter !== 'xml') {
+    console.error('Incorrect parameters: reporter has to be "json" or "xml"');
+    process.exit(1);
+}
+
 
 (function execute(url, options) {
     'use strict';
@@ -76,7 +84,13 @@ options.authPass = cli.flags.authPass || null;
         then(function(data) {
 
             debug('Success');
-            console.log(JSON.stringify(data, null, 2));
+            switch(cli.flags.reporter) {
+                case 'xml':
+                    console.log(jstoxml.toXML(data, {indent: '  '}));
+                    break;
+                default:
+                    console.log(JSON.stringify(data, null, 2));
+            }
 
         }).fail(function(err) {
             

+ 1 - 0
package.json

@@ -34,6 +34,7 @@
     "express": "4.13.3",
     "imagemin": "3.2.0",
     "imagemin-jpegoptim": "4.0.0",
+    "jstoxml": "0.2.3",
     "lwip": "0.0.7",
     "meow": "3.3.0",
     "minimize": "1.7.0",