index.js 2.6 KB

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