Skip to content

Instantly share code, notes, and snippets.

@ramiroaraujo
Created April 25, 2023 02:06
Show Gist options
  • Save ramiroaraujo/51a8303fd66cc9d8b6db8a19c651254e to your computer and use it in GitHub Desktop.
Save ramiroaraujo/51a8303fd66cc9d8b6db8a19c651254e to your computer and use it in GitHub Desktop.
// Name: convert selected images
import "@johnlindquist/kit";
// Grab selected files
const files = (await getSelectedFile()).split("\n");
// Set up whitelist of formats
const supportedFormats = [".heic", ".png", ".gif", ".webp", ".jpg", ".jpeg"];
// Filter files based on supported formats
const selectedFiles = files.filter(file =>
supportedFormats.some(format => file.toLowerCase().endsWith(format))
);
// Notify if no files are selected
if (!selectedFiles.length) {
await notify("No supported files selected");
exit();
}
const convertHeic = await npm("heic-convert");
const sharp = await npm("sharp");
// Select the output format
const outputFormat = await arg("Choose an output format", [
"jpg",
"png",
"webp",
]);
const getUniquePath = async (outputPath, suffix = "") => {
if (await isFile(outputPath)) {
const name = path.basename(outputPath, path.extname(outputPath));
const newName = `${name}${suffix}-copy${path.extname(outputPath)}`;
const newPath = path.join(path.dirname(outputPath), newName);
return await getUniquePath(newPath, `${suffix}-copy`);
} else {
return outputPath;
}
};
// Convert selected files to the chosen output format using appropriate libraries
for (const file of selectedFiles) {
const content = await readFile(file);
const name = path.basename(file).split(".")[0];
const outputPath = path.join(path.dirname(file), name + `.${outputFormat}`);
const uniqueOutputPath = await getUniquePath(outputPath);
if (file.toLowerCase().endsWith(".heic")) {
const formatMap = {
jpg: "JPEG",
png: "PNG",
}
const outputBuffer = await convertHeic({
buffer: content,
format: formatMap[outputFormat],
quality: 0.5,
});
await writeFile(uniqueOutputPath, outputBuffer);
} else {
const sharpImage = sharp(content);
switch (outputFormat) {
case "jpg":
await sharpImage.jpeg({ quality: 40 }).toFile(uniqueOutputPath);
break;
case "png":
await sharpImage.png().toFile(uniqueOutputPath);
break;
case "webp":
await sharpImage.webp({ quality: 40 }).toFile(uniqueOutputPath);
break;
}
}
}
await notify(`Converted selected files to ${outputFormat.toUpperCase()}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment