Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ engines:
enabled: true
config:
languages:
- ruby
- javascript
- python
- php
- ruby
- javascript
- python
- php
eslint:
enabled: true
fixme:
enabled: true
ratings:
paths:
- "**.inc"
- "**.js"
- "**.jsx"
- "**.module"
- "**.php"
- "**.py"
- "**.rb"
- "**.inc"
- "**.js"
- "**.jsx"
- "**.module"
- "**.php"
- "**.py"
- "**.rb"
exclude_paths:
- spec/
- lib/**/*
Expand Down
11 changes: 11 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"useTabs": false,
"printWidth": 120,
"tabWidth": 2,
"singleQuote": true,
"trailingComma": "es5",
"semi": true,
"arrowParens": "always",
"parser": "babylon",
"bracketSpacing": false
}
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ cache:
directories:
- "node_modules"
addons:
code_climate:
repo_token: ca31e4b1924e441790988993f307af5359d99d164d8308c39f81c81b8121f30f
code_climate:
repo_token: ca31e4b1924e441790988993f307af5359d99d164d8308c39f81c81b8121f30f
after_success:
- npm install -g codeclimate-test-reporter
- npm install -g coveralls
Expand Down
176 changes: 88 additions & 88 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,100 +1,100 @@
#!/usr/bin/env node
"use strict";
'use strict';
/*eslint-disable no-console*/
const fs = require("fs");
const path = require("path");
const parser = require("./src/parser");
const readToEnd = require("./src/read").readToEnd;
const fs = require('fs');
const path = require('path');
const parser = require('./src/parser');
const readToEnd = require('./src/read').readToEnd;

if (process.argv[2] === "--help" || process.argv[2] === "-h") {
console.log("Fast XML Parser " + require(path.join(__dirname + "/package.json")).version);
console.log("----------------");
console.log("xml2js [-ns|-a|-c|-v|-V] <filename> [-o outputfile.json]");
console.log("cat xmlfile.xml | xml2js [-ns|-a|-c|-v|-V] [-o outputfile.json]");
console.log("-ns: remove namespace from tag and atrribute name.");
console.log("-a: don't parse attributes.");
console.log("-c: parse values to premitive type.");
console.log("-v: validate before parsing.");
console.log("-V: validate only.");
} else if (process.argv[2] === "--version") {
console.log(require(path.join(__dirname + "/package.json")).version);
if (process.argv[2] === '--help' || process.argv[2] === '-h') {
console.log('Fast XML Parser ' + require(path.join(__dirname + '/package.json')).version);
console.log('----------------');
console.log('xml2js [-ns|-a|-c|-v|-V] <filename> [-o outputfile.json]');
console.log('cat xmlfile.xml | xml2js [-ns|-a|-c|-v|-V] [-o outputfile.json]');
console.log('-ns: remove namespace from tag and atrribute name.');
console.log("-a: don't parse attributes.");
console.log('-c: parse values to premitive type.');
console.log('-v: validate before parsing.');
console.log('-V: validate only.');
} else if (process.argv[2] === '--version') {
console.log(require(path.join(__dirname + '/package.json')).version);
} else {
const options = {
ignoreNameSpace: true,
ignoreAttributes: false,
parseNodeValue: true,
parseAttributeValue: true
};
let fileName = "";
let outputFileName;
let validate = false;
let validateOnly = false;
for (let i = 2; i < process.argv.length; i++) {
if (process.argv[i] === "-ns") {
options.ignoreNameSpace = false;
} else if (process.argv[i] === "-a") {
options.ignoreAttributes = true;
} else if (process.argv[i] === "-c") {
options.parseNodeValue = false;
options.parseAttributeValue = false;
} else if (process.argv[i] === "-o") {
outputFileName = process.argv[++i];
} else if (process.argv[i] === "-v") {
validate = true;
} else if (process.argv[i] === "-V") {
validateOnly = true;
} else {//filename
fileName = process.argv[i];
}
const options = {
ignoreNameSpace: true,
ignoreAttributes: false,
parseNodeValue: true,
parseAttributeValue: true,
};
let fileName = '';
let outputFileName;
let validate = false;
let validateOnly = false;
for (let i = 2; i < process.argv.length; i++) {
if (process.argv[i] === '-ns') {
options.ignoreNameSpace = false;
} else if (process.argv[i] === '-a') {
options.ignoreAttributes = true;
} else if (process.argv[i] === '-c') {
options.parseNodeValue = false;
options.parseAttributeValue = false;
} else if (process.argv[i] === '-o') {
outputFileName = process.argv[++i];
} else if (process.argv[i] === '-v') {
validate = true;
} else if (process.argv[i] === '-V') {
validateOnly = true;
} else {
//filename
fileName = process.argv[i];
}
const callback = function(xmlData) {
let output = "";
if (validate) {
const result = parser.validate(xmlData);
if (result === true) {
output = JSON.stringify(parser.parse(xmlData, options), null, 4);
} else {
output = result;
}
} else if (validateOnly) {
output = parser.validate(xmlData);
} else {
output = JSON.stringify(parser.parse(xmlData, options), null, 4);
}
if (outputFileName) {
writeToFile(outputFileName, output);
} else {
console.log(output);
}
};
}
const callback = function(xmlData) {
let output = '';
if (validate) {
const result = parser.validate(xmlData);
if (result === true) {
output = JSON.stringify(parser.parse(xmlData, options), null, 4);
} else {
output = result;
}
} else if (validateOnly) {
output = parser.validate(xmlData);
} else {
output = JSON.stringify(parser.parse(xmlData, options), null, 4);
}
if (outputFileName) {
writeToFile(outputFileName, output);
} else {
console.log(output);
}
};

try {
if (!fileName) {
readToEnd(process.stdin, function(err, data) {
if (err) {
throw err;
}
callback(data.toString());
});
} else {
fs.readFile(fileName, function(err, data) {
if (err) {
throw err;
}
callback(data.toString());
});
try {
if (!fileName) {
readToEnd(process.stdin, function(err, data) {
if (err) {
throw err;
}
callback(data.toString());
});
} else {
fs.readFile(fileName, function(err, data) {
if (err) {
throw err;
}
callback(data.toString());
});
}
catch (e) {
console.log("Seems an invalid file or stream." + e);
}
} catch (e) {
console.log('Seems an invalid file or stream.' + e);
}
}

function writeToFile(fileName, data) {
fs.writeFile(fileName, data, function(err) {
if (err) {
throw err;
}
console.log('JSON output has been written to ' + fileName);
});
fs.writeFile(fileName, data, function(err) {
if (err) {
throw err;
}
console.log('JSON output has been written to ' + fileName);
});
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"bundle": "webpack && webpack --config webpack-prod.config.js",
"coverage": "istanbul cover -x \"cli.js\" -x \"spec/*spec.js\" jasmine spec/*spec.js;",
"coverage:check": "istanbul check-coverage --branch 90 --statement 90",
"postinstall": "node tasks/postinstall.js"
"postinstall": "node tasks/postinstall.js",
"prettier": "prettier --write src/**/*.js"
},
"bin": {
"xml2js": "./cli.js"
Expand Down Expand Up @@ -78,6 +79,7 @@
"istanbul": "^0.4.5",
"jasmine": "^3.3.0",
"portfinder": "^1.0.19",
"prettier": "^1.15.3",
"webpack": "^4.17.3",
"webpack-cli": "^3.1.2",
"xml2js": "^0.4.19",
Expand Down
Loading