Skip to content

Commit

Permalink
upd: Add resize script
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonjgardner committed Sep 3, 2024
1 parent e6757f8 commit 6782808
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 26 deletions.
25 changes: 0 additions & 25 deletions .github/workflows/branch-sync.yml

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{ "dependencies": { "sharp": "^0.33.2" }, "devDependencies": { "@types/bun": "latest", "@types/node": "^20.11.19" }, "name": "jg-rtx", "module": "index.ts", "type": "module", "peerDependencies": {
{ "dependencies": { "sharp": "^0.33.3" }, "devDependencies": { "@types/bun": "latest", "@types/node": "^20.11.19" }, "name": "jg-rtx", "module": "index.ts", "type": "module", "peerDependencies": {
"typescript": "^5.0.0"
} }
52 changes: 52 additions & 0 deletions src/scripts/resize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import sharp from "sharp";
import { readdir, copyFile, mkdir, stat } from "node:fs/promises";
import { dirname, basename } from "node:path";

const resize = async (input: string, output: string, size?: number) => {
try {
const img = sharp(input);
const { width } = await img.metadata() ?? { width: size ?? 256 };

await img.resize(size ?? Math.max(0.5 * width!, 16), null).toFile(output);
} catch (err) {
console.error(`File ${input} could not be resized: ${err}`);
}
};

const resizeImages = async (inputDir: string, outputDir: string, width: number) => {
const files = await readdir(inputDir);
for (const file of files) {
const src = `${inputDir}/${file}`;
const dest = `${outputDir}/${file}`;

if ((await stat(src)).isDirectory()) {
await resizeImages(src, dest, width);
continue;
}

try {
await mkdir(dirname(dest), { recursive: true });
} catch (err) {
console.error(`Directory ${dirname(dest)} could not be created: ${err}`);
}

if (!file.endsWith(".png")) {
// try {
// await copyFile(src, dest);
// console.log(`📋 COPIED ${basename(src)}`);
// } catch (err) {
// console.error(`File ${basename(src)} could not be copied: ${err}`);
// }
continue;
}

try {
await resize(src, dest, width);
console.log(`📏 RESIZED ${basename(src)}`);
} catch(err) {
console.error(`File ${basename(src)} could not be resized: ${err}`);
}
}
};

await resizeImages("../../bedrock/pack/RP/textures/blocks", "../../dist/64x", 64);

0 comments on commit 6782808

Please sign in to comment.