This commit is contained in:
Patrick Alvin Alcala 2025-11-26 14:01:40 +08:00
parent baed4fd80e
commit fec3abd5d8
15 changed files with 141 additions and 119 deletions

View file

@ -0,0 +1,49 @@
// deno-lint-ignore-file no-explicit-any
import { consola } from "consola";
import process from "node:process";
import sharp from "sharp";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
(async () => {
const argv = yargs(hideBin(process.argv))
.option("name", {
alias: "n",
describe: "Specify the name of the image",
type: "string",
demandOption: true,
})
.option("size", {
alias: "s",
describe: "Specify the size of the image",
type: "number",
demandOption: true,
}).argv;
const name = argv.name;
const size = argv.size;
try {
const avifOutputPath = `./@dasig/images/${name.toString().split(".").slice(0, -1).join(".")}.avif`;
const webpOutputPath = `./@dasig/images/${name.toString().split(".").slice(0, -1).join(".")}.webp`;
const avifBuffer = await sharp(`./src/images/${name}`)
.avif({ quality: 60 })
.resize(size)
.toBuffer();
await sharp(avifBuffer).toFile(avifOutputPath);
consola.success(`${name} successfully optimized in Avif`);
const webpBuffer = await sharp(`./src/images/${name}`)
.webp({ quality: 75 })
.resize(size)
.toBuffer();
await sharp(webpBuffer).toFile(webpOutputPath);
consola.success(`${name} successfully optimized in Webp`);
} catch (error: any) {
consola.error("Error optimizing image:", error);
if (error.message.includes("missing"))
consola.error(`${name} could not be found on image folder`);
}
})();