cli.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/usr/bin/env node
  2. 'use strict';
  3. /*eslint-disable no-console*/
  4. import fs from 'fs';
  5. import { resolve } from 'path';
  6. import {XMLParser, XMLValidator} from "../fxp.js";
  7. import ReadToEnd from './read.js';
  8. import cmdDetail from "./man.js"
  9. console.warn("\x1b[33m%s\x1b[0m", "⚠️ Warning: The built-in CLI interface is now deprecated.");
  10. console.warn("Please install the dedicated CLI package instead:");
  11. console.warn(" npm install -g fxp-cli");
  12. if (process.argv[2] === '--help' || process.argv[2] === '-h') {
  13. console.log(cmdDetail);
  14. } else if (process.argv[2] === '--version') {
  15. const packageJsonPath = resolve(process.cwd(), 'package.json');
  16. const version = JSON.parse(fs.readFileSync(packageJsonPath).toString()).version;
  17. console.log(version);
  18. } else {
  19. const options = {
  20. removeNSPrefix: true,
  21. ignoreAttributes: false,
  22. parseTagValue: true,
  23. parseAttributeValue: true,
  24. };
  25. let fileName = '';
  26. let outputFileName;
  27. let validate = false;
  28. let validateOnly = false;
  29. for (let i = 2; i < process.argv.length; i++) {
  30. if (process.argv[i] === '-ns') {
  31. options.removeNSPrefix = false;
  32. } else if (process.argv[i] === '-a') {
  33. options.ignoreAttributes = true;
  34. } else if (process.argv[i] === '-c') {
  35. options.parseTagValue = false;
  36. options.parseAttributeValue = false;
  37. } else if (process.argv[i] === '-o') {
  38. outputFileName = process.argv[++i];
  39. } else if (process.argv[i] === '-v') {
  40. validate = true;
  41. } else if (process.argv[i] === '-V') {
  42. validateOnly = true;
  43. } else {
  44. //filename
  45. fileName = process.argv[i];
  46. }
  47. }
  48. const callback = function(xmlData) {
  49. let output = '';
  50. if (validateOnly) {
  51. output = XMLValidator.validate(xmlData);
  52. process.exitCode = output === true ? 0 : 1;
  53. } else {
  54. const parser = new XMLParser(options);
  55. output = JSON.stringify(parser.parse(xmlData,validate), null, 4);
  56. }
  57. if (outputFileName) {
  58. writeToFile(outputFileName, output);
  59. } else {
  60. console.log(output);
  61. }
  62. };
  63. try {
  64. if (!fileName) {
  65. ReadToEnd.readToEnd(process.stdin, function(err, data) {
  66. if (err) {
  67. throw err;
  68. }
  69. callback(data.toString());
  70. });
  71. } else {
  72. fs.readFile(fileName, function(err, data) {
  73. if (err) {
  74. throw err;
  75. }
  76. callback(data.toString());
  77. });
  78. }
  79. } catch (e) {
  80. console.log('Seems an invalid file or stream.' + e);
  81. }
  82. }
  83. function writeToFile(fileName, data) {
  84. fs.writeFile(fileName, data, function(err) {
  85. if (err) {
  86. throw err;
  87. }
  88. console.log('JSON output has been written to ' + fileName);
  89. });
  90. }