This commit is contained in:
Dax Raad
2025-05-17 21:31:42 -04:00
parent 96fbc37f01
commit a34d020bc6
26 changed files with 979 additions and 54 deletions

46
js/src/app/index.ts Normal file
View File

@@ -0,0 +1,46 @@
import fs from "fs/promises";
import { AppPath } from "./path";
import { Log } from "../util/log";
import { Context } from "../util/context";
export namespace App {
const log = Log.create({ service: "app" });
export type Info = Awaited<ReturnType<typeof create>>;
const ctx = Context.create<Info>("app");
export async function create(input: { directory: string }) {
log.info("creating");
const dataDir = AppPath.data(input.directory);
await fs.mkdir(dataDir, { recursive: true });
log.info("created", { path: dataDir });
const services = new Map<any, any>();
return {
get root() {
return input.directory;
},
service<T extends () => any>(service: any, init: T) {
if (!services.has(service)) {
log.info("registering service", { name: service });
services.set(service, init());
}
return services.get(service) as ReturnType<T>;
},
};
}
export function service<T extends () => any>(key: any, init: T) {
const app = ctx.use();
return app.service(key, init);
}
export async function use() {
return ctx.use();
}
export const provide = ctx.provide;
}