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M,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC;QACtC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,GAAG,CAAC,EAAE,CAAC;YACrC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC,CAAA;QAChC,CAAC;IACH,CAAC;IACD,OAAO,CAAC,eAAe,CAAC,CAAC,MAAM,CAAC,CAAA;IAChC,OAAO,MAAM,CAAA;AACf,CAAC;AAdD,0BAcC","sourcesContent":["import { promises as fs } from \"fs\"\nimport { load } from \"js-yaml\"\nimport * as path from \"path\"\nimport { Lazy } from \"lazy-val\"\nimport { parse as parseEnv } from \"dotenv\"\nimport { loadTsConfig } from \"config-file-ts\"\n\nexport interface ReadConfigResult<T> {\n readonly result: T\n readonly configFile: string | null\n}\n\nasync function readConfig<T>(configFile: string, request: ReadConfigRequest): Promise<ReadConfigResult<T>> {\n const data = await fs.readFile(configFile, \"utf8\")\n let result\n if (configFile.endsWith(\".json5\") || configFile.endsWith(\".json\")) {\n result = require(\"json5\").parse(data)\n }\n else if (configFile.endsWith(\".js\") || configFile.endsWith(\".cjs\")) {\n result = require(configFile)\n if (result.default != null) {\n result = result.default\n }\n if (typeof result === \"function\") {\n result = result(request)\n }\n result = await Promise.resolve(result)\n }\n else if (configFile.endsWith(\".ts\")) {\n result = loadTsConfig<T>(configFile)\n if (typeof result === \"function\") {\n result = result(request)\n }\n result = await Promise.resolve(result)\n }\n else if (configFile.endsWith(\".toml\")) {\n result = require(\"toml\").parse(data)\n }\n else {\n result = load(data)\n }\n return {result, configFile}\n}\n\nexport async function findAndReadConfig<T>(request: ReadConfigRequest): Promise<ReadConfigResult<T> | null> {\n const prefix = request.configFilename\n for (const configFile of [`${prefix}.yml`, `${prefix}.yaml`, `${prefix}.json`, `${prefix}.json5`, `${prefix}.toml`, `${prefix}.js`, `${prefix}.cjs`, `${prefix}.ts`]) {\n const data = await orNullIfFileNotExist(readConfig<T>(path.join(request.projectDir, configFile), request))\n if (data != null) {\n return data\n }\n }\n\n return null\n}\n\nexport function orNullIfFileNotExist<T>(promise: Promise<T>): Promise<T | null> {\n return orIfFileNotExist(promise, null)\n}\n\nexport function orIfFileNotExist<T>(promise: Promise<T>, fallbackValue: T): Promise<T> {\n return promise\n .catch(e => {\n if (e.code === \"ENOENT\" || e.code === \"ENOTDIR\") {\n return fallbackValue\n }\n throw e\n })\n}\n\nexport interface ReadConfigRequest {\n packageKey: string\n configFilename: string\n\n projectDir: string\n packageMetadata: Lazy<{ [key: string]: any } | null> | null\n}\n\nexport async function loadConfig<T>(request: ReadConfigRequest): Promise<ReadConfigResult<T> | null> {\n let packageMetadata = request.packageMetadata == null ? null : await request.packageMetadata.value\n if (packageMetadata == null) {\n const json = await orNullIfFileNotExist(fs.readFile(path.join(request.projectDir, \"package.json\"), \"utf8\"))\n packageMetadata = json == null ? null : JSON.parse(json)\n }\n\n const data: T = packageMetadata == null ? null : packageMetadata[request.packageKey]\n return data == null ? findAndReadConfig<T>(request) : {result: data, configFile: null}\n}\n\nexport function getConfig<T>(request: ReadConfigRequest, configPath?: string | null): Promise<ReadConfigResult<T> | null> {\n if (configPath == null) {\n return loadConfig<T>(request)\n }\n else {\n return readConfig<T>(path.resolve(request.projectDir, configPath), request)\n }\n}\n\nexport async function loadParentConfig<T>(request: ReadConfigRequest, spec: string): Promise<ReadConfigResult<T>> {\n let isFileSpec: boolean | undefined\n if (spec.startsWith(\"file:\")) {\n spec = spec.substring(\"file:\".length)\n isFileSpec = true\n }\n\n let parentConfig = await orNullIfFileNotExist(readConfig<T>(path.resolve(request.projectDir, spec), request))\n if (parentConfig == null && isFileSpec !== true) {\n let resolved: string | null = null\n try {\n resolved = require.resolve(spec)\n }\n catch (e) {\n // ignore\n }\n\n if (resolved != null) {\n parentConfig = await readConfig<T>(resolved, request)\n }\n }\n\n if (parentConfig == null) {\n throw new Error(`Cannot find parent config file: ${spec}`)\n }\n\n return parentConfig\n}\n\nexport async function loadEnv(envFile: string) {\n const data = await orNullIfFileNotExist(fs.readFile(envFile, \"utf8\"))\n if (data == null) {\n return null\n }\n\n const parsed = parseEnv(data)\n for (const key of Object.keys(parsed)) {\n if (!process.env.hasOwnProperty(key)) {\n process.env[key] = parsed[key]\n }\n }\n require(\"dotenv-expand\")(parsed)\n return parsed\n}\n"]} |