Skip to content

Commit 38e5ff5

Browse files
committed
fix: create missing dirs when renaming screenshot files
Signed-off-by: Jakub Freisler <[email protected]>
1 parent 2a82dcd commit 38e5ff5

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"Cypress image snapshot"
6262
],
6363
"dependencies": {
64+
"move-file": "^2.1.0",
6465
"pixelmatch": "^5.2.1",
6566
"pngjs": "^6.0.0"
6667
},

src/plugins.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import pixelmatch from "pixelmatch";
33
import fs from "fs";
44
import { PNG } from "pngjs";
55
import { FILE_SUFFIX, IMAGE_SNAPSHOT_PREFIX, TASK } from "./constants";
6+
import moveFile from "move-file";
67

78
type NotFalsy<T> = T extends false | null | undefined ? never : T;
89

@@ -78,7 +79,7 @@ export const initPlugin = (
7879
const diffImg = img.replace(FILE_SUFFIX.actual, FILE_SUFFIX.diff);
7980
if (fs.existsSync(diffImg)) fs.unlinkSync(diffImg);
8081

81-
fs.renameSync(img, oldImg);
82+
moveFile.sync(img, oldImg);
8283

8384
return null;
8485
},
@@ -146,7 +147,7 @@ export const initPlugin = (
146147
imgDiff = 0;
147148
}
148149

149-
fs.renameSync(cfg.imgNew, cfg.imgOld);
150+
moveFile.sync(cfg.imgNew, cfg.imgOld);
150151

151152
if (typeof imgDiff !== "undefined") {
152153
const roundedImgDiff = Math.ceil(imgDiff * 1000) / 1000;
@@ -177,19 +178,19 @@ export const initPlugin = (
177178
path.join(config.projectRoot, newRelativePath)
178179
);
179180

180-
fs.rename(details.path, newAbsolutePath, (err) => {
181-
if (err) return reject(err);
181+
void moveFile(details.path, newAbsolutePath)
182+
.then(() => {
183+
fs.rm(
184+
path.join(screenshotsFolder, IMAGE_SNAPSHOT_PREFIX),
185+
{ recursive: true, force: true },
186+
(err) => {
187+
if (err) return reject(err);
182188

183-
fs.rm(
184-
path.join(screenshotsFolder, IMAGE_SNAPSHOT_PREFIX),
185-
{ recursive: true, force: true },
186-
(err) => {
187-
if (err) return reject(err);
188-
189-
resolve({ path: newAbsolutePath });
190-
}
191-
);
192-
});
189+
resolve({ path: newAbsolutePath });
190+
}
191+
);
192+
})
193+
.catch(reject);
193194
});
194195
});
195196
};

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,6 +1414,7 @@ __metadata:
14141414
eslint-plugin-cypress: ^2.12.1
14151415
eslint-plugin-eslint-comments: ^3.2.0
14161416
microbundle: ^0.14.1
1417+
move-file: ^2.1.0
14171418
pixelmatch: ^5.2.1
14181419
pngjs: ^6.0.0
14191420
prettier: ^2.4.1
@@ -6396,6 +6397,15 @@ fsevents@~2.3.2:
63966397
languageName: node
63976398
linkType: hard
63986399

6400+
"move-file@npm:^2.1.0":
6401+
version: 2.1.0
6402+
resolution: "move-file@npm:2.1.0"
6403+
dependencies:
6404+
path-exists: ^4.0.0
6405+
checksum: 2b92bbe047a302b593fcb2c0bf1131bb090ec80b3264569fc80d782c8ff829eecc13573943fa4d804c9747dec612ef2d1e84a5cfcf29cbc64a69ffcbb7ea09b3
6406+
languageName: node
6407+
linkType: hard
6408+
63996409
"mri@npm:^1.1.0":
64006410
version: 1.2.0
64016411
resolution: "mri@npm:1.2.0"

0 commit comments

Comments
 (0)