@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 20 lines 691 Bytes
Raw
1 import type { File } from "expo-file-system";
2
3 let tempFileSequence = 0;
4
5 /** Use distinct staging files for concurrent writes and remove them on failure. */
6 export async function writeFileAtomically(file: File, contents: string): Promise<void> {
7 const { File: FileConstructor } = await import("expo-file-system");
8 tempFileSequence += 1;
9 const temp = new FileConstructor(file.parentDirectory, `${file.name}.${tempFileSequence}.tmp`);
10 try {
11 temp.create({ intermediates: true, overwrite: true });
12 await temp.write(contents);
13 temp.moveSync(file, { overwrite: true });
14 } catch (error) {
15 try {
16 if (temp.exists) temp.delete();
17 } catch {}
18 throw error;
19 }
20 }