index.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.cosmiconfig = cosmiconfig;
  6. exports.cosmiconfigSync = cosmiconfigSync;
  7. 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. // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
  16. function cosmiconfig(moduleName, options = {}) {
  17. const normalizedOptions = normalizeOptions(moduleName, options);
  18. const explorer = new _Explorer.Explorer(normalizedOptions);
  19. return {
  20. search: explorer.search.bind(explorer),
  21. load: explorer.load.bind(explorer),
  22. clearLoadCache: explorer.clearLoadCache.bind(explorer),
  23. clearSearchCache: explorer.clearSearchCache.bind(explorer),
  24. clearCaches: explorer.clearCaches.bind(explorer)
  25. };
  26. } // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
  27. function cosmiconfigSync(moduleName, options = {}) {
  28. const normalizedOptions = normalizeOptions(moduleName, options);
  29. const explorerSync = new _ExplorerSync.ExplorerSync(normalizedOptions);
  30. return {
  31. search: explorerSync.searchSync.bind(explorerSync),
  32. load: explorerSync.loadSync.bind(explorerSync),
  33. clearLoadCache: explorerSync.clearLoadCache.bind(explorerSync),
  34. clearSearchCache: explorerSync.clearSearchCache.bind(explorerSync),
  35. clearCaches: explorerSync.clearCaches.bind(explorerSync)
  36. };
  37. } // do not allow mutation of default loaders. Make sure it is set inside options
  38. const defaultLoaders = Object.freeze({
  39. '.cjs': _loaders.loaders.loadJs,
  40. '.js': _loaders.loaders.loadJs,
  41. '.json': _loaders.loaders.loadJson,
  42. '.yaml': _loaders.loaders.loadYaml,
  43. '.yml': _loaders.loaders.loadYaml,
  44. noExt: _loaders.loaders.loadYaml
  45. });
  46. exports.defaultLoaders = defaultLoaders;
  47. const identity = function identity(x) {
  48. return x;
  49. };
  50. function normalizeOptions(moduleName, options) {
  51. const defaults = {
  52. packageProp: moduleName,
  53. 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`],
  54. ignoreEmptySearchPlaces: true,
  55. stopDir: _os.default.homedir(),
  56. cache: true,
  57. transform: identity,
  58. loaders: defaultLoaders
  59. };
  60. const normalizedOptions = { ...defaults,
  61. ...options,
  62. loaders: { ...defaults.loaders,
  63. ...options.loaders
  64. }
  65. };
  66. return normalizedOptions;
  67. }
  68. //# sourceMappingURL=index.js.map