extract.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env node
  2. // Imports
  3. const program = require('commander');
  4. const inquirer = require('inquirer');
  5. const file = require('./src/file');
  6. const cst = require('./src/constant');
  7. const timer = require('./src/timer');
  8. // Variables configs et globales
  9. const sourceFile = './data/dblp.xml';
  10. const outputFile = './data/extract.xml';
  11. let verbose = false;
  12. // Parametrage CLI
  13. program.version('1.2.1');
  14. program
  15. .option('-n, --name <string>', 'author\'s name for data extraction')
  16. .option('-f, --file <source>', 'path to the DBLP database in XML format', sourceFile)
  17. .option('-o, --output <source>', 'path for the XML file to be generated', outputFile)
  18. .option('-v, --verbose', 'displays more information', verbose);
  19. // Parse des arguments CLI
  20. program.parse(process.argv);
  21. const name = (program.name && typeof program.name === 'string') ? program.name : null;
  22. let source = program.file;
  23. let out = program.output;
  24. verbose = program.verbose;
  25. // Verification que le fichier du pre-traitement existe
  26. if (!file.exist(source)) {
  27. // Impossible de trouver le fichier XML à traiter
  28. console.error(cst.toString(cst.FILE_NOT_FOUND) + ': ' + source);
  29. process.exit(cst.FILE_NOT_FOUND);
  30. }
  31. // En cas d'absence de nom on le demande
  32. if (name === null) {
  33. inquirer.prompt({
  34. type: 'input',
  35. name: 'name',
  36. message: 'Enter the name of the author whose information you want to extract: >',
  37. }).then(answer => {
  38. // Lancement du prgramme
  39. extract(source, answer.name);
  40. });
  41. }
  42. // Sinon on lance le programme
  43. else {
  44. extract(source, name);
  45. }
  46. function extract(source, name) {
  47. name = name.trim();
  48. if (verbose) {
  49. console.info(`Extraction of ${name}'s information...`);
  50. timer.start();
  51. }
  52. const parser = require('./src/extract/parser').get();
  53. parser.from(source).search(name).parse(data => {
  54. if (verbose) {
  55. timer.stop();
  56. console.info(`Information about ${name} extracted in ${timer.time()}`);
  57. }
  58. // Transforme en XML et ecrit dans le fichier
  59. const xml = require('./src/extract/formatter').toXml(name, data);
  60. file.makedir(out, true);
  61. file.put(out, xml, true);
  62. // Affiche un message de fin
  63. console.info(`Data extracted to ${out}`);
  64. });
  65. }