Write `next` to `absPath` only if it differs from `original`, returning a standardized change response.
( c: RouteContext, projectDir: string, filePath: string, absPath: string, original: string, next: string, )
| 157 | |
| 158 | /** Write `next` to `absPath` only if it differs from `original`, returning a standardized change response. */ |
| 159 | function writeIfChanged( |
| 160 | c: RouteContext, |
| 161 | projectDir: string, |
| 162 | filePath: string, |
| 163 | absPath: string, |
| 164 | original: string, |
| 165 | next: string, |
| 166 | ): Response { |
| 167 | if (next === original) { |
| 168 | return c.json({ ok: true, changed: false, content: original, path: filePath }); |
| 169 | } |
| 170 | const backup = snapshotBeforeWrite(projectDir, absPath); |
| 171 | if (backup.error) console.warn(`Failed to create backup for ${filePath}: ${backup.error}`); |
| 172 | writeFileSync(absPath, next, "utf-8"); |
| 173 | return c.json({ |
| 174 | ok: true, |
| 175 | changed: true, |
| 176 | content: next, |
| 177 | path: filePath, |
| 178 | backupPath: backupPathForResponse(projectDir, backup.backupPath), |
| 179 | }); |
| 180 | } |
| 181 | |
| 182 | function rejectUnsafeMutationValues( |
| 183 | c: RouteContext, |
no test coverage detected