load-parser-opts.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.loadParserOpts = void 0;
  4. function isObjectLike(obj) {
  5. return Boolean(obj) && typeof obj === 'object'; // typeof null === 'object'
  6. }
  7. function isParserOptsFunction(obj) {
  8. return typeof obj.parserOpts === 'function';
  9. }
  10. async function loadParserOpts(pendingParser) {
  11. if (!pendingParser || typeof pendingParser !== 'object') {
  12. return undefined;
  13. }
  14. // Await for the module, loaded with require
  15. const parser = await pendingParser;
  16. // exit early, no opts to resolve
  17. if (!parser.parserOpts) {
  18. return parser;
  19. }
  20. // Pull nested parserOpts, might happen if overwritten with a module in main config
  21. if (typeof parser.parserOpts === 'object') {
  22. // Await parser opts if applicable
  23. parser.parserOpts = await parser.parserOpts;
  24. if (isObjectLike(parser.parserOpts) &&
  25. isObjectLike(parser.parserOpts.parserOpts)) {
  26. parser.parserOpts = parser.parserOpts.parserOpts;
  27. }
  28. return parser;
  29. }
  30. // Create parser opts from factory
  31. if (isParserOptsFunction(parser) &&
  32. typeof parser.name === 'string' &&
  33. parser.name.startsWith('conventional-changelog-')) {
  34. return new Promise((resolve) => {
  35. const result = parser.parserOpts((_, opts) => {
  36. resolve(Object.assign(Object.assign({}, parser), { parserOpts: opts === null || opts === void 0 ? void 0 : opts.parserOpts }));
  37. });
  38. // If result has data or a promise, the parser doesn't support factory-init
  39. // due to https://github.com/nodejs/promises-debugging/issues/16 it just quits, so let's use this fallback
  40. if (result) {
  41. Promise.resolve(result).then((opts) => {
  42. resolve(Object.assign(Object.assign({}, parser), { parserOpts: opts === null || opts === void 0 ? void 0 : opts.parserOpts }));
  43. });
  44. }
  45. return;
  46. });
  47. }
  48. return parser;
  49. }
  50. exports.loadParserOpts = loadParserOpts;
  51. //# sourceMappingURL=load-parser-opts.js.map