tui-status-line-write.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { randomBytes } from 'node:crypto';
  2. import { promises as fs } from 'node:fs';
  3. import { dirname, join } from 'node:path';
  4. import yaml from 'js-yaml';
  5. import lockfile from 'proper-lockfile';
  6. /** Update only the profile's presentation setting, under the same lock as model writes. */
  7. export async function writeTuiStatusLineSetting(
  8. dataDir: string,
  9. items: readonly string[] | undefined,
  10. ): Promise<void> {
  11. const configPath = join(dataDir, 'config.yaml');
  12. let temporaryPath: string | undefined;
  13. let temporaryCreated = false;
  14. let release: (() => Promise<void>) | undefined;
  15. try {
  16. await fs.mkdir(dataDir, { recursive: true });
  17. await fs.writeFile(configPath, '', { flag: 'a', mode: 0o600 });
  18. await fs.chmod(configPath, 0o600);
  19. // Replace the real file, preserving any symlink used to manage profile settings.
  20. // Keep the temporary file on the target filesystem so rename remains atomic.
  21. const targetPath = await fs.realpath(configPath);
  22. temporaryPath = join(dirname(targetPath), `.config-tmp-${randomBytes(6).toString('hex')}`);
  23. release = await lockfile.lock(targetPath, {
  24. stale: 10_000,
  25. retries: { retries: 20, factor: 1, minTimeout: 5, maxTimeout: 25 },
  26. });
  27. const parsed: unknown = yaml.load(await fs.readFile(targetPath, 'utf8'));
  28. const document = parsed ?? {};
  29. if (!isRecord(document)) throw new Error('Invalid config document');
  30. const tui = document.tui ?? {};
  31. if (!isRecord(tui)) throw new Error('Invalid TUI settings');
  32. // Copy the subtree: YAML aliases must not mutate another settings block.
  33. const nextTui = { ...tui };
  34. if (items === undefined) delete nextTui.statusLine;
  35. else nextTui.statusLine = [...items];
  36. const next = { ...document, tui: nextTui };
  37. const mode = 0o600;
  38. const temporary = await fs.open(temporaryPath, 'wx', mode);
  39. temporaryCreated = true;
  40. try {
  41. await temporary.writeFile(yaml.dump(next, { lineWidth: -1, noRefs: true }), 'utf8');
  42. await temporary.chmod(mode);
  43. } finally {
  44. await temporary.close();
  45. }
  46. await fs.rename(temporaryPath, targetPath);
  47. } catch {
  48. // Parser errors can contain unrelated credentials from the config source.
  49. throw new Error(
  50. 'Unable to save status line settings. Check config.yaml syntax and permissions.',
  51. );
  52. } finally {
  53. if (temporaryCreated && temporaryPath) await fs.unlink(temporaryPath).catch(() => undefined);
  54. await release?.().catch(() => undefined);
  55. }
  56. }
  57. function isRecord(value: unknown): value is Record<string, unknown> {
  58. return (
  59. typeof value === 'object' &&
  60. value !== null &&
  61. !Array.isArray(value) &&
  62. Object.getPrototypeOf(value) === Object.prototype
  63. );
  64. }