Skip to content

Commit 3774360

Browse files
add more options for upload command
1 parent 992bae1 commit 3774360

File tree

4 files changed

+55
-21
lines changed

4 files changed

+55
-21
lines changed

src/commander/upload.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ command
1616
.description('Upload screenshots from given directory')
1717
.argument('<directory>', 'Path of the directory')
1818
.option('-R, --ignoreResolutions', 'Ignore resolution')
19+
.option('-F, --files <extensions>', 'Comma-separated list of allowed file extensions', val => {
20+
return val.split(',').map(ext => ext.trim().toLowerCase());
21+
})
22+
.option('-E, --ignoreStripExtensions', 'Strips file extensions from snapshot names')
23+
.option('-i, --ignorePattern <patterns>', 'Comma-separated list of directories to ignore', val => {
24+
return val.split(',').map(pattern => pattern.trim());
25+
})
1926
.action(async function(directory, _, command) {
2027
let ctx: Context = ctxInit(command.optsWithGlobals());
2128

src/lib/ctx.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ export default (options: Record<string, string>): Context => {
1414
let config = constants.DEFAULT_CONFIG;
1515
let port: number;
1616
let resolutionOff: boolean;
17-
17+
let extensionFiles: string;
18+
let ignoreStripExtension: Array<string>;
19+
let ignoreFilePattern: Array<string>;
1820
try {
1921
if (options.config) {
2022
config = JSON.parse(fs.readFileSync(options.config, 'utf-8'));
@@ -35,6 +37,9 @@ export default (options: Record<string, string>): Context => {
3537
throw new Error('Invalid port number. Port number must be an integer between 1 and 65535.');
3638
}
3739
resolutionOff = options.ignoreResolutions || false;
40+
extensionFiles = options.files || ['png', 'jpeg', 'jpg'];
41+
ignoreStripExtension = options.ignoreStripExtensions || false
42+
ignoreFilePattern = options.ignorePattern || []
3843
} catch (error: any) {
3944
console.log(`[smartui] Error: ${error.message}`);
4045
process.exit();
@@ -85,7 +90,10 @@ export default (options: Record<string, string>): Context => {
8590
markBaseline: options.markBaseline ? true : false,
8691
buildName: options.buildName || '',
8792
port: port,
88-
ignoreResolutions: resolutionOff
93+
ignoreResolutions: resolutionOff,
94+
fileExtension: extensionFiles,
95+
stripExtension: ignoreStripExtension,
96+
ignorePattern: ignoreFilePattern,
8997
},
9098
cliVersion: version,
9199
totalSnapshots: -1

src/lib/screenshot.ts

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import fs from 'fs';
22
import path from 'path';
3-
import sizeOf from 'image-size';
43
import { Browser, BrowserContext, Page } from "@playwright/test"
54
import { Context } from "../types.js"
65
import * as utils from "./utils.js"
@@ -167,30 +166,47 @@ function getImageDimensions(filePath: string): { width: number, height: number }
167166
}
168167

169168
export async function uploadScreenshots(ctx: Context): Promise<void> {
170-
let screenshotsDir = ctx.uploadFilePath;
169+
const allowedExtensions = ctx.options.fileExtension.map(ext => `.${ext.trim().toLowerCase()}`);
171170

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);
174173

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);
180178

181-
let viewport = 'default'
179+
if (stat.isDirectory() && ctx.options.ignorePattern.includes(relativeFilePath)) {
180+
continue; // Skip this path
181+
}
182182

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);
187206
}
188-
const width = dimensions.width;
189-
const height = dimensions.height;
190-
viewport = `${width}x${height}`;
191207
}
192-
193-
await ctx.client.uploadScreenshot(ctx.build, filePath, ssId, 'default', viewport, ctx.log);
194208
}
195209
}
210+
211+
await processDirectory(ctx.uploadFilePath);
196212
}

src/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ export interface Context {
3333
buildName?: string,
3434
port?: number,
3535
ignoreResolutions?: boolean,
36+
fileExtension?: Array<string>,
37+
stripExtension?: boolean,
38+
ignorePattern?: Array<string>,
3639
}
3740
cliVersion: string;
3841
totalSnapshots: number;

0 commit comments

Comments
 (0)