Skip to content

Commit

Permalink
feat: dry run check state
Browse files Browse the repository at this point in the history
  • Loading branch information
nhoss2 committed Jun 20, 2024
1 parent f9f9465 commit 189059d
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 33 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
"main": "index.js",
"scripts": {
"test": "jest src/test",
"cli": "tsx src/cli.ts",
"check": "tsx src/check.ts"
"cli": "tsx src/cli.ts"
},
"author": "nafis hossain <[email protected]>",
"license": "BSD-3-Clause",
Expand Down
24 changes: 24 additions & 0 deletions src/check.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { S3Client } from "@aws-sdk/client-s3";

import { getClient } from "./s3";
import { buildState } from "./state";
import { getConfig } from "./config";
import { logger } from "./logger";

export const checkState = async (): Promise<void> => {
try {
const config = getConfig();
const { bucketName, inputConfig } = config;

const s3Client: S3Client = await getClient();

await buildState(bucketName, inputConfig, s3Client);
} catch (err) {
if (err instanceof Error) {
logger.error(err.message);
} else {
logger.error("An unknown error occurred.");
}
process.exit(1);
}
};
29 changes: 26 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
import { Command } from "commander";

import { run } from "./run";
import { checkState } from "./check";

const program = new Command();

program
.command("run")
.description("process all images")
.action(async () => {
await run();
});

program
.command("check")
.description(
"check how many images have been processed and need to be processed"
)
.action(async () => {
await checkState();
});

program.parse(process.argv);

(async () => {
await run();
})();
if (!process.argv.slice(2).length) {
program.outputHelp();
}
45 changes: 24 additions & 21 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,29 @@ const ConfigSchema = z.object({

export type Config = z.infer<typeof ConfigSchema>;

const validateConfig = (config: unknown): Config => {
const configObj = ConfigSchema.parse(config);
const { inputConfig } = configObj;

const outputImages = inputConfig.outputImages;
const dimensionSet = new Set<string>();

outputImages.forEach((image, index) => {
const dimensionKey = image.width
? `width:${image.width}`
: `height:${image.height}`;
if (dimensionSet.has(dimensionKey)) {
throw new Error(
`Duplicate dimension found in OutputImageConfig at index ${index}: ${dimensionKey}`
);
}

dimensionSet.add(dimensionKey);
});

return configObj;
};

export const getConfig = (): Config => {
try {
const explorerSync = cosmiconfigSync("lilsync");
Expand All @@ -37,27 +60,7 @@ export const getConfig = (): Config => {
throw new Error("configuration file not found or is empty");
}

const configObj = ConfigSchema.parse(result.config);
const { inputConfig } = configObj;

const outputImages = inputConfig.outputImages;

const dimensionSet = new Set<string>();

outputImages.forEach((image, index) => {
const dimensionKey = image.width
? `width:${image.width}`
: `height:${image.height}`;
if (dimensionSet.has(dimensionKey)) {
throw new Error(
`Duplicate dimension found in OutputImageConfig at index ${index}: ${dimensionKey}`
);
}

dimensionSet.add(dimensionKey);
});

return configObj;
return validateConfig(result.config);
} catch (err) {
if (err instanceof z.ZodError) {
const formattedErrors = err.errors
Expand Down
2 changes: 0 additions & 2 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ export const run = async (): Promise<void> => {

const s3Client = await getClient();

logger.info("building state");

const state = await buildState(bucketName, inputConfig, s3Client);

logger.info("processing images");
Expand Down
21 changes: 16 additions & 5 deletions src/state.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ansis from "ansis";
import { S3Client } from "@aws-sdk/client-s3";

import type {
Expand Down Expand Up @@ -201,6 +202,8 @@ export const buildState = async (
config: InputConfig,
s3Client: S3Client
): Promise<State> => {
logger.info(ansis.gray("building state"));

const inputFileKeys = await listBucketObjects(
bucketName,
s3Client,
Expand All @@ -214,11 +217,13 @@ export const buildState = async (

const allFileKeys = [...inputFileKeys, ...outputFileKeys];

logger.info(`number of files: ${allFileKeys.length}`);
logger.info(`number of files: ${ansis.green(String(allFileKeys.length))}`);

const inputBaseNames = collectInputImages(allFileKeys, config);

logger.info(`input images ${Object.keys(inputBaseNames).length}`);
logger.info(
`input images: ${ansis.green(String(Object.keys(inputBaseNames).length))}`
);

const publishedBaseNames = collectProcessedImages(
allFileKeys,
Expand All @@ -232,11 +237,17 @@ export const buildState = async (
},
0
);
logger.info(`output images already created: ${numOutputImages}`);
logger.info(
`output images already created: ${ansis.green(String(numOutputImages))}`
);

const unmatchedOutputImages = findUnmatchedImages(publishedBaseNames);

logger.info(`unmatched output images: ${unmatchedOutputImages.length}`);
logger.info(
`unmatched output images: ${ansis.green(
String(unmatchedOutputImages.length)
)}`
);

const missedImagesToCreate = getPartialProcessedImages(
config,
Expand All @@ -257,7 +268,7 @@ export const buildState = async (
0
);

logger.info(`images to create: ${numImagesToCreate}`);
logger.info(`images to create: ${ansis.green(String(numImagesToCreate))}`);

return {
config,
Expand Down

0 comments on commit 189059d

Please sign in to comment.