Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions action/dist/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -83310,6 +83310,12 @@ var PutObjectCommand = class extends Command2.classBuilder().ep({
}).s("AmazonS3", "PutObject", {}).n("S3Client", "PutObjectCommand").sc(PutObject$).build() {
};

// src/s3-client.ts
var s3Client = new S3Client();
var listObjects = (input) => s3Client.send(new ListObjectsV2Command(input));
var getObject = (input) => s3Client.send(new GetObjectCommand(input));
var putObject = (input) => s3Client.send(new PutObjectCommand(input));

// ../shared/index.ts
var VISUAL_REGRESSION_CONTEXT = "Visual Regression";
var BASE_IMAGES_DIRECTORY = "base-images";
Expand Down Expand Up @@ -159664,8 +159670,7 @@ var Jimp = createJimp({
plugins: defaultPlugins
});

// src/s3-operations.ts
var s3Client = new S3Client();
// src/resize.ts
async function resizeImageIfNeeded(buffer) {
const resizeWidth = getInput("resize-width");
const resizeHeight = getInput("resize-height");
Expand All @@ -159687,26 +159692,26 @@ async function resizeImageIfNeeded(buffer) {
}
return image2.getBuffer("image/png");
}

// src/s3-operations.ts
async function checkS3PrefixExists(bucketName, prefix) {
try {
const command = new ListObjectsV2Command({
const response = await listObjects({
Bucket: bucketName,
Prefix: prefix,
MaxKeys: 1
});
const response = await s3Client.send(command);
return (response.Contents?.length ?? 0) > 0;
} catch {
return false;
}
}
async function downloadS3Directory(bucketName, s3Prefix, localDir) {
info(`Downloading base images from s3://${bucketName}/${s3Prefix}`);
const command = new ListObjectsV2Command({
const response = await listObjects({
Bucket: bucketName,
Prefix: s3Prefix
});
const response = await s3Client.send(command);
const allObjects = response.Contents ?? [];
const baseObjects = allObjects.filter((obj2) => obj2.Key?.endsWith("base.png"));
info(`Found ${baseObjects.length} base image(s) to download`);
Expand All @@ -159715,11 +159720,10 @@ async function downloadS3Directory(bucketName, s3Prefix, localDir) {
const relativePath = Key.substring(s3Prefix.length);
const localFilePath = path4.join(localDir, relativePath);
await import_fs7.promises.mkdir(path4.dirname(localFilePath), { recursive: true });
const getCommand = new GetObjectCommand({
const { Body } = await getObject({
Bucket: bucketName,
Key
});
const { Body } = await s3Client.send(getCommand);
if (Body instanceof import_stream10.Readable) {
const writeStream = fs6.createWriteStream(localFilePath);
await new Promise((resolve3, reject2) => {
Expand Down Expand Up @@ -159748,12 +159752,11 @@ async function uploadLocalDirectoryWithResize(localDir, bucketName, s3Prefix) {
const s3Key = path4.join(s3Prefix, file);
const fileBuffer = await import_fs7.promises.readFile(localFilePath);
const resizedBuffer = await resizeImageIfNeeded(fileBuffer);
const command = new PutObjectCommand({
await putObject({
Bucket: bucketName,
Key: s3Key,
Body: resizedBuffer
});
await s3Client.send(command);
});
info(
`Uploaded ${filesFromFailingTests.length} file(s) to s3://${bucketName}/${s3Prefix}`
Expand All @@ -159763,12 +159766,11 @@ async function uploadSingleFile(localFilePath, s3Key) {
const bucketName = getInput("bucket-name", { required: true });
const fileBuffer = await import_fs7.promises.readFile(localFilePath);
const resizedBuffer = await resizeImageIfNeeded(fileBuffer);
const command = new PutObjectCommand({
await putObject({
Bucket: bucketName,
Key: s3Key,
Body: resizedBuffer
});
await s3Client.send(command);
info(`Uploaded ${localFilePath} to s3://${bucketName}/${s3Key}`);
}
var downloadBaseImages = async () => {
Expand Down Expand Up @@ -159836,12 +159838,11 @@ async function uploadOriginalNewPngs(localDir, bucketName, s3Prefix) {
const localFilePath = path4.join(localDir, file);
const s3Key = path4.join(s3Prefix, file);
const fileBuffer = await import_fs7.promises.readFile(localFilePath);
const command = new PutObjectCommand({
await putObject({
Bucket: bucketName,
Key: s3Key,
Body: fileBuffer
});
await s3Client.send(command);
});
info(
`Uploaded ${files.length} original new.png file(s) to s3://${bucketName}/${s3Prefix}`
Expand Down
2 changes: 1 addition & 1 deletion action/dist/main.js.map

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions action/src/resize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { getInput } from '@actions/core';
import { Jimp } from 'jimp';

export async function resizeImageIfNeeded(buffer: Buffer): Promise<Buffer> {
const resizeWidth = getInput('resize-width');
const resizeHeight = getInput('resize-height');

if (!resizeWidth && !resizeHeight) {
return buffer;
}
const width = resizeWidth ? Number(resizeWidth) : undefined;
const height = resizeHeight ? Number(resizeHeight) : undefined;
if ((width && isNaN(width)) || (height && isNaN(height))) {
throw new Error('resize-width and resize-height must be valid numbers');
}

const image = await Jimp.read(buffer);
if (width && height) {
image.cover({ w: width, h: height });
} else if (width) {
image.resize({ w: width });
} else if (height) {
image.resize({ h: height });
}

return image.getBuffer('image/png');
}
29 changes: 29 additions & 0 deletions action/src/s3-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {
S3Client,
ListObjectsV2Command,
ListObjectsV2CommandInput,
ListObjectsV2CommandOutput,
GetObjectCommand,
GetObjectCommandInput,
GetObjectCommandOutput,
PutObjectCommand,
PutObjectCommandInput,
PutObjectCommandOutput
} from '@aws-sdk/client-s3';

const s3Client = new S3Client();

export const listObjects = (
input: ListObjectsV2CommandInput
): Promise<ListObjectsV2CommandOutput> =>
s3Client.send(new ListObjectsV2Command(input));

export const getObject = (
input: GetObjectCommandInput
): Promise<GetObjectCommandOutput> =>
s3Client.send(new GetObjectCommand(input));

export const putObject = (
input: PutObjectCommandInput
): Promise<PutObjectCommandOutput> =>
s3Client.send(new PutObjectCommand(input));
59 changes: 8 additions & 51 deletions action/src/s3-operations.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { getInput, info } from '@actions/core';
import {
S3Client,
ListObjectsV2Command,
GetObjectCommand,
PutObjectCommand
} from '@aws-sdk/client-s3';
import { listObjects, getObject, putObject } from './s3-client';
import {
BASE_IMAGE_NAME,
BASE_IMAGES_DIRECTORY,
Expand All @@ -18,46 +13,18 @@ import * as fs from 'fs';
import { promises as fsPromises } from 'fs';
import { glob } from 'glob';
import { Readable } from 'stream';
import { Jimp } from 'jimp';

const s3Client = new S3Client();

async function resizeImageIfNeeded(buffer: Buffer): Promise<Buffer> {
const resizeWidth = getInput('resize-width');
const resizeHeight = getInput('resize-height');

if (!resizeWidth && !resizeHeight) {
return buffer;
}
const width = resizeWidth ? Number(resizeWidth) : undefined;
const height = resizeHeight ? Number(resizeHeight) : undefined;
if ((width && isNaN(width)) || (height && isNaN(height))) {
throw new Error('resize-width and resize-height must be valid numbers');
}

const image = await Jimp.read(buffer);
if (width && height) {
image.cover({ w: width, h: height });
} else if (width) {
image.resize({ w: width });
} else if (height) {
image.resize({ h: height });
}

return image.getBuffer('image/png');
}
import { resizeImageIfNeeded } from './resize';

async function checkS3PrefixExists(
bucketName: string,
prefix: string
): Promise<boolean> {
try {
const command = new ListObjectsV2Command({
const response = await listObjects({
Bucket: bucketName,
Prefix: prefix,
MaxKeys: 1
});
const response = await s3Client.send(command);
return (response.Contents?.length ?? 0) > 0;
} catch {
return false;
Expand All @@ -71,12 +38,10 @@ async function downloadS3Directory(
): Promise<void> {
info(`Downloading base images from s3://${bucketName}/${s3Prefix}`);

const command = new ListObjectsV2Command({
const response = await listObjects({
Bucket: bucketName,
Prefix: s3Prefix
});

const response = await s3Client.send(command);
const allObjects = response.Contents ?? [];
const baseObjects = allObjects.filter(obj => obj.Key?.endsWith('base.png'));

Expand All @@ -90,12 +55,10 @@ async function downloadS3Directory(

await fsPromises.mkdir(path.dirname(localFilePath), { recursive: true });

const getCommand = new GetObjectCommand({
const { Body } = await getObject({
Bucket: bucketName,
Key
});

const { Body } = await s3Client.send(getCommand);
if (Body instanceof Readable) {
const writeStream = fs.createWriteStream(localFilePath);
await new Promise((resolve, reject) => {
Expand Down Expand Up @@ -137,13 +100,11 @@ async function uploadLocalDirectoryWithResize(
const fileBuffer = await fsPromises.readFile(localFilePath);
const resizedBuffer = await resizeImageIfNeeded(fileBuffer);

const command = new PutObjectCommand({
await putObject({
Bucket: bucketName,
Key: s3Key,
Body: resizedBuffer
});

await s3Client.send(command);
});

info(
Expand All @@ -158,13 +119,11 @@ async function uploadSingleFile(
const bucketName = getInput('bucket-name', { required: true });
const fileBuffer = await fsPromises.readFile(localFilePath);
const resizedBuffer = await resizeImageIfNeeded(fileBuffer);
const command = new PutObjectCommand({
await putObject({
Bucket: bucketName,
Key: s3Key,
Body: resizedBuffer
});

await s3Client.send(command);
info(`Uploaded ${localFilePath} to s3://${bucketName}/${s3Key}`);
}

Expand Down Expand Up @@ -248,13 +207,11 @@ async function uploadOriginalNewPngs(

const fileBuffer = await fsPromises.readFile(localFilePath);

const command = new PutObjectCommand({
await putObject({
Bucket: bucketName,
Key: s3Key,
Body: fileBuffer
});

await s3Client.send(command);
});

info(
Expand Down
Loading
Loading