index.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.cosmiconfig = cosmiconfig;
  6. exports.cosmiconfigSync = cosmiconfigSync;
  7. exports.metaSearchPlaces = exports.defaultLoadersSync = exports.defaultLoaders = void 0;
  8. var _os = _interopRequireDefault(require("os"));
  9. var _Explorer = require("./Explorer");
  10. var _ExplorerSync = require("./ExplorerSync");
  11. var _loaders = require("./loaders");
  12. var _types = require("./types");
  13. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  14. /* eslint-disable @typescript-eslint/explicit-module-boundary-types */
  15. // this needs to be hardcoded, as this is intended for end users, who can't supply options at this point
  16. const metaSearchPlaces = ['package.json', '.config.json', '.config.yaml', '.config.yml', '.config.js', '.config.cjs', '.config.mjs']; // do not allow mutation of default loaders. Make sure it is set inside options
  17. exports.metaSearchPlaces = metaSearchPlaces;
  18. const defaultLoaders = Object.freeze({
  19. '.mjs': _loaders.loaders.loadJs,
  20. '.cjs': _loaders.loaders.loadJs,
  21. '.js': _loaders.loaders.loadJs,
  22. '.json': _loaders.loaders.loadJson,
  23. '.yaml': _loaders.loaders.loadYaml,
  24. '.yml': _loaders.loaders.loadYaml,
  25. noExt: _loaders.loaders.loadYaml
  26. });
  27. exports.defaultLoaders = defaultLoaders;
  28. const defaultLoadersSync = Object.freeze({
  29. '.cjs': _loaders.loaders.loadJsSync,
  30. '.js': _loaders.loaders.loadJsSync,
  31. '.json': _loaders.loaders.loadJson,
  32. '.yaml': _loaders.loaders.loadYaml,
  33. '.yml': _loaders.loaders.loadYaml,
  34. noExt: _loaders.loaders.loadYaml
  35. });
  36. exports.defaultLoadersSync = defaultLoadersSync;
  37. const identity = function identity(x) {
  38. return x;
  39. };
  40. function replaceMetaPlaceholders(paths, moduleName) {
  41. return paths.map(path => path.replace('{name}', moduleName));
  42. }
  43. function getExplorerOptions(moduleName, options) {
  44. var _metaConfig$config;
  45. const metaExplorer = new _ExplorerSync.ExplorerSync({
  46. packageProp: 'cosmiconfig',
  47. stopDir: process.cwd(),
  48. searchPlaces: metaSearchPlaces,
  49. ignoreEmptySearchPlaces: false,
  50. usePackagePropInConfigFiles: true,
  51. loaders: defaultLoaders,
  52. transform: identity,
  53. cache: true,
  54. metaConfigFilePath: null
  55. });
  56. const metaConfig = metaExplorer.searchSync();
  57. if (!metaConfig) {
  58. return options;
  59. }
  60. if ((_metaConfig$config = metaConfig.config) !== null && _metaConfig$config !== void 0 && _metaConfig$config.loaders) {
  61. throw new Error('Can not specify loaders in meta config file');
  62. }
  63. const overrideOptions = metaConfig.config ?? {};
  64. if (overrideOptions.searchPlaces) {
  65. overrideOptions.searchPlaces = replaceMetaPlaceholders(overrideOptions.searchPlaces, moduleName);
  66. }
  67. overrideOptions.metaConfigFilePath = metaConfig.filepath;
  68. return { ...options,
  69. ...overrideOptions
  70. };
  71. }
  72. function cosmiconfig(moduleName, options = {}) {
  73. const explorerOptions = getExplorerOptions(moduleName, options);
  74. const normalizedOptions = normalizeOptions(moduleName, explorerOptions);
  75. const explorer = new _Explorer.Explorer(normalizedOptions);
  76. return {
  77. search: explorer.search.bind(explorer),
  78. load: explorer.load.bind(explorer),
  79. clearLoadCache: explorer.clearLoadCache.bind(explorer),
  80. clearSearchCache: explorer.clearSearchCache.bind(explorer),
  81. clearCaches: explorer.clearCaches.bind(explorer)
  82. };
  83. } // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
  84. function cosmiconfigSync(moduleName, options = {}) {
  85. const explorerOptions = getExplorerOptions(moduleName, options);
  86. const normalizedOptions = normalizeOptionsSync(moduleName, explorerOptions);
  87. const explorerSync = new _ExplorerSync.ExplorerSync(normalizedOptions);
  88. return {
  89. search: explorerSync.searchSync.bind(explorerSync),
  90. load: explorerSync.loadSync.bind(explorerSync),
  91. clearLoadCache: explorerSync.clearLoadCache.bind(explorerSync),
  92. clearSearchCache: explorerSync.clearSearchCache.bind(explorerSync),
  93. clearCaches: explorerSync.clearCaches.bind(explorerSync)
  94. };
  95. }
  96. function normalizeOptions(moduleName, options) {
  97. const defaults = {
  98. packageProp: moduleName,
  99. searchPlaces: ['package.json', `.${moduleName}rc`, `.${moduleName}rc.json`, `.${moduleName}rc.yaml`, `.${moduleName}rc.yml`, `.${moduleName}rc.js`, `.${moduleName}rc.cjs`, `.${moduleName}rc.mjs`, `.config/${moduleName}rc`, `.config/${moduleName}rc.json`, `.config/${moduleName}rc.yaml`, `.config/${moduleName}rc.yml`, `.config/${moduleName}rc.js`, `.config/${moduleName}rc.cjs`, `.config/${moduleName}rc.mjs`, `${moduleName}.config.js`, `${moduleName}.config.cjs`, `${moduleName}.config.mjs`].filter(Boolean),
  100. ignoreEmptySearchPlaces: true,
  101. stopDir: _os.default.homedir(),
  102. cache: true,
  103. transform: identity,
  104. loaders: defaultLoaders,
  105. metaConfigFilePath: null
  106. };
  107. const normalizedOptions = { ...defaults,
  108. ...options,
  109. loaders: { ...defaults.loaders,
  110. ...options.loaders
  111. }
  112. };
  113. return normalizedOptions;
  114. }
  115. function normalizeOptionsSync(moduleName, options) {
  116. const defaults = {
  117. packageProp: moduleName,
  118. searchPlaces: ['package.json', `.${moduleName}rc`, `.${moduleName}rc.json`, `.${moduleName}rc.yaml`, `.${moduleName}rc.yml`, `.${moduleName}rc.js`, `.${moduleName}rc.cjs`, `.config/${moduleName}rc`, `.config/${moduleName}rc.json`, `.config/${moduleName}rc.yaml`, `.config/${moduleName}rc.yml`, `.config/${moduleName}rc.js`, `.config/${moduleName}rc.cjs`, `${moduleName}.config.js`, `${moduleName}.config.cjs`],
  119. ignoreEmptySearchPlaces: true,
  120. stopDir: _os.default.homedir(),
  121. cache: true,
  122. transform: identity,
  123. loaders: defaultLoadersSync,
  124. metaConfigFilePath: null
  125. };
  126. const normalizedOptions = { ...defaults,
  127. ...options,
  128. loaders: { ...defaults.loaders,
  129. ...options.loaders
  130. }
  131. };
  132. return normalizedOptions;
  133. }
  134. //# sourceMappingURL=index.js.map