ExplorerBase.js.map 7.8 KB

1
  1. {"version":3,"file":"ExplorerBase.js","names":["ExplorerBase","constructor","options","cache","loadCache","Map","searchCache","config","validateConfig","clearLoadCache","clear","clearSearchCache","clearCaches","searchPlaces","forEach","place","loaderKey","path","extname","loader","loaders","Error","getExtensionDescription","shouldSearchStopWithResult","result","isEmpty","ignoreEmptySearchPlaces","nextDirectoryToSearch","currentDir","currentResult","nextDir","nextDirUp","stopDir","loadPackageProp","filepath","content","parsedContent","loadJson","packagePropValue","getPropertyByPath","packageProp","getLoaderEntryForFile","basename","bind","loadedContentToCosmiconfigResult","loadedContent","forceProp","undefined","usePackagePropInConfigFiles","validateFilePath","dir","dirname","ext"],"sources":["../src/ExplorerBase.ts"],"sourcesContent":["import path from 'path';\nimport { getPropertyByPath } from './getPropertyByPath';\nimport { Loader } from './index';\nimport { loaders } from './loaders';\nimport {\n Cache,\n CosmiconfigResult,\n ExplorerOptions,\n ExplorerOptionsSync,\n LoadedFileContent,\n} from './types';\n\nclass ExplorerBase<T extends ExplorerOptions | ExplorerOptionsSync> {\n protected readonly loadCache?: Cache;\n protected readonly searchCache?: Cache;\n protected readonly config: T;\n\n public constructor(options: T) {\n if (options.cache) {\n this.loadCache = new Map();\n this.searchCache = new Map();\n }\n\n this.config = options;\n this.validateConfig();\n }\n\n public clearLoadCache(): void {\n if (this.loadCache) {\n this.loadCache.clear();\n }\n }\n\n public clearSearchCache(): void {\n if (this.searchCache) {\n this.searchCache.clear();\n }\n }\n\n public clearCaches(): void {\n this.clearLoadCache();\n this.clearSearchCache();\n }\n\n private validateConfig(): void {\n const config = this.config;\n\n config.searchPlaces.forEach((place): void => {\n const loaderKey = path.extname(place) || 'noExt';\n const loader = config.loaders[loaderKey];\n if (!loader) {\n throw new Error(\n `No loader specified for ${getExtensionDescription(\n place,\n )}, so searchPlaces item \"${place}\" is invalid`,\n );\n }\n\n if (typeof loader !== 'function') {\n throw new Error(\n `loader for ${getExtensionDescription(\n place,\n )} is not a function (type provided: \"${typeof loader}\"), so searchPlaces item \"${place}\" is invalid`,\n );\n }\n });\n }\n\n protected shouldSearchStopWithResult(result: CosmiconfigResult): boolean {\n if (result === null) return false;\n return !(result.isEmpty && this.config.ignoreEmptySearchPlaces);\n }\n\n protected nextDirectoryToSearch(\n currentDir: string,\n currentResult: CosmiconfigResult,\n ): string | null {\n if (this.shouldSearchStopWithResult(currentResult)) {\n return null;\n }\n const nextDir = nextDirUp(currentDir);\n if (nextDir === currentDir || currentDir === this.config.stopDir) {\n return null;\n }\n return nextDir;\n }\n\n private loadPackageProp(filepath: string, content: string): unknown {\n const parsedContent = loaders.loadJson(filepath, content);\n const packagePropValue = getPropertyByPath(\n parsedContent,\n this.config.packageProp,\n );\n return packagePropValue || null;\n }\n\n protected getLoaderEntryForFile(filepath: string): Loader {\n if (path.basename(filepath) === 'package.json') {\n return this.loadPackageProp.bind(this);\n }\n\n const loaderKey = path.extname(filepath) || 'noExt';\n\n const loader = this.config.loaders[loaderKey];\n\n if (!loader) {\n throw new Error(\n `No loader specified for ${getExtensionDescription(filepath)}`,\n );\n }\n\n return loader;\n }\n\n protected loadedContentToCosmiconfigResult(\n filepath: string,\n loadedContent: LoadedFileContent,\n forceProp: boolean,\n ): CosmiconfigResult {\n if (loadedContent === null) {\n return null;\n }\n if (loadedContent === undefined) {\n return { filepath, config: undefined, isEmpty: true };\n }\n if (this.config.usePackagePropInConfigFiles || forceProp) {\n loadedContent = getPropertyByPath(loadedContent, this.config.packageProp);\n }\n if (loadedContent === undefined) {\n return { filepath, config: undefined, isEmpty: true };\n }\n return { config: loadedContent, filepath };\n }\n\n protected validateFilePath(filepath: string): void {\n if (!filepath) {\n throw new Error('load must pass a non-empty string');\n }\n }\n}\n\nfunction nextDirUp(dir: string): string {\n return path.dirname(dir);\n}\n\nfunction getExtensionDescription(filepath: string): string {\n const ext = path.extname(filepath);\n return ext ? `extension \"${ext}\"` : 'files without extensions';\n}\n\nexport { ExplorerBase, getExtensionDescription };\n"],"mappings":";;;;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