2026-03-23 18:22:49 +09:00
|
|
|
import path from "path"
|
|
|
|
|
import { log } from "../../shared"
|
2026-05-05 22:41:45 +09:00
|
|
|
import { spawn as bunSpawn } from "../../shared/bun-spawn-shim"
|
2026-03-23 18:22:49 +09:00
|
|
|
|
|
|
|
|
interface FormatterConfig {
|
|
|
|
|
disabled?: boolean
|
|
|
|
|
command?: string[]
|
|
|
|
|
environment?: Record<string, string>
|
|
|
|
|
extensions?: string[]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface OpencodeConfig {
|
|
|
|
|
formatter?:
|
|
|
|
|
| false
|
|
|
|
|
| Record<string, FormatterConfig>
|
|
|
|
|
experimental?: {
|
|
|
|
|
hook?: {
|
|
|
|
|
file_edited?: Record<string, Array<{ command: string[]; environment?: Record<string, string> }>>
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-23 18:46:44 +09:00
|
|
|
export interface FormatterClient {
|
2026-03-23 18:22:49 +09:00
|
|
|
config: {
|
|
|
|
|
get: (options?: { query?: { directory?: string } }) => Promise<{ data?: OpencodeConfig }>
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-24 20:30:16 +09:00
|
|
|
type FormatterDefinition = { command: string[]; environment: Record<string, string> }
|
|
|
|
|
type FormatterMap = Map<string, FormatterDefinition[]>
|
|
|
|
|
|
|
|
|
|
const cachedFormattersByDirectory = new Map<string, FormatterMap>()
|
|
|
|
|
|
|
|
|
|
function getFormatterCacheKey(directory: string): string {
|
|
|
|
|
return path.resolve(directory)
|
|
|
|
|
}
|
2026-03-23 18:22:49 +09:00
|
|
|
|
2026-03-23 18:46:44 +09:00
|
|
|
export async function resolveFormatters(
|
|
|
|
|
client: FormatterClient,
|
2026-03-23 18:22:49 +09:00
|
|
|
directory: string,
|
2026-03-24 20:30:16 +09:00
|
|
|
): Promise<FormatterMap> {
|
|
|
|
|
const cacheKey = getFormatterCacheKey(directory)
|
|
|
|
|
const cachedFormatters = cachedFormattersByDirectory.get(cacheKey)
|
2026-03-23 18:22:49 +09:00
|
|
|
if (cachedFormatters) return cachedFormatters
|
|
|
|
|
|
2026-03-24 20:30:16 +09:00
|
|
|
const result = new Map<string, FormatterDefinition[]>()
|
2026-03-23 18:22:49 +09:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const response = await client.config.get({ query: { directory } })
|
|
|
|
|
const config = response.data
|
|
|
|
|
if (!config) return result
|
|
|
|
|
|
|
|
|
|
if (config.formatter && typeof config.formatter === "object") {
|
|
|
|
|
for (const [, formatter] of Object.entries(config.formatter)) {
|
|
|
|
|
if (formatter.disabled || !formatter.command?.length || !formatter.extensions?.length) continue
|
|
|
|
|
for (const ext of formatter.extensions) {
|
|
|
|
|
const normalizedExt = ext.startsWith(".") ? ext : `.${ext}`
|
|
|
|
|
const existing = result.get(normalizedExt) ?? []
|
|
|
|
|
existing.push({
|
|
|
|
|
command: formatter.command,
|
|
|
|
|
environment: formatter.environment ?? {},
|
|
|
|
|
})
|
|
|
|
|
result.set(normalizedExt, existing)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (config.experimental?.hook?.file_edited) {
|
|
|
|
|
for (const [ext, commands] of Object.entries(config.experimental.hook.file_edited)) {
|
|
|
|
|
const normalizedExt = ext.startsWith(".") ? ext : `.${ext}`
|
|
|
|
|
const existing = result.get(normalizedExt) ?? []
|
|
|
|
|
for (const cmd of commands) {
|
|
|
|
|
existing.push({
|
|
|
|
|
command: cmd.command,
|
|
|
|
|
environment: cmd.environment ?? {},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
result.set(normalizedExt, existing)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-24 20:30:16 +09:00
|
|
|
|
|
|
|
|
cachedFormattersByDirectory.set(cacheKey, result)
|
2026-03-23 18:22:49 +09:00
|
|
|
} catch (error) {
|
|
|
|
|
log("[formatter-trigger] Failed to fetch formatter config", { error })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-23 18:46:44 +09:00
|
|
|
export function buildFormatterCommand(command: string[], filePath: string): string[] {
|
2026-03-23 18:22:49 +09:00
|
|
|
return command.map((arg) => arg.replace(/\$FILE/g, filePath))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function runFormattersForFile(
|
2026-03-23 18:46:44 +09:00
|
|
|
client: FormatterClient,
|
2026-03-23 18:22:49 +09:00
|
|
|
directory: string,
|
|
|
|
|
filePath: string,
|
|
|
|
|
): Promise<void> {
|
|
|
|
|
const ext = path.extname(filePath)
|
|
|
|
|
if (!ext) return
|
|
|
|
|
|
|
|
|
|
const formatters = await resolveFormatters(client, directory)
|
|
|
|
|
const matching = formatters.get(ext)
|
|
|
|
|
if (!matching?.length) return
|
|
|
|
|
|
|
|
|
|
for (const formatter of matching) {
|
|
|
|
|
const cmd = buildFormatterCommand(formatter.command, filePath)
|
|
|
|
|
try {
|
|
|
|
|
log("[formatter-trigger] Running formatter", { command: cmd, file: filePath })
|
2026-05-05 22:41:45 +09:00
|
|
|
const proc = bunSpawn(cmd, {
|
2026-03-23 18:22:49 +09:00
|
|
|
cwd: directory,
|
|
|
|
|
env: { ...process.env, ...formatter.environment },
|
|
|
|
|
stdout: "ignore",
|
|
|
|
|
stderr: "pipe",
|
|
|
|
|
})
|
|
|
|
|
await proc.exited
|
|
|
|
|
if (proc.exitCode !== 0) {
|
|
|
|
|
const stderr = await new Response(proc.stderr).text()
|
|
|
|
|
log("[formatter-trigger] Formatter failed", {
|
|
|
|
|
command: cmd,
|
|
|
|
|
exitCode: proc.exitCode,
|
|
|
|
|
stderr: stderr.slice(0, 500),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
log("[formatter-trigger] Formatter execution error", { command: cmd, error })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function clearFormatterCache(): void {
|
2026-03-24 20:30:16 +09:00
|
|
|
cachedFormattersByDirectory.clear()
|
2026-03-23 18:22:49 +09:00
|
|
|
}
|