feat: add .kuberrc.ts configuration

This commit is contained in:
2026-08-30 18:46:25 +07:00 Unverified
parent 32ec172335
commit 716246659c
15 changed files with 682 additions and 93 deletions
+31 -15
View File
@@ -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,
);