UNPKG

1.98 kBJavaScriptView Raw
1#!/usr/bin/env node
2'use strict';
3/**
4 * json2xml module
5 * @module json2xml
6 * @see module:index
7 */
8const commander = require('commander');
9const fs = require('fs');
10const path = require('path');
11const XMLLite = require('../lib/index');
12
13// xml2json
14commander
15 .command('xml2json [xml]')
16 .description('converting xml to json')
17 .option('--replacer [replacer]', 'replacer')
18 .option('--space [space]', 'space')
19 .action((xml, options) => {
20 fs.readFile(path.resolve(process.cwd(), xml), 'utf8', (err, content) => {
21 if (err) throw err;
22 console.log(XMLLite.xml2json(content, options.replacer, options.space));
23 });
24 });
25
26// json2xml
27commander
28 .command('json2xml [json]')
29 .description('converting json to xml')
30 .option('--beautify', 'beautify')
31 .option('--indent [indent]', 'indent', ' ')
32 .option('--uglify', 'uglify')
33 .option('--preserveComments', 'preserve comments')
34 .action((json, options) => {
35 fs.readFile(path.resolve(process.cwd(), json), 'utf8', (err, content) => {
36 if (err) throw err;
37 console.log(XMLLite.json2xml(content, options));
38 });
39 });
40
41// beautify
42commander
43 .command('beautify [xml]')
44 .description('beautifying an xml file')
45 .option('--indent [indent]', 'indent', ' ')
46 .action((xml, options) => {
47 fs.readFile(path.resolve(process.cwd(), xml), 'utf8', (err, content) => {
48 if (err) throw err;
49 console.log(XMLLite.beautify(content, options.indent));
50 // XMLLite.beautify(content, options.indent);
51 });
52 });
53
54// uglify
55commander
56 .command('uglify [xml]')
57 .description('uglifying an xml file')
58 .option('--preserveComments', 'preserve comments')
59 .action((xml, options) => {
60 fs.readFile(path.resolve(process.cwd(), xml), 'utf8', (err, content) => {
61 if (err) throw err;
62 console.log(XMLLite.uglify(content, options.preserveComments));
63 });
64 });
65
66// execute client
67commander.parse(process.argv);
68
69if (process.argv.length === 2) commander.outputHelp();