GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — opencode/packages/opencode/src/format/index.ts at dev · githubhjs/opencode Expand file tree/ Copy pathindex.tsMore file actions 203 lines (176 loc) · 6.71 KB / Copy pathindex.tsFile metadata and controls203 lines (176 loc) · 6.71 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 import { LayerNode } from "@opencode-ai/core/effect/layer-node" import { Effect, Layer, Context, Schema } from "effect" import { serviceUse } from "@opencode-ai/core/effect/service-use" import { ChildProcess } from "effect/unstable/process" import { AppProcess } from "@opencode-ai/core/process" import { InstanceState } from "@/effect/instance-state" import path from "path" import { mergeDeep } from "remeda" import { Config } from "@/config/config" import { RuntimeFlags } from "@/effect/runtime-flags" import { errorMessage } from "@/util/error" import * as Formatter from "./formatter" export const Status = Schema.Struct({ name: Schema.String, extensions: Schema.Array(Schema.String), enabled: Schema.Boolean, }).annotate({ identifier: "FormatterStatus" }) export type Status = Schema.Schema.Type<typeof Status> export interface Interface { readonly init: () => Effect.Effect<void> readonly status: () => Effect.Effect<Status[]> readonly file: (filepath: string) => Effect.Effect<boolean> } export class Service extends Context.Service<Service, Interface>()("@opencode/Format") {} export const use = serviceUse(Service) const layer = Layer.effect( Service, Effect.gen(function* () { const config = yield* Config.Service const appProcess = yield* AppProcess.Service const flags = yield* RuntimeFlags.Service const state = yield* InstanceState.make( Effect.fn("Format.state")(function* (ctx) { const commands: Record<string, string[] | false> = {} const formatters: Record<string, Formatter.Info> = {} async function getCommand(item: Formatter.Info) { let cmd = commands[item.name] if (cmd === false || cmd === undefined) { cmd = await item.enabled({ ...ctx, experimentalOxfmt: flags.experimentalOxfmt }) commands[item.name] = cmd } return cmd } async function isEnabled(item: Formatter.Info) { const cmd = await getCommand(item) return cmd !== false } async function getFormatter(ext: string) { const matching = Object.values(formatters).filter((item) => item.extensions.includes(ext)) const checks = await Promise.all( matching.map(async (item) => { const cmd = await getCommand(item) return { item, cmd, } }), ) return checks .filter((x): x is { item: Formatter.Info; cmd: string[] } => x.cmd !== false) .map((x) => ({ item: x.item, cmd: x.cmd })) } function formatFile(filepath: string) { return Effect.gen(function* () { yield* Effect.logInfo("formatting", { file: filepath }) const formatters = yield* Effect.promise(() => getFormatter(path.extname(filepath))) if (!formatters.length) return false for (const { item, cmd } of formatters) { yield* Effect.logInfo("running", { command: cmd }) const replaced = cmd.map((x) => x.replace("$FILE", filepath)) const dir = yield* InstanceState.directory const result = yield* appProcess .run( ChildProcess.make(replaced[0]!, replaced.slice(1), { cwd: dir, env: item.environment, extendEnv: true, stdin: "ignore", stdout: "ignore", stderr: "ignore", }), ) .pipe( Effect.catch((error) => Effect.logError("failed to format file", { error: "spawn failed", command: cmd, ...item.environment, file: filepath, cause: errorMessage(error.cause ?? error), }).pipe(Effect.as(undefined)), ), ) if (result && result.exitCode !== 0) { yield* Effect.logError("failed", { command: cmd, ...item.environment, }) } } return true }) } const cfg = yield* config.get() if (!cfg.formatter) { yield* Effect.logInfo("all formatters are disabled") yield* Effect.logInfo("init") return { formatters, isEnabled, formatFile, } } for (const item of Object.values(Formatter)) { formatters[item.name] = item } if (cfg.formatter !== true) { for (const [name, item] of Object.entries(cfg.formatter)) { const builtIn = Formatter[name as keyof typeof Formatter] // Ruff and uv are both the same formatter, so disabling either should disable both. if (["ruff", "uv"].includes(name) && (cfg.formatter.ruff?.disabled || cfg.formatter.uv?.disabled)) { // TODO combine formatters so shared backends like Ruff/uv don't need linked disable handling here. delete formatters.ruff delete formatters.uv continue } if (item.disabled) { delete formatters[name] continue } const info = mergeDeep(builtIn ?? { extensions: [] }, item) formatters[name] = { ...info, name, extensions: info.extensions ?? [], enabled: builtIn && !info.command ? builtIn.enabled : async (_context) => info.command ?? false, } } } yield* Effect.logInfo("init") return { formatters, isEnabled, formatFile, } }), ) const init = Effect.fn("Format.init")(function* () { yield* InstanceState.get(state) }) const status = Effect.fn("Format.status")(function* () { const { formatters, isEnabled } = yield* InstanceState.get(state) const result: Status[] = [] for (const formatter of Object.values(formatters)) { const isOn = yield* Effect.promise(() => isEnabled(formatter)) result.push({ name: formatter.name, extensions: formatter.extensions, enabled: isOn, }) } return result }) const file = Effect.fn("Format.file")(function* (filepath: string) { const { formatFile } = yield* InstanceState.get(state) return yield* formatFile(filepath) }) return Service.of({ init, status, file }) }), ) export const node = LayerNode.make({ service: Service, layer: layer, deps: [Config.node, AppProcess.node, RuntimeFlags.node], }) export * as Format from "." |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |