* Write JSON file to workspace session directory with file locking. * Creates session directory if it doesn't exist. * * If options.shouldWrite returns false, the write is skipped (and the session * directory is not created).
(
workspaceId: string,
data: T,
options?: SessionFileWriteOptions
)
| 66 | * directory is not created). |
| 67 | */ |
| 68 | async write( |
| 69 | workspaceId: string, |
| 70 | data: T, |
| 71 | options?: SessionFileWriteOptions |
| 72 | ): Promise<Result<void>> { |
| 73 | return this.fileLocks.withLock(workspaceId, async () => { |
| 74 | try { |
| 75 | if (options?.shouldWrite && !options.shouldWrite()) { |
| 76 | return Ok(undefined); |
| 77 | } |
| 78 | |
| 79 | const sessionDir = this.config.getSessionDir(workspaceId); |
| 80 | await fs.mkdir(sessionDir, { recursive: true }); |
| 81 | const filePath = this.getFilePath(workspaceId); |
| 82 | // Atomic write prevents corruption if app crashes mid-write |
| 83 | await writeFileAtomic(filePath, JSON.stringify(data, null, 2)); |
| 84 | return Ok(undefined); |
| 85 | } catch (error) { |
| 86 | const message = getErrorMessage(error); |
| 87 | return Err(`Failed to write ${this.fileName}: ${message}`); |
| 88 | } |
| 89 | }); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Delete JSON file from workspace session directory with file locking. |
nothing calls this directly
no test coverage detected