From 1a444a481ecc61a768e37890441fab2910245236 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20M=C3=A9tais?= Date: Thu, 10 Sep 2015 18:42:19 +0200 Subject: [PATCH] Add xml output option to the CLI --- bin/cli.js | 24 +++++++++++++++++++----- package.json | 1 + 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/bin/cli.js b/bin/cli.js index 58a8fb4..54cfbe8 100644 --- a/bin/cli.js +++ b/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) { diff --git a/package.json b/package.json index 4be3d98..15c2ee5 100644 --- a/package.json +++ b/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",