import { AutoConfigure, Effects, ExpectedExports } from "../types"; import { deepEqual, deepMerge } from "../util"; export type AutoConfigFrom = { [key: string]: (options: { effects: Effects; localConfig: unknown; remoteConfig: unknown; }) => Promise>; }; export class AutoConfig { constructor( readonly configs: AutoConfigFrom, readonly path: keyof AutoConfigFrom, ) {} async check( options: Parameters[0], ): ReturnType { const origConfig = JSON.parse(JSON.stringify(options.localConfig)); if ( !deepEqual( origConfig, deepMerge( {}, options.localConfig, await this.configs[this.path](options), ), ) ) throw new Error(`Check failed for ${this.path}`); } async autoConfigure( options: Parameters[0], ): ReturnType { return deepMerge( {}, options.localConfig, await this.configs[this.path](options), ); } }