mirror of
https://github.com/Start9Labs/start-sdk.git
synced 2026-03-30 12:21:57 +00:00
feat: Create the autoconfigure
This commit is contained in:
70
lib/autoconfig/AutoConfig.ts
Normal file
70
lib/autoconfig/AutoConfig.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import deepmerge from "deepmerge";
|
||||
import { AutoConfigure, Effects } from "../types";
|
||||
import { Message, MaybePromise, ReadonlyDeep } from ".";
|
||||
|
||||
class AutoConfigBuilt<Config> implements AutoConfigure<Config> {
|
||||
constructor(private autoConfig: AutoConfig<Config>) {}
|
||||
|
||||
async check(effects: Effects, config: Config): Promise<void> {
|
||||
for (const [message, configure] of this.autoConfig.getConfigures()) {
|
||||
const value = await configure({ effects, config });
|
||||
if (value !== null) {
|
||||
throw new Error(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
/** This is called after we know that the dependency package needs a new configuration, this would be a transform for defaults */
|
||||
async autoConfigure(effects: Effects, config: Config): Promise<Config> {
|
||||
const input = { effects, config };
|
||||
const newOverwrites = (
|
||||
await Promise.all(this.autoConfig.getConfigures().map((x) => x[1](input)))
|
||||
).filter((x): x is NonNullable<typeof x> => x !== null);
|
||||
return deepmerge.all([config, ...newOverwrites]);
|
||||
}
|
||||
}
|
||||
export class AutoConfig<Config> {
|
||||
private constructor(
|
||||
private configures: Array<
|
||||
[
|
||||
Message,
|
||||
(
|
||||
options: Readonly<{ config: Config; effects: Effects }>
|
||||
) => MaybePromise<null | Partial<Config>>
|
||||
]
|
||||
>
|
||||
) {}
|
||||
getConfigures(): ReadonlyDeep<
|
||||
Array<
|
||||
[
|
||||
Message,
|
||||
(
|
||||
options: Readonly<{ config: Config; effects: Effects }>
|
||||
) => MaybePromise<null | Partial<Config>>
|
||||
]
|
||||
>
|
||||
> {
|
||||
return this.configures;
|
||||
}
|
||||
|
||||
static autoConfig<Config>(
|
||||
message: Message,
|
||||
configure: (
|
||||
options: Readonly<{ config: Config; effects: Effects }>
|
||||
) => MaybePromise<null | Partial<Config>>
|
||||
): AutoConfig<Config> {
|
||||
return new AutoConfig([[message, configure]]);
|
||||
}
|
||||
autoConfig(
|
||||
message: Message,
|
||||
configure: (
|
||||
options: Readonly<{ config: Config; effects: Effects }>
|
||||
) => MaybePromise<null | Partial<Config>>
|
||||
): AutoConfig<Config> {
|
||||
this.configures.push([message, configure]);
|
||||
return this;
|
||||
}
|
||||
|
||||
build() {
|
||||
return new AutoConfigBuilt(this);
|
||||
}
|
||||
}
|
||||
14
lib/autoconfig/autoconfigSetup.ts
Normal file
14
lib/autoconfig/autoconfigSetup.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { ExpectedExports, PackageId } from "../types";
|
||||
import { AutoConfig } from "./AutoConfig";
|
||||
|
||||
export function autoconfigSetup<Config>(
|
||||
autoconfigs: Record<PackageId, AutoConfig<Config>>
|
||||
) {
|
||||
const autoconfig: ExpectedExports.autoConfig<Config> = {};
|
||||
|
||||
for (const [id, autoconfigValue] of Object.entries(autoconfigs)) {
|
||||
autoconfig[id] = autoconfigValue.build();
|
||||
}
|
||||
|
||||
return autoconfig;
|
||||
}
|
||||
9
lib/autoconfig/index.ts
Normal file
9
lib/autoconfig/index.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
// prettier-ignore
|
||||
export type ReadonlyDeep<A> =
|
||||
A extends Function ? A :
|
||||
A extends {} ? { readonly [K in keyof A]: ReadonlyDeep<A[K]> } : A;
|
||||
export type MaybePromise<A> = Promise<A> | A;
|
||||
export type Message = string;
|
||||
|
||||
export { AutoConfig } from "./AutoConfig";
|
||||
export { autoconfigSetup } from "./autoconfigSetup";
|
||||
Reference in New Issue
Block a user