电速宝智配引擎
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cli.js 5.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/usr/bin/env node
  2. var fs = require('fs');
  3. var helper = require('./cli-helper');
  4. var project = require('../package.json');
  5. var xml2json = require('../lib/xml2json');
  6. var json2xml = require('../lib/json2xml');
  7. var output = '';
  8. var stream = '';
  9. var options = {};
  10. var requiredArgs = [
  11. { arg: 'src', type: 'file', option: 'src', desc: 'Input file that need to be converted.'}
  12. ];
  13. var optionalArgs = [
  14. { arg: 'help', alias: 'h', type: 'flag', option: 'help', desc: 'Display this help content.' },
  15. { arg: 'version', alias: 'v', type: 'flag', option: 'version', desc: 'Display version number of this module.' },
  16. { arg: 'out', type: 'file', option: 'out', desc: 'Output file where the converted result should be written.' },
  17. { arg: 'to-json', type: 'flag', option:'toJason', desc: 'Convert.' },
  18. { arg: 'compact', type: 'flag', option:'compact', desc: 'Compact JSON form (see explanation in www.npmjs.com/package/xml-js).' },
  19. { arg: 'spaces', type: 'number', option:'spaces', desc: 'Specifies amount of space indentation in the output.' },
  20. { arg: 'trim', type: 'flag', option:'trim', desc: 'Any whitespaces surrounding texts will be trimmed.' },
  21. // { arg: 'sanitize', type: 'flag', option:'sanitize', desc: 'Special xml characters will be replaced with entity codes.' },
  22. { arg: 'native-type', type: 'flag', option:'nativeType', desc: 'Numbers and boolean will be converted (coerced) to native type instead of text.' },
  23. { arg: 'always-array', type: 'flag', option:'alwaysArray', desc: 'Every element will always be an array type (applicable if --compact is set). If the passed value is an array, only elements with names in the passed array are always made arrays.' },
  24. { arg: 'always-children', type: 'flag', option:'alwaysChildren', desc: 'Every element will always contain sub-elements (applicable if --compact is not set).' },
  25. { arg: 'instruction-attr', type: 'flag', option:'instructionHasAttributes', desc: 'Whether to parse contents of processing instruction as attributes.' },
  26. { arg: 'full-tag', type: 'flag', option:'fullTagEmptyElement', desc: 'XML elements will always be in <a></a> form.' },
  27. { arg: 'no-decl', type: 'flag', option:'ignoreDeclaration', desc: 'Declaration instruction <?xml?> will be ignored.' },
  28. { arg: 'no-decl', type: 'flag', option:'ignoreInstruction', desc: 'Processing instruction <?...?> will be ignored.' },
  29. { arg: 'no-attr', type: 'flag', option:'ignoreAttributes', desc: 'Attributes of elements will be ignored.' },
  30. { arg: 'no-text', type: 'flag', option:'ignoreText', desc: 'Texts of elements will be ignored.' },
  31. { arg: 'no-cdata', type: 'flag', option:'ignoreCdata', desc: 'CData of elements will be ignored.' },
  32. { arg: 'no-doctype', type: 'flag', option:'ignoreDoctype', desc: 'DOCTYPE of elements will be ignored.' },
  33. { arg: 'no-comment', type: 'flag', option:'ignoreComment', desc: 'Comments of elements will be ignored.' },
  34. { arg: 'text-key', type: 'string', option:'textKey', desc: 'To change the default \'text\' key.' },
  35. { arg: 'cdata-key', type: 'string', option:'cdataKey', desc: 'To change the default \'cdata\' key.' },
  36. { arg: 'doctype-key', type: 'string', option:'doctypeKey', desc: 'To change the default \'doctype\' key.' },
  37. { arg: 'comment-key', type: 'string', option:'commentKey', desc: 'To change the default \'comment\' key.' },
  38. { arg: 'attributes-key', type: 'string', option:'attributesKey', desc: 'To change the default \'attributes\' key.' },
  39. { arg: 'declaration-key', type: 'string', option:'declarationKey', desc: 'To change the default \'declaration\' key <?xml?>.' },
  40. { arg: 'instruction-key', type: 'string', option:'instructionKey', desc: 'To change the default \'processing instruction\' key <?...?>.' },
  41. { arg: 'type-key', type: 'string', option:'typeKey', desc: 'To change the default \'type\' key (applicable if --compact is not set).' },
  42. { arg: 'name-key', type: 'string', option:'nameKey', desc: 'To change the default \'name\' key (applicable if --compact is not set).' },
  43. { arg: 'elements-key', type: 'string', option:'elementsKey', desc: 'To change the default \'elements\' key (applicable if --compact is not set).' }
  44. ];
  45. process.stdin.setEncoding('utf8');
  46. process.stdin.on('readable', function () {
  47. var chunk = process.stdin.read();
  48. if (chunk !== null) {
  49. stream += chunk;
  50. }
  51. });
  52. process.stdin.on('end', function () {
  53. process.stdout.write(xml2json(stream, {}) + '\n');
  54. });
  55. options = helper.mapCommandLineArgs(requiredArgs, optionalArgs);
  56. if (options.version) {
  57. console.log(project.version);
  58. process.exit(0);
  59. } else if (options.help || process.argv.length <= 2 + requiredArgs.length - 1) {
  60. console.log(helper.getCommandLineHelp('xml-js', requiredArgs, optionalArgs));
  61. process.exit(process.argv.length <= 2 ? 1 : 0);
  62. } else if ('src' in options) {
  63. if (fs.statSync(options.src).isFile()) {
  64. if (options.src.split('.').pop() === 'xml') {
  65. output = xml2json(fs.readFileSync(options.src, 'utf8'), options);
  66. } else if (options.src.split('.').pop() === 'json') {
  67. output = json2xml(fs.readFileSync(options.src, 'utf8'), options);
  68. }
  69. if (options.out) {
  70. fs.writeFileSync(options.out, output, 'utf8');
  71. } else {
  72. console.log(output);
  73. }
  74. process.exit(0);
  75. }
  76. } else {
  77. process.exit(1);
  78. }