|
1 | 1 | import fs from 'fs'; |
2 | 2 | import path from 'path'; |
3 | | -import sizeOf from 'image-size'; |
4 | 3 | import { Browser, BrowserContext, Page } from "@playwright/test" |
5 | 4 | import { Context } from "../types.js" |
6 | 5 | import * as utils from "./utils.js" |
@@ -167,30 +166,47 @@ function getImageDimensions(filePath: string): { width: number, height: number } |
167 | 166 | } |
168 | 167 |
|
169 | 168 | export async function uploadScreenshots(ctx: Context): Promise<void> { |
170 | | - let screenshotsDir = ctx.uploadFilePath; |
| 169 | + const allowedExtensions = ctx.options.fileExtension.map(ext => `.${ext.trim().toLowerCase()}`); |
171 | 170 |
|
172 | | - // Read all files in the screenshots directory |
173 | | - const files = fs.readdirSync(screenshotsDir); |
| 171 | + async function processDirectory(directory: string, relativePath: string = ''): Promise<void> { |
| 172 | + const files = fs.readdirSync(directory); |
174 | 173 |
|
175 | | - for (let file of files) { |
176 | | - let fileExtension = path.extname(file).toLowerCase(); |
177 | | - if (fileExtension === '.png' || fileExtension === '.jpeg' || fileExtension === '.jpg') { |
178 | | - let filePath = `${screenshotsDir}/${file}`; |
179 | | - let ssId = path.basename(file, fileExtension); |
| 174 | + for (let file of files) { |
| 175 | + const filePath = path.join(directory, file); |
| 176 | + const stat = fs.statSync(filePath); |
| 177 | + const relativeFilePath = path.join(relativePath, file); |
180 | 178 |
|
181 | | - let viewport = 'default' |
| 179 | + if (stat.isDirectory() && ctx.options.ignorePattern.includes(relativeFilePath)) { |
| 180 | + continue; // Skip this path |
| 181 | + } |
182 | 182 |
|
183 | | - if(!ctx.options.ignoreResolutions){ |
184 | | - const dimensions = getImageDimensions(filePath); |
185 | | - if (!dimensions) { |
186 | | - throw new Error(`Unable to determine dimensions for image: ${filePath}`); |
| 183 | + if (stat.isDirectory()) { |
| 184 | + await processDirectory(filePath, relativeFilePath); // Recursively process subdirectory |
| 185 | + } else { |
| 186 | + let fileExtension = path.extname(file).toLowerCase(); |
| 187 | + if (allowedExtensions.includes(fileExtension)) { |
| 188 | + let ssId = relativeFilePath; |
| 189 | + if (ctx.options.stripExtension) { |
| 190 | + ssId = path.join(relativePath, path.basename(file, fileExtension)); |
| 191 | + } |
| 192 | + |
| 193 | + let viewport = 'default'; |
| 194 | + |
| 195 | + if (!ctx.options.ignoreResolutions) { |
| 196 | + const dimensions = getImageDimensions(filePath); |
| 197 | + if (!dimensions) { |
| 198 | + throw new Error(`Unable to determine dimensions for image: ${filePath}`); |
| 199 | + } |
| 200 | + const width = dimensions.width; |
| 201 | + const height = dimensions.height; |
| 202 | + viewport = `${width}x${height}`; |
| 203 | + } |
| 204 | + |
| 205 | + await ctx.client.uploadScreenshot(ctx.build, filePath, ssId, 'default', viewport, ctx.log); |
187 | 206 | } |
188 | | - const width = dimensions.width; |
189 | | - const height = dimensions.height; |
190 | | - viewport = `${width}x${height}`; |
191 | 207 | } |
192 | | - |
193 | | - await ctx.client.uploadScreenshot(ctx.build, filePath, ssId, 'default', viewport, ctx.log); |
194 | 208 | } |
195 | 209 | } |
| 210 | + |
| 211 | + await processDirectory(ctx.uploadFilePath); |
196 | 212 | } |
0 commit comments