UNPKG

2.81 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.cosmiconfig = cosmiconfig;
7exports.cosmiconfigSync = cosmiconfigSync;
8exports.defaultLoaders = void 0;
9
10var _os = _interopRequireDefault(require("os"));
11
12var _Explorer = require("./Explorer");
13
14var _ExplorerSync = require("./ExplorerSync");
15
16var _loaders = require("./loaders");
17
18var _types = require("./types");
19
20function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
21
22/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
23// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
24function cosmiconfig(moduleName, options = {}) {
25 const normalizedOptions = normalizeOptions(moduleName, options);
26 const explorer = new _Explorer.Explorer(normalizedOptions);
27 return {
28 search: explorer.search.bind(explorer),
29 load: explorer.load.bind(explorer),
30 clearLoadCache: explorer.clearLoadCache.bind(explorer),
31 clearSearchCache: explorer.clearSearchCache.bind(explorer),
32 clearCaches: explorer.clearCaches.bind(explorer)
33 };
34} // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
35
36
37function cosmiconfigSync(moduleName, options = {}) {
38 const normalizedOptions = normalizeOptions(moduleName, options);
39 const explorerSync = new _ExplorerSync.ExplorerSync(normalizedOptions);
40 return {
41 search: explorerSync.searchSync.bind(explorerSync),
42 load: explorerSync.loadSync.bind(explorerSync),
43 clearLoadCache: explorerSync.clearLoadCache.bind(explorerSync),
44 clearSearchCache: explorerSync.clearSearchCache.bind(explorerSync),
45 clearCaches: explorerSync.clearCaches.bind(explorerSync)
46 };
47} // do not allow mutation of default loaders. Make sure it is set inside options
48
49
50const defaultLoaders = Object.freeze({
51 '.cjs': _loaders.loaders.loadJs,
52 '.js': _loaders.loaders.loadJs,
53 '.json': _loaders.loaders.loadJson,
54 '.yaml': _loaders.loaders.loadYaml,
55 '.yml': _loaders.loaders.loadYaml,
56 noExt: _loaders.loaders.loadYaml
57});
58exports.defaultLoaders = defaultLoaders;
59
60const identity = function identity(x) {
61 return x;
62};
63
64function normalizeOptions(moduleName, options) {
65 const defaults = {
66 packageProp: moduleName,
67 searchPlaces: ['package.json', `.${moduleName}rc`, `.${moduleName}rc.json`, `.${moduleName}rc.yaml`, `.${moduleName}rc.yml`, `.${moduleName}rc.js`, `.${moduleName}rc.cjs`, `${moduleName}.config.js`, `${moduleName}.config.cjs`],
68 ignoreEmptySearchPlaces: true,
69 stopDir: _os.default.homedir(),
70 cache: true,
71 transform: identity,
72 loaders: defaultLoaders
73 };
74 const normalizedOptions = { ...defaults,
75 ...options,
76 loaders: { ...defaults.loaders,
77 ...options.loaders
78 }
79 };
80 return normalizedOptions;
81}
82//# sourceMappingURL=index.js.map
\No newline at end of file