mirror of
https://gitea.toothfairyai.com/ToothFairyAI/tf_code.git
synced 2026-03-31 06:12:26 +00:00
This release has a bunch of minor breaking changes if you are using opencode plugins or sdk 1. storage events have been removed (we might bring this back but had some issues) 2. concept of `app` is gone - there is a new concept called `project` and endpoints to list projects and get the current project 3. plugin receives `directory` which is cwd and `worktree` which is where the root of the project is if it's a git repo 4. the session.chat function has been renamed to session.prompt in sdk. it no longer requires model to be passed in (model is now an object) 5. every endpoint takes an optional `directory` parameter to operate as though opencode is running in that directory
98 lines
2.6 KiB
TypeScript
98 lines
2.6 KiB
TypeScript
import { Bus } from "../bus"
|
|
import { File } from "../file"
|
|
import { Log } from "../util/log"
|
|
import path from "path"
|
|
|
|
import * as Formatter from "./formatter"
|
|
import { Config } from "../config/config"
|
|
import { mergeDeep } from "remeda"
|
|
import { Instance } from "../project/instance"
|
|
|
|
export namespace Format {
|
|
const log = Log.create({ service: "format" })
|
|
|
|
const state = Instance.state(async () => {
|
|
const enabled: Record<string, boolean> = {}
|
|
const cfg = await Config.get()
|
|
|
|
const formatters = { ...Formatter } as Record<string, Formatter.Info>
|
|
for (const [name, item] of Object.entries(cfg.formatter ?? {})) {
|
|
if (item.disabled) {
|
|
delete formatters[name]
|
|
continue
|
|
}
|
|
const result: Formatter.Info = mergeDeep(formatters[name] ?? {}, {
|
|
command: [],
|
|
extensions: [],
|
|
...item,
|
|
})
|
|
result.enabled = async () => true
|
|
result.name = name
|
|
formatters[name] = result
|
|
}
|
|
|
|
return {
|
|
enabled,
|
|
formatters,
|
|
}
|
|
})
|
|
|
|
async function isEnabled(item: Formatter.Info) {
|
|
const s = await state()
|
|
let status = s.enabled[item.name]
|
|
if (status === undefined) {
|
|
status = await item.enabled()
|
|
s.enabled[item.name] = status
|
|
}
|
|
return status
|
|
}
|
|
|
|
async function getFormatter(ext: string) {
|
|
const formatters = await state().then((x) => x.formatters)
|
|
const result = []
|
|
for (const item of Object.values(formatters)) {
|
|
log.info("checking", { name: item.name, ext })
|
|
if (!item.extensions.includes(ext)) continue
|
|
if (!(await isEnabled(item))) continue
|
|
result.push(item)
|
|
}
|
|
return result
|
|
}
|
|
|
|
export function init() {
|
|
log.info("init")
|
|
Bus.subscribe(File.Event.Edited, async (payload) => {
|
|
const file = payload.properties.file
|
|
log.info("formatting", { file })
|
|
const ext = path.extname(file)
|
|
|
|
for (const item of await getFormatter(ext)) {
|
|
log.info("running", { command: item.command })
|
|
try {
|
|
const proc = Bun.spawn({
|
|
cmd: item.command.map((x) => x.replace("$FILE", file)),
|
|
cwd: Instance.directory,
|
|
env: { ...process.env, ...item.environment },
|
|
stdout: "ignore",
|
|
stderr: "ignore",
|
|
})
|
|
const exit = await proc.exited
|
|
if (exit !== 0)
|
|
log.error("failed", {
|
|
command: item.command,
|
|
...item.environment,
|
|
})
|
|
} catch (error) {
|
|
log.error("failed", {
|
|
error,
|
|
command: item.command,
|
|
...item.environment,
|
|
})
|
|
// re-raising
|
|
throw error
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|