index.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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);
  26. extended.push(config);
  27. return extended.reduce((r, _a) => {
  28. var { extends: _ } = _a, c = __rest(_a, ["extends"]);
  29. return (0, mergeWith_1.default)(r, c, (objValue, srcValue, key) => {
  30. if (key === 'plugins') {
  31. if (Array.isArray(objValue)) {
  32. return objValue.concat(srcValue);
  33. }
  34. }
  35. else if (Array.isArray(objValue)) {
  36. return srcValue;
  37. }
  38. });
  39. }, e ? { extends: e } : {});
  40. }
  41. exports.default = resolveExtends;
  42. function loadExtends(config = {}, context = {}) {
  43. const { extends: e } = config;
  44. const ext = e ? (Array.isArray(e) ? e : [e]) : [];
  45. return ext.reduce((configs, raw) => {
  46. const load = context.require || require;
  47. const resolved = resolveConfig(raw, context);
  48. const c = load(resolved);
  49. const cwd = path_1.default.dirname(resolved);
  50. const ctx = (0, merge_1.default)({}, context, { cwd });
  51. // Resolve parser preset if none was present before
  52. if (!context.parserPreset &&
  53. typeof c === 'object' &&
  54. typeof c.parserPreset === 'string') {
  55. const resolvedParserPreset = (0, resolve_from_1.default)(cwd, c.parserPreset);
  56. const parserPreset = {
  57. name: c.parserPreset,
  58. path: `./${path_1.default.relative(process.cwd(), resolvedParserPreset)}`
  59. .split(path_1.default.sep)
  60. .join('/'),
  61. parserOpts: require(resolvedParserPreset),
  62. };
  63. ctx.parserPreset = parserPreset;
  64. config.parserPreset = parserPreset;
  65. }
  66. return [...configs, ...loadExtends(c, ctx), c];
  67. }, []);
  68. }
  69. function getId(raw = '', prefix = '') {
  70. const first = raw.charAt(0);
  71. const scoped = first === '@';
  72. const relative = first === '.';
  73. const absolute = path_1.default.isAbsolute(raw);
  74. if (scoped) {
  75. return raw.includes('/') ? raw : [raw, prefix].filter(String).join('/');
  76. }
  77. return relative || absolute ? raw : [prefix, raw].filter(String).join('-');
  78. }
  79. function resolveConfig(raw, context = {}) {
  80. const resolve = context.resolve || resolveId;
  81. const id = getId(raw, context.prefix);
  82. try {
  83. return resolve(id, context);
  84. }
  85. catch (err) {
  86. const legacy = getId(raw, 'conventional-changelog-lint-config');
  87. const resolved = resolve(legacy, context);
  88. console.warn(`Resolving ${raw} to legacy config ${legacy}. To silence this warning raise an issue at 'npm repo ${legacy}' to rename to ${id}.`);
  89. return resolved;
  90. }
  91. }
  92. function resolveId(id, context = {}) {
  93. const cwd = context.cwd || process.cwd();
  94. const localPath = resolveFromSilent(cwd, id);
  95. if (typeof localPath === 'string') {
  96. return localPath;
  97. }
  98. const resolveGlobal = context.resolveGlobal || resolveGlobalSilent;
  99. const globalPath = resolveGlobal(id);
  100. if (typeof globalPath === 'string') {
  101. return globalPath;
  102. }
  103. const err = new Error(`Cannot find module "${id}" from "${cwd}"`);
  104. err.code = 'MODULE_NOT_FOUND';
  105. throw err;
  106. }
  107. function resolveFromSilent(cwd, id) {
  108. try {
  109. return (0, resolve_from_1.default)(cwd, id);
  110. }
  111. catch (err) { }
  112. }
  113. function resolveGlobalSilent(id) {
  114. try {
  115. const resolveGlobal = importFresh('resolve-global');
  116. return resolveGlobal(id);
  117. }
  118. catch (err) { }
  119. }
  120. //# sourceMappingURL=index.js.map