lazy load formatters

This commit is contained in:
Dax Raad
2025-06-27 11:29:20 -04:00
parent 334161a30e
commit 2ec0611f42
19 changed files with 408 additions and 435 deletions

View File

@@ -2,7 +2,6 @@ import "zod-openapi/extend"
import { Log } from "../util/log"
import { Context } from "../util/context"
import { Filesystem } from "../util/filesystem"
import { Project } from "../util/project"
import { Global } from "../global"
import path from "path"
import os from "os"
@@ -13,7 +12,6 @@ export namespace App {
export const Info = z
.object({
project: z.string(),
user: z.string(),
hostname: z.string(),
git: z.boolean(),
@@ -33,11 +31,21 @@ export namespace App {
})
export type Info = z.infer<typeof Info>
const ctx = Context.create<Awaited<ReturnType<typeof create>>>("app")
const ctx = Context.create<{
info: Info
services: Map<any, { state: any; shutdown?: (input: any) => Promise<void> }>
}>("app")
const APP_JSON = "app.json"
async function create(input: { cwd: string }) {
export type Input = {
cwd: string
}
export async function provide<T>(
input: Input,
cb: (app: App.Info) => Promise<T>,
) {
log.info("creating", {
cwd: input.cwd,
})
@@ -66,10 +74,8 @@ export namespace App {
>()
const root = git ?? input.cwd
const project = await Project.getName(root)
const info: Info = {
project: project,
user: os.userInfo().username,
hostname: os.hostname(),
time: {
@@ -84,12 +90,20 @@ export namespace App {
cwd: input.cwd,
},
}
const result = {
const app = {
services,
info,
}
return result
return ctx.provide(app, async () => {
const result = await cb(app.info)
for (const [key, entry] of app.services.entries()) {
if (!entry.shutdown) continue
log.info("shutdown", { name: key })
await entry.shutdown?.(await entry.state)
}
return result
})
}
export function state<State>(
@@ -115,22 +129,6 @@ export namespace App {
return ctx.use().info
}
export async function provide<T>(
input: { cwd: string },
cb: (app: Info) => Promise<T>,
) {
const app = await create(input)
return ctx.provide(app, async () => {
const result = await cb(app.info)
for (const [key, entry] of app.services.entries()) {
if (!entry.shutdown) continue
log.info("shutdown", { name: key })
await entry.shutdown?.(await entry.state)
}
return result
})
}
export async function initialize() {
const { info } = ctx.use()
info.time.initialized = Date.now()