|
| 1 | +import { promises as fs } from "fs" |
| 2 | +import { load } from "js-yaml" |
| 3 | +import * as path from "path" |
| 4 | +import { Lazy } from "lazy-val" |
| 5 | +import { parse as parseEnv } from "dotenv" |
| 6 | +import { loadTsConfig } from "config-file-ts" |
| 7 | +import { DotenvParseInput, expand } from "dotenv-expand" |
| 8 | +import { resolveModule } from "../resolve" |
| 9 | +import { log } from "builder-util" |
| 10 | + |
| 11 | +export interface ReadConfigResult<T> { |
| 12 | + readonly result: T |
| 13 | + readonly configFile: string | null |
| 14 | +} |
| 15 | + |
| 16 | +async function readConfig<T>(configFile: string, request: ReadConfigRequest): Promise<ReadConfigResult<T>> { |
| 17 | + const data = await fs.readFile(configFile, "utf8") |
| 18 | + let result: any |
| 19 | + if (configFile.endsWith(".json5") || configFile.endsWith(".json")) { |
| 20 | + result = require("json5").parse(data) |
| 21 | + } else if (configFile.endsWith(".js") || configFile.endsWith(".cjs" || configFile.endsWith(".mjs"))) { |
| 22 | + const json = await orNullIfFileNotExist(fs.readFile(path.join(process.cwd(), "package.json"), "utf8")) |
| 23 | + const moduleType = json === null ? null : JSON.parse(json).type |
| 24 | + result = await resolveModule(moduleType, configFile) |
| 25 | + if (result.default != null) { |
| 26 | + result = result.default |
| 27 | + } |
| 28 | + if (typeof result === "function") { |
| 29 | + result = result(request) |
| 30 | + } |
| 31 | + result = await Promise.resolve(result) |
| 32 | + } else if (configFile.endsWith(".ts")) { |
| 33 | + result = loadTsConfig(configFile) |
| 34 | + if (typeof result === "function") { |
| 35 | + result = result(request) |
| 36 | + } |
| 37 | + result = await Promise.resolve(result) |
| 38 | + } else if (configFile.endsWith(".toml")) { |
| 39 | + result = require("toml").parse(data) |
| 40 | + } else { |
| 41 | + result = load(data) |
| 42 | + } |
| 43 | + return { result, configFile } |
| 44 | +} |
| 45 | + |
| 46 | +export async function findAndReadConfig<T>(request: ReadConfigRequest): Promise<ReadConfigResult<T> | null> { |
| 47 | + const prefix = request.configFilename |
| 48 | + for (const configFile of [`${prefix}.yml`, `${prefix}.yaml`, `${prefix}.json`, `${prefix}.json5`, `${prefix}.toml`, `${prefix}.js`, `${prefix}.cjs`, `${prefix}.ts`]) { |
| 49 | + const data = await orNullIfFileNotExist(readConfig<T>(path.join(request.projectDir, configFile), request)) |
| 50 | + if (data != null) { |
| 51 | + return data |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + return null |
| 56 | +} |
| 57 | + |
| 58 | +export function orNullIfFileNotExist<T>(promise: Promise<T>): Promise<T | null> { |
| 59 | + return orIfFileNotExist(promise, null) |
| 60 | +} |
| 61 | + |
| 62 | +export function orIfFileNotExist<T>(promise: Promise<T>, fallbackValue: T): Promise<T> { |
| 63 | + return promise.catch(e => { |
| 64 | + if (e.code === "ENOENT" || e.code === "ENOTDIR") { |
| 65 | + return fallbackValue |
| 66 | + } |
| 67 | + throw e |
| 68 | + }) |
| 69 | +} |
| 70 | + |
| 71 | +export interface ReadConfigRequest { |
| 72 | + packageKey: string |
| 73 | + configFilename: string |
| 74 | + |
| 75 | + projectDir: string |
| 76 | + packageMetadata: Lazy<{ [key: string]: any } | null> | null |
| 77 | +} |
| 78 | + |
| 79 | +export async function loadConfig<T>(request: ReadConfigRequest): Promise<ReadConfigResult<T> | null> { |
| 80 | + let packageMetadata = request.packageMetadata == null ? null : await request.packageMetadata.value |
| 81 | + if (packageMetadata == null) { |
| 82 | + const json = await orNullIfFileNotExist(fs.readFile(path.join(request.projectDir, "package.json"), "utf8")) |
| 83 | + packageMetadata = json == null ? null : JSON.parse(json) |
| 84 | + } |
| 85 | + |
| 86 | + const data: T = packageMetadata == null ? null : packageMetadata[request.packageKey] |
| 87 | + return data == null ? findAndReadConfig<T>(request) : { result: data, configFile: null } |
| 88 | +} |
| 89 | + |
| 90 | +export function getConfig<T>(request: ReadConfigRequest, configPath?: string | null): Promise<ReadConfigResult<T> | null> { |
| 91 | + if (configPath == null) { |
| 92 | + return loadConfig<T>(request) |
| 93 | + } else { |
| 94 | + return readConfig<T>(path.resolve(request.projectDir, configPath), request) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +export async function loadParentConfig<T>(request: ReadConfigRequest, spec: string): Promise<ReadConfigResult<T>> { |
| 99 | + let isFileSpec: boolean | undefined |
| 100 | + if (spec.startsWith("file:")) { |
| 101 | + spec = spec.substring("file:".length) |
| 102 | + isFileSpec = true |
| 103 | + } |
| 104 | + |
| 105 | + let parentConfig = await orNullIfFileNotExist(readConfig<T>(path.resolve(request.projectDir, spec), request)) |
| 106 | + if (parentConfig == null && isFileSpec !== true) { |
| 107 | + let resolved: string | null = null |
| 108 | + try { |
| 109 | + resolved = require.resolve(spec) |
| 110 | + } catch (e) { |
| 111 | + // ignore |
| 112 | + } |
| 113 | + |
| 114 | + if (resolved != null) { |
| 115 | + parentConfig = await readConfig<T>(resolved, request) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + if (parentConfig == null) { |
| 120 | + throw new Error(`Cannot find parent config file: ${spec}`) |
| 121 | + } |
| 122 | + |
| 123 | + return parentConfig |
| 124 | +} |
| 125 | + |
| 126 | +export async function loadEnv(envFile: string) { |
| 127 | + const data = await orNullIfFileNotExist(fs.readFile(envFile, "utf8")) |
| 128 | + if (data == null) { |
| 129 | + return null |
| 130 | + } |
| 131 | + |
| 132 | + const parsed = parseEnv<DotenvParseInput>(data) |
| 133 | + |
| 134 | + log.info({ envFile }, "injecting environment") |
| 135 | + Object.entries(parsed).forEach(([key, value]) => { |
| 136 | + if (!Object.prototype.hasOwnProperty.call(process.env, key)) { |
| 137 | + process.env[key] = value |
| 138 | + } |
| 139 | + }) |
| 140 | + expand({ parsed }) |
| 141 | + return parsed |
| 142 | +} |
0 commit comments