Skip to content

Commit ec4d1dc

Browse files
committed
refactor: extract safe file utils
Signed-off-by: Jakub Freisler <[email protected]>
1 parent 02f6672 commit ec4d1dc

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/tasks.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,20 @@ const initGetScreenshotPathTask: () => Cypress.Tasks = () => ({
3030
},
3131
});
3232

33+
const unlinkSyncSafe = (path: string) =>
34+
fs.existsSync(path) && fs.unlinkSync(path);
35+
const moveSyncSafe = (pathFrom: string, pathTo: string) =>
36+
fs.existsSync(pathFrom) && moveFile.sync(pathFrom, pathTo);
37+
3338
const initApproveImageTask: () => Cypress.Tasks = () => ({
3439
[TASK.approveImage]({ img }) {
3540
const oldImg = img.replace(FILE_SUFFIX.actual, "");
36-
if (fs.existsSync(oldImg)) fs.unlinkSync(oldImg);
41+
unlinkSyncSafe(oldImg);
3742

3843
const diffImg = img.replace(FILE_SUFFIX.actual, FILE_SUFFIX.diff);
39-
if (fs.existsSync(diffImg)) fs.unlinkSync(diffImg);
44+
unlinkSyncSafe(diffImg);
4045

41-
if (fs.existsSync(img)) moveFile.sync(img, oldImg);
46+
moveSyncSafe(img, oldImg);
4247

4348
return null;
4449
},

0 commit comments

Comments
 (0)