feat: add .kuberrc.ts configuration
This commit is contained in:
+31
-15
@@ -1,35 +1,51 @@
|
||||
import { AsyncLocalStorage } from "node:async_hooks";
|
||||
import type { ComposeSpecification } from "../schema/docker.d";
|
||||
import { getProject, throwWhen } from "./shared";
|
||||
import type { KuberHookContext } from "../types";
|
||||
import { hookContext, loadConfig, type ResolvedKuberConfig } from "./config";
|
||||
import { readCompose, resolveComposeFile } from "./yaml";
|
||||
|
||||
type AppContext = {
|
||||
project: string;
|
||||
cwd: string;
|
||||
config: ResolvedKuberConfig;
|
||||
path(): Promise<string | undefined>;
|
||||
hookContext(): Promise<KuberHookContext>;
|
||||
compose(): Promise<ComposeSpecification>;
|
||||
};
|
||||
|
||||
const appContext = new AsyncLocalStorage<AppContext>();
|
||||
|
||||
export function provideContext<T>(fn: () => T): T {
|
||||
export async function provideContext<T>(
|
||||
fn: () => T,
|
||||
configPath?: string,
|
||||
): Promise<Awaited<T>> {
|
||||
const cwd = process.cwd();
|
||||
const path = createCachedGetter("composeFile", () => resolveComposeFile(cwd));
|
||||
return appContext.run(
|
||||
const config = await loadConfig(cwd, configPath);
|
||||
const path = createCachedGetter("composeFile", () =>
|
||||
config.composeFile
|
||||
? Promise.resolve(config.composeFile)
|
||||
: resolveComposeFile(cwd),
|
||||
);
|
||||
const getHookContext = createCachedGetter("hookContext", async () => {
|
||||
const composeFile = await path();
|
||||
if (!composeFile) throw new Error("Compose file cannot be found");
|
||||
return hookContext(config, cwd, composeFile);
|
||||
});
|
||||
const compose = createCachedGetter("compose", async () => {
|
||||
const context = await getHookContext();
|
||||
const value = await readCompose(context.composeFile);
|
||||
await config.compose?.(value, context);
|
||||
return value;
|
||||
});
|
||||
|
||||
return await appContext.run(
|
||||
{
|
||||
project: getProject(),
|
||||
project: config.project,
|
||||
cwd,
|
||||
config,
|
||||
path,
|
||||
compose: createCachedGetter("compose", () =>
|
||||
path()
|
||||
.then(
|
||||
throwWhen(
|
||||
(path): path is undefined => path === undefined,
|
||||
"Compose file cannot be found",
|
||||
),
|
||||
)
|
||||
.then(readCompose),
|
||||
),
|
||||
hookContext: getHookContext,
|
||||
compose,
|
||||
},
|
||||
fn,
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user