50 lines
1.5 KiB
TypeScript
50 lines
1.5 KiB
TypeScript
/** biome-ignore-all assist/source/organizeImports: <_> */
|
|
/** biome-ignore-all lint/suspicious/noExplicitAny: <_> */
|
|
|
|
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`);
|
|
}
|
|
})();
|