Vojta Holik

Vojta Holik

// Menu: Compress Images
// Description: Compress images using imagemin
// Author: Vojta Holik
// Twitter: @vjthlk
/** @type {import("@johnlindquist/kit")} */
let imagemin = await npm("imagemin");
let imageminJpegtran = await npm("imagemin-jpegtran");
let imageminJpegRecompress = await npm("imagemin-jpeg-recompress");
let imageminPngquant = await npm("imagemin-pngquant");
let imageminSvgo = await npm("imagemin-svgo");
let imageminGifsicle = await npm("imagemin-gifsicle");
let selectedFiles = await getSelectedFile();
let filePaths;
if (selectedFiles) {
filePaths = selectedFiles.split("\n");
} else {
let droppedFiles = await drop({ placeholder: "Drop images to compress" });
filePaths = droppedFiles.map((file) => file.path);
}
for (let filePath of filePaths) {
let directory = path.dirname(filePath);
await imagemin([filePath], {
destination: directory,
plugins: [
imageminJpegtran({
arithmetic: true,
progressive: true,
}),
imageminJpegRecompress({ max: 85 }),
imageminSvgo(),
imageminGifsicle({
optimizationLevel: 2,
}),
imageminPngquant({
quality: [0.3, 0.5],
}),
],
});
}

// Menu: Tinify
// Description: Compress selected images with Tinify
// Author: Vojta Holik
// Twitter: @vjthlk
let tinify = await npm("tinify");
let fs = await import("fs");
let selectedFiles = await getSelectedFile();
tinify.key = await env("TINIFY_API_KEY", {
hint: md("get your Tinify api key at https://tinypng.com/developers"),
ignoreBlur: true,
secret: true,
});
let filePaths = selectedFiles.split("\n");
for (let filePath of filePaths) {
let directory = path.dirname(filePath);
let extension = path.extname(filePath);
let originalFileName = path.basename(filePath);
let suffix = "";
let isHD = originalFileName.includes("@2x");
let newFileName = isHD
? originalFileName
.replace("@2x", "")
.replace(extension, `${suffix}@2x${extension}`)
: originalFileName.replace(extension, `${suffix}${extension}`);
fs.readFile(filePath, (err, sourceData) => {
if (err) throw err;
tinify.fromBuffer(sourceData).toBuffer((err, resultData) => {
if (err) throw err;
fs.writeFile(`${directory}/` + newFileName, resultData, (err) => {
if (err) throw err;
});
});
});
}
<details> <summary>You can change file names and directory to best suite your workflow. Since I often work with <code>@2x</code> images I adjusted the script to correctly suffix that part.</summary>
let isHD = originalFileName.includes("@2x");
let newFileName = isHD
? originalFileName
.replace("@2x", "")
.replace(extension, `${suffix}@2x${extension}`)
: originalFileName.replace(extension, `${suffix}${extension}`);
</details>

<img src="https://p-ZmFjNlQ.b3.n0.cdn.getcloudapp.com/items/mXur6N2W/50e6f3d6-8b3d-4b24-a4c6-3ee047a9613e.gif?v=f5fe168354cb0d1e34dcbf405c7891ac" width="700" />
// Menu: Slugify file name
// Description: Slugify selected files
// Author: Vojta Holik
// Twitter: @vjthlk
let slugify = await npm("slugify")
let selectedFiles = await getSelectedFile()
const filePaths = selectedFiles.split("\n")
for (let filePath of filePaths) {
let originalFileName = path.basename(filePath)
let newFileName = slugify(originalFileName, {
lower: true,
})
let newFilePath = path.join(
path.dirname(filePath),
newFileName
)
cp(filePath, newFilePath)
}