123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. const fs = require('fs');
  2. const path = require('path');
  3. module.exports = {
  4. parseVersion,
  5. root,
  6. isExternalLib
  7. };
  8. const parseString = require('xml2js').parseString;
  9. // return the version number from `pom.xml` file
  10. function parseVersion() {
  11. let version = null;
  12. const pomXml = fs.readFileSync('pom.xml', 'utf8');
  13. parseString(pomXml, (err, result) => {
  14. if (err) {
  15. throw new Error('Failed to parse pom.xml: ' + err);
  16. }
  17. if (result.project.version && result.project.version[0]) {
  18. version = result.project.version[0];
  19. } else if (result.project.parent && result.project.parent[0] && result.project.parent[0].version && result.project.parent[0].version[0]) {
  20. version = result.project.parent[0].version[0];
  21. }
  22. });
  23. if (version === null) {
  24. throw new Error('pom.xml is malformed. No version is defined');
  25. }
  26. return version;
  27. }
  28. const _root = path.resolve(__dirname, '..');
  29. function root(args) {
  30. args = Array.prototype.slice.call(arguments, 0);
  31. return path.join.apply(path, [_root].concat(args));
  32. }
  33. function isExternalLib(module, check = /node_modules/) {
  34. const req = module.userRequest;
  35. if (typeof req !== 'string') {
  36. return false;
  37. }
  38. return req.search(check) >= 0;
  39. }