apply-extends.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. "use strict";
  2. const path = require("path");
  3. const resolveFrom = require("resolve-from");
  4. const { ValidationError } = require("@lerna/validation-error");
  5. const { deprecateConfig } = require("./deprecate-config");
  6. const { shallowExtend } = require("./shallow-extend");
  7. module.exports.applyExtends = applyExtends;
  8. /**
  9. * @param {{ [key: string]: unknown }} config
  10. * @param {string} cwd
  11. * @param {Set<string>} seen
  12. */
  13. function applyExtends(config, cwd, seen = new Set()) {
  14. let defaultConfig = {};
  15. if ("extends" in config) {
  16. let pathToDefault;
  17. try {
  18. pathToDefault = resolveFrom(cwd, config.extends);
  19. } catch (err) {
  20. throw new ValidationError("ERESOLVED", "Config .extends must be locally-resolvable", err);
  21. }
  22. if (seen.has(pathToDefault)) {
  23. throw new ValidationError("ECIRCULAR", "Config .extends cannot be circular", seen);
  24. }
  25. seen.add(pathToDefault);
  26. // eslint-disable-next-line import/no-dynamic-require, global-require
  27. defaultConfig = require(pathToDefault);
  28. delete config.extends; // eslint-disable-line no-param-reassign
  29. deprecateConfig(defaultConfig, pathToDefault);
  30. defaultConfig = applyExtends(defaultConfig, path.dirname(pathToDefault), seen);
  31. }
  32. return shallowExtend(config, defaultConfig);
  33. }