1 | #!/usr/bin/env node
|
2 | 'use strict';
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | const commander = require('commander');
|
9 | const fs = require('fs');
|
10 | const path = require('path');
|
11 | const XMLLite = require('../lib/index');
|
12 |
|
13 |
|
14 | commander
|
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 |
|
27 | commander
|
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 |
|
42 | commander
|
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 |
|
51 | });
|
52 | });
|
53 |
|
54 |
|
55 | commander
|
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 |
|
67 | commander.parse(process.argv);
|
68 |
|
69 | if (process.argv.length === 2) commander.outputHelp();
|