( filePath: string, schema: z.ZodType<T>, value: T, )
| 49 | } |
| 50 | |
| 51 | export async function writeJsonFile<T>( |
| 52 | filePath: string, |
| 53 | schema: z.ZodType<T>, |
| 54 | value: T, |
| 55 | ): Promise<void> { |
| 56 | assertNonConfigWrite(filePath); |
| 57 | const parsed = schema.parse(value); |
| 58 | await mkdir(dirname(filePath), { recursive: true }); |
| 59 | const tmpPath = tempPathFor(filePath); |
| 60 | try { |
| 61 | await writeFile(tmpPath, `${JSON.stringify(parsed, null, 2)}\n`, 'utf-8'); |
| 62 | await rename(tmpPath, filePath); |
| 63 | } catch (error) { |
| 64 | await unlink(tmpPath).catch(() => {}); |
| 65 | throw error; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | export async function readJsonlFile<T>( |
| 70 | filePath: string, |
no test coverage detected