index.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.defaultLoadersSync = exports.defaultLoaders = exports.globalConfigSearchPlacesSync = exports.globalConfigSearchPlaces = exports.getDefaultSearchPlacesSync = exports.getDefaultSearchPlaces = exports.cosmiconfigSync = exports.cosmiconfig = void 0;
  4. const defaults_1 = require("./defaults");
  5. Object.defineProperty(exports, "defaultLoaders", { enumerable: true, get: function () { return defaults_1.defaultLoaders; } });
  6. Object.defineProperty(exports, "defaultLoadersSync", { enumerable: true, get: function () { return defaults_1.defaultLoadersSync; } });
  7. Object.defineProperty(exports, "getDefaultSearchPlaces", { enumerable: true, get: function () { return defaults_1.getDefaultSearchPlaces; } });
  8. Object.defineProperty(exports, "getDefaultSearchPlacesSync", { enumerable: true, get: function () { return defaults_1.getDefaultSearchPlacesSync; } });
  9. Object.defineProperty(exports, "globalConfigSearchPlaces", { enumerable: true, get: function () { return defaults_1.globalConfigSearchPlaces; } });
  10. Object.defineProperty(exports, "globalConfigSearchPlacesSync", { enumerable: true, get: function () { return defaults_1.globalConfigSearchPlacesSync; } });
  11. const Explorer_js_1 = require("./Explorer.js");
  12. const ExplorerSync_js_1 = require("./ExplorerSync.js");
  13. const util_1 = require("./util");
  14. const identity = function identity(x) {
  15. return x;
  16. };
  17. function getUserDefinedOptionsFromMetaConfig() {
  18. const metaExplorer = new ExplorerSync_js_1.ExplorerSync({
  19. moduleName: 'cosmiconfig',
  20. stopDir: process.cwd(),
  21. searchPlaces: defaults_1.metaSearchPlaces,
  22. ignoreEmptySearchPlaces: false,
  23. applyPackagePropertyPathToConfiguration: true,
  24. loaders: defaults_1.defaultLoaders,
  25. transform: identity,
  26. cache: true,
  27. metaConfigFilePath: null,
  28. mergeImportArrays: true,
  29. mergeSearchPlaces: true,
  30. searchStrategy: 'none',
  31. });
  32. const metaConfig = metaExplorer.search();
  33. if (!metaConfig) {
  34. return null;
  35. }
  36. if (metaConfig.config?.loaders) {
  37. throw new Error('Can not specify loaders in meta config file');
  38. }
  39. if (metaConfig.config?.searchStrategy) {
  40. throw new Error('Can not specify searchStrategy in meta config file');
  41. }
  42. const overrideOptions = {
  43. mergeSearchPlaces: true,
  44. ...(metaConfig.config ?? {}),
  45. };
  46. return {
  47. config: (0, util_1.removeUndefinedValuesFromObject)(overrideOptions),
  48. filepath: metaConfig.filepath,
  49. };
  50. }
  51. function getResolvedSearchPlaces(moduleName, toolDefinedSearchPlaces, userConfiguredOptions) {
  52. const userConfiguredSearchPlaces = userConfiguredOptions.searchPlaces?.map((path) => path.replace('{name}', moduleName));
  53. if (userConfiguredOptions.mergeSearchPlaces) {
  54. return [...(userConfiguredSearchPlaces ?? []), ...toolDefinedSearchPlaces];
  55. }
  56. return (userConfiguredSearchPlaces ??
  57. /* istanbul ignore next */ toolDefinedSearchPlaces);
  58. }
  59. function mergeOptionsBase(moduleName, defaults, options) {
  60. const userDefinedConfig = getUserDefinedOptionsFromMetaConfig();
  61. if (!userDefinedConfig) {
  62. return {
  63. ...defaults,
  64. ...(0, util_1.removeUndefinedValuesFromObject)(options),
  65. loaders: {
  66. ...defaults.loaders,
  67. ...options.loaders,
  68. },
  69. };
  70. }
  71. const userConfiguredOptions = userDefinedConfig.config;
  72. const toolDefinedSearchPlaces = options.searchPlaces ?? defaults.searchPlaces;
  73. return {
  74. ...defaults,
  75. ...(0, util_1.removeUndefinedValuesFromObject)(options),
  76. metaConfigFilePath: userDefinedConfig.filepath,
  77. ...userConfiguredOptions,
  78. searchPlaces: getResolvedSearchPlaces(moduleName, toolDefinedSearchPlaces, userConfiguredOptions),
  79. loaders: {
  80. ...defaults.loaders,
  81. ...options.loaders,
  82. },
  83. };
  84. }
  85. function validateOptions(options) {
  86. if (options.searchStrategy != null &&
  87. options.searchStrategy !== 'global' &&
  88. options.stopDir) {
  89. throw new Error('Can not supply `stopDir` option with `searchStrategy` other than "global"');
  90. }
  91. }
  92. function mergeOptions(moduleName, options) {
  93. validateOptions(options);
  94. const defaults = {
  95. moduleName,
  96. searchPlaces: (0, defaults_1.getDefaultSearchPlaces)(moduleName),
  97. ignoreEmptySearchPlaces: true,
  98. cache: true,
  99. transform: identity,
  100. loaders: defaults_1.defaultLoaders,
  101. metaConfigFilePath: null,
  102. mergeImportArrays: true,
  103. mergeSearchPlaces: true,
  104. searchStrategy: options.stopDir ? 'global' : 'none',
  105. };
  106. return mergeOptionsBase(moduleName, defaults, options);
  107. }
  108. function mergeOptionsSync(moduleName, options) {
  109. validateOptions(options);
  110. const defaults = {
  111. moduleName,
  112. searchPlaces: (0, defaults_1.getDefaultSearchPlacesSync)(moduleName),
  113. ignoreEmptySearchPlaces: true,
  114. cache: true,
  115. transform: identity,
  116. loaders: defaults_1.defaultLoadersSync,
  117. metaConfigFilePath: null,
  118. mergeImportArrays: true,
  119. mergeSearchPlaces: true,
  120. searchStrategy: options.stopDir ? 'global' : 'none',
  121. };
  122. return mergeOptionsBase(moduleName, defaults, options);
  123. }
  124. function cosmiconfig(moduleName, options = {}) {
  125. const normalizedOptions = mergeOptions(moduleName, options);
  126. const explorer = new Explorer_js_1.Explorer(normalizedOptions);
  127. return {
  128. search: explorer.search.bind(explorer),
  129. load: explorer.load.bind(explorer),
  130. clearLoadCache: explorer.clearLoadCache.bind(explorer),
  131. clearSearchCache: explorer.clearSearchCache.bind(explorer),
  132. clearCaches: explorer.clearCaches.bind(explorer),
  133. };
  134. }
  135. exports.cosmiconfig = cosmiconfig;
  136. function cosmiconfigSync(moduleName, options = {}) {
  137. const normalizedOptions = mergeOptionsSync(moduleName, options);
  138. const explorerSync = new ExplorerSync_js_1.ExplorerSync(normalizedOptions);
  139. return {
  140. search: explorerSync.search.bind(explorerSync),
  141. load: explorerSync.load.bind(explorerSync),
  142. clearLoadCache: explorerSync.clearLoadCache.bind(explorerSync),
  143. clearSearchCache: explorerSync.clearSearchCache.bind(explorerSync),
  144. clearCaches: explorerSync.clearCaches.bind(explorerSync),
  145. };
  146. }
  147. exports.cosmiconfigSync = cosmiconfigSync;
  148. //# sourceMappingURL=index.js.map