(
effects: ParsedPatchFile,
{
dryRun,
bestEffort,
errors,
cwd,
}: { dryRun: boolean; cwd?: string; errors?: string[]; bestEffort: boolean },
)
| 4 | import { assertNever } from "../assertNever" |
| 5 | |
| 6 | export const executeEffects = ( |
| 7 | effects: ParsedPatchFile, |
| 8 | { |
| 9 | dryRun, |
| 10 | bestEffort, |
| 11 | errors, |
| 12 | cwd, |
| 13 | }: { dryRun: boolean; cwd?: string; errors?: string[]; bestEffort: boolean }, |
| 14 | ) => { |
| 15 | const inCwd = (path: string) => (cwd ? join(cwd, path) : path) |
| 16 | const humanReadable = (path: string) => relative(process.cwd(), inCwd(path)) |
| 17 | effects.forEach((eff) => { |
| 18 | switch (eff.type) { |
| 19 | case "file deletion": |
| 20 | if (dryRun) { |
| 21 | if (!fs.existsSync(inCwd(eff.path))) { |
| 22 | throw new Error( |
| 23 | "Trying to delete file that doesn't exist: " + |
| 24 | humanReadable(eff.path), |
| 25 | ) |
| 26 | } |
| 27 | } else { |
| 28 | // TODO: integrity checks |
| 29 | try { |
| 30 | fs.unlinkSync(inCwd(eff.path)) |
| 31 | } catch (e) { |
| 32 | if (bestEffort) { |
| 33 | errors?.push(`Failed to delete file ${eff.path}`) |
| 34 | } else { |
| 35 | throw e |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | break |
| 40 | case "rename": |
| 41 | if (dryRun) { |
| 42 | // TODO: see what patch files look like if moving to exising path |
| 43 | if (!fs.existsSync(inCwd(eff.fromPath))) { |
| 44 | throw new Error( |
| 45 | "Trying to move file that doesn't exist: " + |
| 46 | humanReadable(eff.fromPath), |
| 47 | ) |
| 48 | } |
| 49 | } else { |
| 50 | try { |
| 51 | fs.moveSync(inCwd(eff.fromPath), inCwd(eff.toPath)) |
| 52 | } catch (e) { |
| 53 | if (bestEffort) { |
| 54 | errors?.push( |
| 55 | `Failed to rename file ${eff.fromPath} to ${eff.toPath}`, |
| 56 | ) |
| 57 | } else { |
| 58 | throw e |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | break |
| 63 | case "file creation": |
no test coverage detected
searching dependent graphs…