UNPKG

7.39 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 var desc = Object.getOwnPropertyDescriptor(m, k);
5 if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6 desc = { enumerable: true, get: function() { return m[k]; } };
7 }
8 Object.defineProperty(o, k2, desc);
9}) : (function(o, m, k, k2) {
10 if (k2 === undefined) k2 = k;
11 o[k2] = m[k];
12}));
13var __exportStar = (this && this.__exportStar) || function(m, exports) {
14 for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15};
16var __importDefault = (this && this.__importDefault) || function (mod) {
17 return (mod && mod.__esModule) ? mod : { "default": mod };
18};
19Object.defineProperty(exports, "__esModule", { value: true });
20exports.cosmiconfigSync = exports.cosmiconfig = exports.defaultLoadersSync = exports.defaultLoaders = exports.metaSearchPlaces = void 0;
21/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
22__exportStar(require("./types.js"), exports);
23const os_1 = __importDefault(require("os"));
24const Explorer_js_1 = require("./Explorer.js");
25const ExplorerSync_js_1 = require("./ExplorerSync.js");
26const loaders_js_1 = require("./loaders.js");
27const util_1 = require("./util");
28// this needs to be hardcoded, as this is intended for end users, who can't supply options at this point
29exports.metaSearchPlaces = [
30 'package.json',
31 '.config.json',
32 '.config.yaml',
33 '.config.yml',
34 '.config.js',
35 '.config.ts',
36 '.config.cjs',
37 '.config.mjs',
38];
39// do not allow mutation of default loaders. Make sure it is set inside options
40exports.defaultLoaders = Object.freeze({
41 '.mjs': loaders_js_1.loadJs,
42 '.cjs': loaders_js_1.loadJs,
43 '.js': loaders_js_1.loadJs,
44 '.ts': loaders_js_1.loadTs,
45 '.json': loaders_js_1.loadJson,
46 '.yaml': loaders_js_1.loadYaml,
47 '.yml': loaders_js_1.loadYaml,
48 noExt: loaders_js_1.loadYaml,
49});
50exports.defaultLoadersSync = Object.freeze({
51 '.cjs': loaders_js_1.loadJsSync,
52 '.js': loaders_js_1.loadJsSync,
53 '.ts': loaders_js_1.loadTsSync,
54 '.json': loaders_js_1.loadJson,
55 '.yaml': loaders_js_1.loadYaml,
56 '.yml': loaders_js_1.loadYaml,
57 noExt: loaders_js_1.loadYaml,
58});
59const identity = function identity(x) {
60 return x;
61};
62function getInternalOptions(moduleName, options) {
63 const metaExplorer = new ExplorerSync_js_1.ExplorerSync({
64 packageProp: 'cosmiconfig',
65 stopDir: process.cwd(),
66 searchPlaces: exports.metaSearchPlaces,
67 ignoreEmptySearchPlaces: false,
68 applyPackagePropertyPathToConfiguration: true,
69 loaders: exports.defaultLoaders,
70 transform: identity,
71 cache: true,
72 metaConfigFilePath: null,
73 });
74 const metaConfig = metaExplorer.search();
75 if (!metaConfig) {
76 return options;
77 }
78 if (metaConfig.config?.loaders) {
79 throw new Error('Can not specify loaders in meta config file');
80 }
81 const overrideOptions = metaConfig.config ?? {};
82 if (overrideOptions.searchPlaces) {
83 overrideOptions.searchPlaces = overrideOptions.searchPlaces.map((path) => path.replace('{name}', moduleName));
84 }
85 overrideOptions.metaConfigFilePath = metaConfig.filepath;
86 return { ...options, ...(0, util_1.removeUndefinedValuesFromObject)(overrideOptions) };
87}
88function normalizeOptions(moduleName, options) {
89 const defaults = {
90 packageProp: moduleName,
91 searchPlaces: [
92 'package.json',
93 `.${moduleName}rc`,
94 `.${moduleName}rc.json`,
95 `.${moduleName}rc.yaml`,
96 `.${moduleName}rc.yml`,
97 `.${moduleName}rc.js`,
98 `.${moduleName}rc.ts`,
99 `.${moduleName}rc.cjs`,
100 `.${moduleName}rc.mjs`,
101 `.config/${moduleName}rc`,
102 `.config/${moduleName}rc.json`,
103 `.config/${moduleName}rc.yaml`,
104 `.config/${moduleName}rc.yml`,
105 `.config/${moduleName}rc.js`,
106 `.config/${moduleName}rc.ts`,
107 `.config/${moduleName}rc.cjs`,
108 `.config/${moduleName}rc.mjs`,
109 `${moduleName}.config.js`,
110 `${moduleName}.config.ts`,
111 `${moduleName}.config.cjs`,
112 `${moduleName}.config.mjs`,
113 ],
114 ignoreEmptySearchPlaces: true,
115 stopDir: os_1.default.homedir(),
116 cache: true,
117 transform: identity,
118 loaders: exports.defaultLoaders,
119 metaConfigFilePath: null,
120 };
121 return {
122 ...defaults,
123 ...(0, util_1.removeUndefinedValuesFromObject)(options),
124 loaders: {
125 ...defaults.loaders,
126 ...options.loaders,
127 },
128 };
129}
130function normalizeOptionsSync(moduleName, options) {
131 const defaults = {
132 packageProp: moduleName,
133 searchPlaces: [
134 'package.json',
135 `.${moduleName}rc`,
136 `.${moduleName}rc.json`,
137 `.${moduleName}rc.yaml`,
138 `.${moduleName}rc.yml`,
139 `.${moduleName}rc.js`,
140 `.${moduleName}rc.ts`,
141 `.${moduleName}rc.cjs`,
142 `.config/${moduleName}rc`,
143 `.config/${moduleName}rc.json`,
144 `.config/${moduleName}rc.yaml`,
145 `.config/${moduleName}rc.yml`,
146 `.config/${moduleName}rc.js`,
147 `.config/${moduleName}rc.ts`,
148 `.config/${moduleName}rc.cjs`,
149 `${moduleName}.config.js`,
150 `${moduleName}.config.ts`,
151 `${moduleName}.config.cjs`,
152 ],
153 ignoreEmptySearchPlaces: true,
154 stopDir: os_1.default.homedir(),
155 cache: true,
156 transform: identity,
157 loaders: exports.defaultLoadersSync,
158 metaConfigFilePath: null,
159 };
160 return {
161 ...defaults,
162 ...(0, util_1.removeUndefinedValuesFromObject)(options),
163 loaders: {
164 ...defaults.loaders,
165 ...options.loaders,
166 },
167 };
168}
169function cosmiconfig(moduleName, options = {}) {
170 const internalOptions = getInternalOptions(moduleName, options);
171 const normalizedOptions = normalizeOptions(moduleName, internalOptions);
172 const explorer = new Explorer_js_1.Explorer(normalizedOptions);
173 return {
174 search: explorer.search.bind(explorer),
175 load: explorer.load.bind(explorer),
176 clearLoadCache: explorer.clearLoadCache.bind(explorer),
177 clearSearchCache: explorer.clearSearchCache.bind(explorer),
178 clearCaches: explorer.clearCaches.bind(explorer),
179 };
180}
181exports.cosmiconfig = cosmiconfig;
182function cosmiconfigSync(moduleName, options = {}) {
183 const internalOptions = getInternalOptions(moduleName, options);
184 const normalizedOptions = normalizeOptionsSync(moduleName, internalOptions);
185 const explorerSync = new ExplorerSync_js_1.ExplorerSync(normalizedOptions);
186 return {
187 search: explorerSync.search.bind(explorerSync),
188 load: explorerSync.load.bind(explorerSync),
189 clearLoadCache: explorerSync.clearLoadCache.bind(explorerSync),
190 clearSearchCache: explorerSync.clearSearchCache.bind(explorerSync),
191 clearCaches: explorerSync.clearCaches.bind(explorerSync),
192 };
193}
194exports.cosmiconfigSync = cosmiconfigSync;
195//# sourceMappingURL=index.js.map
\No newline at end of file