(input: MigrateInput)
| 27 | * skips only locations where a tui.json already exists. |
| 28 | */ |
| 29 | export async function migrateTuiConfig(input: MigrateInput) { |
| 30 | const opencode = await opencodeFiles(input) |
| 31 | for (const file of opencode) { |
| 32 | const source = await Filesystem.readText(file).catch(() => undefined) |
| 33 | if (!source) continue |
| 34 | const errors: JsoncParseError[] = [] |
| 35 | const data = parseJsonc(source, errors, { allowTrailingComma: true }) |
| 36 | if (errors.length || !data || typeof data !== "object" || Array.isArray(data)) continue |
| 37 | |
| 38 | const theme = decodeTheme("theme" in data ? data.theme : undefined) |
| 39 | const keybinds = decodeRecord("keybinds" in data ? data.keybinds : undefined) |
| 40 | const legacyTui = decodeRecord("tui" in data ? data.tui : undefined) |
| 41 | const extracted = { |
| 42 | theme: Option.getOrUndefined(theme), |
| 43 | keybinds: Option.getOrUndefined(keybinds), |
| 44 | tui: Option.getOrUndefined(legacyTui), |
| 45 | } |
| 46 | const tui = extracted.tui ? normalizeTui(extracted.tui) : undefined |
| 47 | if (extracted.theme === undefined && extracted.keybinds === undefined && !tui) continue |
| 48 | |
| 49 | const target = path.join(path.dirname(file), "tui.json") |
| 50 | const targetExists = await Filesystem.exists(target) |
| 51 | if (targetExists) continue |
| 52 | |
| 53 | const payload: Record<string, unknown> = { |
| 54 | $schema: TUI_SCHEMA_URL, |
| 55 | } |
| 56 | if (extracted.theme !== undefined) payload.theme = extracted.theme |
| 57 | if (extracted.keybinds !== undefined) payload.keybinds = extracted.keybinds |
| 58 | if (tui) Object.assign(payload, tui) |
| 59 | |
| 60 | const wrote = await Filesystem.write(target, JSON.stringify(payload, null, 2)) |
| 61 | .then(() => true) |
| 62 | .catch(() => false) |
| 63 | if (!wrote) continue |
| 64 | |
| 65 | const stripped = await backupAndStripLegacy(file, source) |
| 66 | if (!stripped) continue |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | function normalizeTui(data: Record<string, unknown>): |
| 71 | | { |
no test coverage detected