Loving Kit! ❤️ Awesome work @johnlindquist 🔥

The simplicity of Airdrop between Apple devices is great until your image turns up in a file format that most file uploads still don't support. Used to spend a few mins opening up preview and re-exporting. So wrote this little script to do the heavy lifting.

// Menu: HEIC Converter
// Description: Convert the currently selected HEIC file to either JPEG or PNG
// Author: Gary McCann
const { getSelectedFile } = await kit("file");
const { promises: fs } = await npm("fs");
const path = await npm("path");
const convert = await npm("heic-convert");
const imagePath = await getSelectedFile();
const { dir, name: imageName, ext } = path.parse(imagePath);
const format = await arg(
"Which format would you like to convert to:",
async () => {
let options = ["JPEG", "PNG"];
return options.map((format) => {
return {
name: format,
value: format,
};
});
}
);
const buffer = await fs.readFile(imagePath);
const outputBuffer = await convert({
buffer,
format,
quality: 1,
});
const updatedFilePath = `${dir}/${imageName}.${format}`;
await fs.writeFile(updatedFilePath, outputBuffer);

Few bits to add:

  1. Validation to make sure a HEIC file is selected.
  2. Would like to maybe show some sort of progress bar, whilst its doing its business.