Skip to content

Commit 3db1628

Browse files
committed
chore: run autofix
Signed-off-by: Jakub Freisler <[email protected]>
1 parent 1ba38a6 commit 3db1628

File tree

4 files changed

+60
-35
lines changed

4 files changed

+60
-35
lines changed

src/afterScreenshot.hook.ts

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ import { IMAGE_SNAPSHOT_PREFIX, PATH_VARIABLES } from "./constants";
55

66
type NotFalsy<T> = T extends false | null | undefined ? never : T;
77

8-
const MIMIC_ROOT_WIN_REGEX = new RegExp(`^${PATH_VARIABLES.winSystemRootPath}\\${path.sep}([A-Z])\\${path.sep}`);
9-
const MIMIC_ROOT_UNIX_REGEX = new RegExp(`^${PATH_VARIABLES.unixSystemRootPath}\\${path.sep}`);
8+
const MIMIC_ROOT_WIN_REGEX = new RegExp(
9+
`^${PATH_VARIABLES.winSystemRootPath}\\${path.sep}([A-Z])\\${path.sep}`
10+
);
11+
const MIMIC_ROOT_UNIX_REGEX = new RegExp(
12+
`^${PATH_VARIABLES.unixSystemRootPath}\\${path.sep}`
13+
);
1014

1115
const getConfigVariableOrThrow = <K extends keyof Cypress.PluginConfigOptions>(
1216
config: Cypress.PluginConfigOptions,
@@ -22,29 +26,36 @@ const getConfigVariableOrThrow = <K extends keyof Cypress.PluginConfigOptions>(
2226
/* c8 ignore stop */
2327

2428
const parseAbsolutePath = ({
25-
screenshotPath, projectRoot,
29+
screenshotPath,
30+
projectRoot,
2631
}: {
27-
screenshotPath: string; projectRoot: string;
32+
screenshotPath: string;
33+
projectRoot: string;
2834
}) => {
2935
let newAbsolutePath: string;
3036
const matchedMimicingWinRoot = screenshotPath.match(MIMIC_ROOT_WIN_REGEX);
3137
const matchedMimicingUnixRoot = screenshotPath.match(MIMIC_ROOT_UNIX_REGEX);
3238
if (matchedMimicingWinRoot && matchedMimicingWinRoot[1]) {
3339
const driveLetter = matchedMimicingWinRoot[1];
34-
newAbsolutePath = path.join(`${driveLetter}:\\`, screenshotPath.substring(matchedMimicingWinRoot[0].length));
40+
newAbsolutePath = path.join(
41+
`${driveLetter}:\\`,
42+
screenshotPath.substring(matchedMimicingWinRoot[0].length)
43+
);
3544
} else if (matchedMimicingUnixRoot) {
36-
newAbsolutePath = path.sep + screenshotPath.substring(matchedMimicingUnixRoot[0].length);
45+
newAbsolutePath =
46+
path.sep + screenshotPath.substring(matchedMimicingUnixRoot[0].length);
3747
} else {
3848
newAbsolutePath = path.join(projectRoot, screenshotPath);
3949
}
4050
return path.normalize(newAbsolutePath);
41-
}
51+
};
4252

4353
export const initAfterScreenshotHook =
4454
(config: Cypress.PluginConfigOptions) =>
4555
(
4656
details: Cypress.ScreenshotDetails
47-
): void
57+
):
58+
| void
4859
| Cypress.AfterScreenshotReturnObject
4960
| Promise<Cypress.AfterScreenshotReturnObject> => {
5061
// it's not a screenshot generated by FRSOURCE Cypress Plugin Visual Regression Diff
@@ -55,23 +66,21 @@ export const initAfterScreenshotHook =
5566
config,
5667
"screenshotsFolder"
5768
);
69+
const screenshotPath = details.name.substring(
70+
IMAGE_SNAPSHOT_PREFIX.length + path.sep.length
71+
);
72+
const newAbsolutePath = parseAbsolutePath({
73+
screenshotPath,
74+
projectRoot: config.projectRoot,
75+
});
5876

59-
return new Promise(async (resolve, reject) => {
60-
const screenshotPath = details.name.substring(
61-
IMAGE_SNAPSHOT_PREFIX.length + path.sep.length
62-
);
63-
const newAbsolutePath = parseAbsolutePath({ screenshotPath, projectRoot: config.projectRoot });
64-
65-
try {
66-
await moveFile(details.path, newAbsolutePath);
67-
await fs.rm(
68-
path.join(screenshotsFolder, IMAGE_SNAPSHOT_PREFIX),
69-
{ recursive: true, force: true },
70-
);
77+
return (async () => {
78+
await moveFile(details.path, newAbsolutePath);
79+
await fs.rm(path.join(screenshotsFolder, IMAGE_SNAPSHOT_PREFIX), {
80+
recursive: true,
81+
force: true,
82+
});
7183

72-
resolve({ path: newAbsolutePath });
73-
} catch(e) {
74-
reject(e);
75-
}
76-
});
84+
return { path: newAbsolutePath };
85+
})();
7786
};

src/commands.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,24 @@ const constructCypressError = (log: Cypress.Log, err: Error) => {
4040
return err;
4141
};
4242

43-
export const getConfig = (options: Cypress.MatchImageOptions) => {
44-
const imagesDir = options.imagesDir || Cypress.env("pluginVisualRegressionImagesDir") as string | undefined;
43+
const getImagesDir = (options: Cypress.MatchImageOptions) => {
44+
const imagesDir =
45+
options.imagesDir ||
46+
(Cypress.env("pluginVisualRegressionImagesDir") as string | undefined);
4547

4648
// TODO: remove in 4.0.0
4749
if (imagesDir) {
48-
console.warn('@frsource/cypress-plugin-visual-regression-diff] `imagesDir` option is deprecated, use `imagesPath` instead (https://github.com/FRSOURCE/cypress-plugin-visual-regression-diff#configuration)');
50+
console.warn(
51+
"@frsource/cypress-plugin-visual-regression-diff] `imagesDir` option is deprecated, use `imagesPath` instead (https://github.com/FRSOURCE/cypress-plugin-visual-regression-diff#configuration)"
52+
);
4953
}
5054

55+
return imagesDir;
56+
};
57+
58+
export const getConfig = (options: Cypress.MatchImageOptions) => {
59+
const imagesDir = getImagesDir(options);
60+
5161
return {
5262
scaleFactor:
5363
Cypress.env("pluginVisualRegressionForceDeviceScaleFactor") === false
@@ -60,7 +70,7 @@ export const getConfig = (options: Cypress.MatchImageOptions) => {
6070
| undefined) ||
6171
false,
6272
imagesPath:
63-
imagesDir && `{spec_path}/${imagesDir}` ||
73+
(imagesDir && `{spec_path}/${imagesDir}`) ||
6474
options.imagesPath ||
6575
(Cypress.env("pluginVisualRegressionImagesPath") as string | undefined) ||
6676
"{spec_path}/__image_snapshots__",
@@ -83,7 +93,7 @@ export const getConfig = (options: Cypress.MatchImageOptions) => {
8393
| undefined) ||
8494
{},
8595
};
86-
}
96+
};
8797

8898
Cypress.Commands.add(
8999
"matchImage",

src/constants.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ export const TASK = {
1717
};
1818

1919
export const PATH_VARIABLES = {
20-
specPath: '{spec_path}',
21-
unixSystemRootPath: '{unix_system_root_path}',
22-
winSystemRootPath: '{win_system_root_path}',
20+
specPath: "{spec_path}",
21+
unixSystemRootPath: "{unix_system_root_path}",
22+
winSystemRootPath: "{win_system_root_path}",
2323
};
2424

2525
export const WINDOWS_LIKE_DRIVE_REGEX = /^[A-Z]:$/;

src/task.hook.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ import { PNG } from "pngjs";
44
import pixelmatch, { PixelmatchOptions } from "pixelmatch";
55
import moveFile from "move-file";
66
import sanitize from "sanitize-filename";
7-
import { FILE_SUFFIX, IMAGE_SNAPSHOT_PREFIX, TASK, PATH_VARIABLES, WINDOWS_LIKE_DRIVE_REGEX } from "./constants";
7+
import {
8+
FILE_SUFFIX,
9+
IMAGE_SNAPSHOT_PREFIX,
10+
TASK,
11+
PATH_VARIABLES,
12+
WINDOWS_LIKE_DRIVE_REGEX,
13+
} from "./constants";
814
import { alignImagesToSameSize, importAndScaleImage } from "./image.utils";
915
import type { CompareImagesTaskReturn } from "./types";
1016

@@ -53,7 +59,7 @@ export const getScreenshotPathTask = ({
5359
...imagesPath.split("/").map(parsePathPartVariables),
5460
`${sanitize(title)}${FILE_SUFFIX.actual}.png`
5561
);
56-
}
62+
};
5763

5864
export const approveImageTask = ({ img }: { img: string }) => {
5965
const oldImg = img.replace(FILE_SUFFIX.actual, "");

0 commit comments

Comments
 (0)