refactor(format): update formatter interface to return command from enabled() (#20703)

This commit is contained in:
Dax
2026-04-02 12:22:01 -04:00
committed by GitHub
parent 93a139315c
commit 3faabdadb7
3 changed files with 104 additions and 89 deletions
+24 -21
View File
@@ -41,7 +41,7 @@ export namespace Format {
const state = yield* InstanceState.make(
Effect.fn("Format.state")(function* (_ctx) {
const enabled: Record<string, boolean> = {}
const commands: Record<string, string[] | false> = {}
const formatters: Record<string, Formatter.Info> = {}
const cfg = yield* config.get()
@@ -56,30 +56,32 @@ export namespace Format {
continue
}
const info = mergeDeep(formatters[name] ?? {}, {
command: [],
extensions: [],
...item,
})
if (info.command.length === 0) continue
formatters[name] = {
...info,
name,
enabled: async () => true,
enabled: async () => info.command ?? false,
}
}
} else {
log.info("all formatters are disabled")
}
async function isEnabled(item: Formatter.Info) {
let status = enabled[item.name]
if (status === undefined) {
status = await item.enabled()
enabled[item.name] = status
async function getCommand(item: Formatter.Info) {
let cmd = commands[item.name]
if (cmd === false || cmd === undefined) {
cmd = await item.enabled()
commands[item.name] = cmd
}
return status
return cmd
}
async function isEnabled(item: Formatter.Info) {
const cmd = await getCommand(item)
return cmd !== false
}
async function getFormatter(ext: string) {
@@ -87,17 +89,17 @@ export namespace Format {
const checks = await Promise.all(
matching.map(async (item) => {
log.info("checking", { name: item.name, ext })
const on = await isEnabled(item)
if (on) {
const cmd = await getCommand(item)
if (cmd) {
log.info("enabled", { name: item.name, ext })
}
return {
item,
enabled: on,
cmd,
}
}),
)
return checks.filter((x) => x.enabled).map((x) => x.item)
return checks.filter((x) => x.cmd).map((x) => ({ item: x.item, cmd: x.cmd! }))
}
function formatFile(filepath: string) {
@@ -105,13 +107,14 @@ export namespace Format {
log.info("formatting", { file: filepath })
const ext = path.extname(filepath)
for (const item of yield* Effect.promise(() => getFormatter(ext))) {
log.info("running", { command: item.command })
const cmd = item.command.map((x) => x.replace("$FILE", filepath))
for (const { item, cmd } of yield* Effect.promise(() => getFormatter(ext))) {
if (cmd === false) continue
log.info("running", { command: cmd })
const replaced = cmd.map((x) => x.replace("$FILE", filepath))
const dir = yield* InstanceState.directory
const code = yield* spawner
.spawn(
ChildProcess.make(cmd[0]!, cmd.slice(1), {
ChildProcess.make(replaced[0]!, replaced.slice(1), {
cwd: dir,
env: item.environment,
extendEnv: true,
@@ -124,7 +127,7 @@ export namespace Format {
Effect.sync(() => {
log.error("failed to format file", {
error: "spawn failed",
command: item.command,
command: cmd,
...item.environment,
file: filepath,
})
@@ -134,7 +137,7 @@ export namespace Format {
)
if (code !== 0) {
log.error("failed", {
command: item.command,
command: cmd,
...item.environment,
})
}