index.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. "use strict";
  2. var __rest = (this && this.__rest) || function (s, e) {
  3. var t = {};
  4. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
  5. t[p] = s[p];
  6. if (s != null && typeof Object.getOwnPropertySymbols === "function")
  7. for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  8. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
  9. t[p[i]] = s[p[i]];
  10. }
  11. return t;
  12. };
  13. var __importDefault = (this && this.__importDefault) || function (mod) {
  14. return (mod && mod.__esModule) ? mod : { "default": mod };
  15. };
  16. Object.defineProperty(exports, "__esModule", { value: true });
  17. const path_1 = __importDefault(require("path"));
  18. require("resolve-global");
  19. const resolve_from_1 = __importDefault(require("resolve-from"));
  20. const merge_1 = __importDefault(require("lodash/merge"));
  21. const mergeWith_1 = __importDefault(require("lodash/mergeWith"));
  22. const importFresh = require('import-fresh');
  23. function resolveExtends(config = {}, context = {}) {
  24. const { extends: e } = config;
  25. const extended = loadExtends(config, context).reduceRight((r, _a) => {
  26. var { extends: _ } = _a, c = __rest(_a, ["extends"]);
  27. return mergeWith_1.default(r, c, (objValue, srcValue) => {
  28. if (Array.isArray(objValue)) {
  29. return srcValue;
  30. }
  31. });
  32. }, e ? { extends: e } : {});
  33. return merge_1.default({}, extended, config);
  34. }
  35. exports.default = resolveExtends;
  36. function loadExtends(config = {}, context = {}) {
  37. const { extends: e } = config;
  38. const ext = e ? (Array.isArray(e) ? e : [e]) : [];
  39. return ext.reduce((configs, raw) => {
  40. const load = context.require || require;
  41. const resolved = resolveConfig(raw, context);
  42. const c = load(resolved);
  43. const cwd = path_1.default.dirname(resolved);
  44. const ctx = merge_1.default({}, context, { cwd });
  45. // Resolve parser preset if none was present before
  46. if (!context.parserPreset &&
  47. typeof c === 'object' &&
  48. typeof c.parserPreset === 'string') {
  49. const resolvedParserPreset = resolve_from_1.default(cwd, c.parserPreset);
  50. const parserPreset = {
  51. name: c.parserPreset,
  52. path: `./${path_1.default.relative(process.cwd(), resolvedParserPreset)}`
  53. .split(path_1.default.sep)
  54. .join('/'),
  55. parserOpts: require(resolvedParserPreset),
  56. };
  57. ctx.parserPreset = parserPreset;
  58. config.parserPreset = parserPreset;
  59. }
  60. return [...configs, c, ...loadExtends(c, ctx)];
  61. }, []);
  62. }
  63. function getId(raw = '', prefix = '') {
  64. const first = raw.charAt(0);
  65. const scoped = first === '@';
  66. const relative = first === '.';
  67. const absolute = path_1.default.isAbsolute(raw);
  68. if (scoped) {
  69. return raw.includes('/') ? raw : [raw, prefix].filter(String).join('/');
  70. }
  71. return relative || absolute ? raw : [prefix, raw].filter(String).join('-');
  72. }
  73. function resolveConfig(raw, context = {}) {
  74. const resolve = context.resolve || resolveId;
  75. const id = getId(raw, context.prefix);
  76. try {
  77. return resolve(id, context);
  78. }
  79. catch (err) {
  80. const legacy = getId(raw, 'conventional-changelog-lint-config');
  81. const resolved = resolve(legacy, context);
  82. console.warn(`Resolving ${raw} to legacy config ${legacy}. To silence this warning raise an issue at 'npm repo ${legacy}' to rename to ${id}.`);
  83. return resolved;
  84. }
  85. }
  86. function resolveId(id, context = {}) {
  87. const cwd = context.cwd || process.cwd();
  88. const localPath = resolveFromSilent(cwd, id);
  89. if (typeof localPath === 'string') {
  90. return localPath;
  91. }
  92. const resolveGlobal = context.resolveGlobal || resolveGlobalSilent;
  93. const globalPath = resolveGlobal(id);
  94. if (typeof globalPath === 'string') {
  95. return globalPath;
  96. }
  97. const err = new Error(`Cannot find module "${id}" from "${cwd}"`);
  98. err.code = 'MODULE_NOT_FOUND';
  99. throw err;
  100. }
  101. function resolveFromSilent(cwd, id) {
  102. try {
  103. return resolve_from_1.default(cwd, id);
  104. }
  105. catch (err) { }
  106. }
  107. function resolveGlobalSilent(id) {
  108. try {
  109. const resolveGlobal = importFresh('resolve-global');
  110. return resolveGlobal(id);
  111. }
  112. catch (err) { }
  113. }
  114. //# sourceMappingURL=index.js.map