import toml from "@iarna/toml" ; import Debug from "debug" ; import fs from "fs-extra" ; import { join } from "path" ; import { Config , EnvironmentConfig } from "./types" ; import { validate } from "./validate" ; const debug = Debug ( "kosko:config" ) ; param path export async function loadConfig ( path ) { const content = await fs . readFile ( path , "utf8" ) ; const data = await toml . parse . async ( content ) ; debug ( "Found config at" , path ) ; return validate ( data ) ; } param cwd export async function searchConfig ( cwd = process . cwd ( ) ) { const path = join ( cwd , "kosko.toml" ) ; try { return await loadConfig ( path ) ; } catch ( err ) { if ( err . code === "ENOENT" ) return { } ; debug ( "Config load failed" , err ) ; throw err ; } } function flatten < T > ( ... arrays ) { return arrays . reduce ( ( acc = [ ] , item = [ ] ) => acc . concat ( item ) , [ ] ) as T [ ] ; } param config param env export function getConfig ( config , env ) { const { environments = { } } = config ; const envConfig = environments [ env ] || { } ; return { require : flatten ( config . require , envConfig . require ) , components : flatten ( config . components , envConfig . components ) } ; }