index.js.map 13 KB

1
  1. {"version":3,"file":"index.js","names":["metaSearchPlaces","defaultLoaders","Object","freeze","loaders","loadJs","loadJson","loadYaml","noExt","defaultLoadersSync","loadJsSync","identity","x","replaceMetaPlaceholders","paths","moduleName","map","path","replace","getExplorerOptions","options","metaExplorer","ExplorerSync","packageProp","stopDir","process","cwd","searchPlaces","ignoreEmptySearchPlaces","usePackagePropInConfigFiles","transform","cache","metaConfigFilePath","metaConfig","searchSync","config","Error","overrideOptions","filepath","cosmiconfig","explorerOptions","normalizedOptions","normalizeOptions","explorer","Explorer","search","bind","load","clearLoadCache","clearSearchCache","clearCaches","cosmiconfigSync","normalizeOptionsSync","explorerSync","loadSync","defaults","filter","Boolean","os","homedir"],"sources":["../src/index.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/explicit-module-boundary-types */\nimport os from 'os';\nimport { Explorer } from './Explorer';\nimport { ExplorerSync } from './ExplorerSync';\nimport { loaders } from './loaders';\nimport {\n Config,\n CosmiconfigResult,\n ExplorerOptions,\n ExplorerOptionsSync,\n Loaders,\n LoadersSync,\n} from './types';\n\ntype LoaderResult = Config | null;\nexport type Loader =\n | ((filepath: string, content: string) => Promise<LoaderResult>)\n | LoaderSync;\nexport type LoaderSync = (filepath: string, content: string) => LoaderResult;\n\nexport type Transform =\n | ((CosmiconfigResult: CosmiconfigResult) => Promise<CosmiconfigResult>)\n | TransformSync;\n\nexport type TransformSync = (\n CosmiconfigResult: CosmiconfigResult,\n) => CosmiconfigResult;\n\ninterface OptionsBase {\n packageProp?: string | Array<string>;\n searchPlaces?: Array<string>;\n ignoreEmptySearchPlaces?: boolean;\n stopDir?: string;\n cache?: boolean;\n}\n\nexport interface Options extends OptionsBase {\n loaders?: Loaders;\n transform?: Transform;\n}\n\nexport interface OptionsSync extends OptionsBase {\n loaders?: LoadersSync;\n transform?: TransformSync;\n}\n\nexport interface PublicExplorerBase {\n clearLoadCache: () => void;\n clearSearchCache: () => void;\n clearCaches: () => void;\n}\n\nexport interface PublicExplorer extends PublicExplorerBase {\n search: (searchFrom?: string) => Promise<CosmiconfigResult>;\n load: (filepath: string) => Promise<CosmiconfigResult>;\n}\n\nexport interface PublicExplorerSync extends PublicExplorerBase {\n search: (searchFrom?: string) => CosmiconfigResult;\n load: (filepath: string) => CosmiconfigResult;\n}\n\n// this needs to be hardcoded, as this is intended for end users, who can't supply options at this point\nexport const metaSearchPlaces = [\n 'package.json',\n '.config.json',\n '.config.yaml',\n '.config.yml',\n '.config.js',\n '.config.cjs',\n '.config.mjs',\n];\n\n// do not allow mutation of default loaders. Make sure it is set inside options\nconst defaultLoaders = Object.freeze({\n '.mjs': loaders.loadJs,\n '.cjs': loaders.loadJs,\n '.js': loaders.loadJs,\n '.json': loaders.loadJson,\n '.yaml': loaders.loadYaml,\n '.yml': loaders.loadYaml,\n noExt: loaders.loadYaml,\n} as const);\nconst defaultLoadersSync = Object.freeze({\n '.cjs': loaders.loadJsSync,\n '.js': loaders.loadJsSync,\n '.json': loaders.loadJson,\n '.yaml': loaders.loadYaml,\n '.yml': loaders.loadYaml,\n noExt: loaders.loadYaml,\n} as const);\n\nconst identity: TransformSync = function identity(x) {\n return x;\n};\n\nfunction replaceMetaPlaceholders(\n paths: Array<string>,\n moduleName: string,\n): Array<string> {\n return paths.map((path) => path.replace('{name}', moduleName));\n}\n\nfunction getExplorerOptions<T extends Options | OptionsSync>(\n moduleName: string,\n options: T,\n): T {\n const metaExplorer = new ExplorerSync({\n packageProp: 'cosmiconfig',\n stopDir: process.cwd(),\n searchPlaces: metaSearchPlaces,\n ignoreEmptySearchPlaces: false,\n usePackagePropInConfigFiles: true,\n loaders: defaultLoaders,\n transform: identity,\n cache: true,\n metaConfigFilePath: null,\n });\n const metaConfig = metaExplorer.searchSync();\n\n if (!metaConfig) {\n return options;\n }\n\n if (metaConfig.config?.loaders) {\n throw new Error('Can not specify loaders in meta config file');\n }\n\n const overrideOptions = metaConfig.config ?? {};\n\n if (overrideOptions.searchPlaces) {\n overrideOptions.searchPlaces = replaceMetaPlaceholders(\n overrideOptions.searchPlaces,\n moduleName,\n );\n }\n\n overrideOptions.metaConfigFilePath = metaConfig.filepath;\n\n return { ...options, ...overrideOptions };\n}\n\nfunction cosmiconfig(\n moduleName: string,\n options: Options = {},\n): PublicExplorer {\n const explorerOptions = getExplorerOptions(moduleName, options);\n\n const normalizedOptions: ExplorerOptions = normalizeOptions(\n moduleName,\n explorerOptions,\n );\n\n const explorer = new Explorer(normalizedOptions);\n\n return {\n search: explorer.search.bind(explorer),\n load: explorer.load.bind(explorer),\n clearLoadCache: explorer.clearLoadCache.bind(explorer),\n clearSearchCache: explorer.clearSearchCache.bind(explorer),\n clearCaches: explorer.clearCaches.bind(explorer),\n };\n}\n\n// eslint-disable-next-line @typescript-eslint/explicit-function-return-type\nfunction cosmiconfigSync(\n moduleName: string,\n options: OptionsSync = {},\n): PublicExplorerSync {\n const explorerOptions = getExplorerOptions(moduleName, options);\n\n const normalizedOptions: ExplorerOptionsSync = normalizeOptionsSync(\n moduleName,\n explorerOptions,\n );\n\n const explorerSync = new ExplorerSync(normalizedOptions);\n\n return {\n search: explorerSync.searchSync.bind(explorerSync),\n load: explorerSync.loadSync.bind(explorerSync),\n clearLoadCache: explorerSync.clearLoadCache.bind(explorerSync),\n clearSearchCache: explorerSync.clearSearchCache.bind(explorerSync),\n clearCaches: explorerSync.clearCaches.bind(explorerSync),\n };\n}\n\nfunction normalizeOptions(\n moduleName: string,\n options: Options,\n): ExplorerOptions {\n const defaults: ExplorerOptions = {\n packageProp: moduleName,\n searchPlaces: [\n 'package.json',\n `.${moduleName}rc`,\n `.${moduleName}rc.json`,\n `.${moduleName}rc.yaml`,\n `.${moduleName}rc.yml`,\n `.${moduleName}rc.js`,\n `.${moduleName}rc.cjs`,\n `.${moduleName}rc.mjs`,\n `.config/${moduleName}rc`,\n `.config/${moduleName}rc.json`,\n `.config/${moduleName}rc.yaml`,\n `.config/${moduleName}rc.yml`,\n `.config/${moduleName}rc.js`,\n `.config/${moduleName}rc.cjs`,\n `.config/${moduleName}rc.mjs`,\n `${moduleName}.config.js`,\n `${moduleName}.config.cjs`,\n `${moduleName}.config.mjs`,\n ].filter(Boolean),\n ignoreEmptySearchPlaces: true,\n stopDir: os.homedir(),\n cache: true,\n transform: identity,\n loaders: defaultLoaders,\n metaConfigFilePath: null,\n };\n\n const normalizedOptions: ExplorerOptions = {\n ...defaults,\n ...options,\n loaders: {\n ...defaults.loaders,\n ...options.loaders,\n },\n };\n\n return normalizedOptions;\n}\n\nfunction normalizeOptionsSync(\n moduleName: string,\n options: OptionsSync,\n): ExplorerOptionsSync {\n const defaults: ExplorerOptionsSync = {\n packageProp: moduleName,\n searchPlaces: [\n 'package.json',\n `.${moduleName}rc`,\n `.${moduleName}rc.json`,\n `.${moduleName}rc.yaml`,\n `.${moduleName}rc.yml`,\n `.${moduleName}rc.js`,\n `.${moduleName}rc.cjs`,\n `.config/${moduleName}rc`,\n `.config/${moduleName}rc.json`,\n `.config/${moduleName}rc.yaml`,\n `.config/${moduleName}rc.yml`,\n `.config/${moduleName}rc.js`,\n `.config/${moduleName}rc.cjs`,\n `${moduleName}.config.js`,\n `${moduleName}.config.cjs`,\n ],\n ignoreEmptySearchPlaces: true,\n stopDir: os.homedir(),\n cache: true,\n transform: identity,\n loaders: defaultLoadersSync,\n metaConfigFilePath: null,\n };\n\n const normalizedOptions: ExplorerOptionsSync = {\n ...defaults,\n ...options,\n loaders: {\n ...defaults.loaders,\n ...options.loaders,\n },\n };\n\n return normalizedOptions;\n}\n\nexport { cosmiconfig, cosmiconfigSync, defaultLoaders, defaultLoadersSync };\n"],"mappings":";;;;;;;;;AACA;;AACA;;AACA;;AACA;;AACA;;;;AALA;AA8DA;AACO,MAAMA,gBAAgB,GAAG,CAC9B,cAD8B,EAE9B,cAF8B,EAG9B,cAH8B,EAI9B,aAJ8B,EAK9B,YAL8B,EAM9B,aAN8B,EAO9B,aAP8B,CAAzB,C,CAUP;;;AACA,MAAMC,cAAc,GAAGC,MAA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