Skip to content

Commit

Permalink
feat: add code files
Browse files Browse the repository at this point in the history
  • Loading branch information
vboechat committed May 21, 2024
1 parent 60d7a69 commit bc3e47a
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/conversors/conversor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { readdirSync } from "fs";
import { extname } from "path";
import { convertImage } from "./convert-image";

export const convertImagesInFolder = async (folderPath: string, outputFormat: string = 'png') => {
const files = readdirSync(folderPath);
const isHeicFile = (file: string) => extname(file).toLowerCase() === '.heic';
const heicFiles = files.filter(isHeicFile);

const conversionPromises = heicFiles.map(file => convertImage(file, folderPath, outputFormat));

await Promise.all(conversionPromises);
};
29 changes: 29 additions & 0 deletions src/conversors/convert-image.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { join, basename, extname } from 'path';
import { convertHeicToPng } from './to-png';
import { convertHeicToJpg } from './to-jpg';
import { handleError } from '../lib/error-handler';

export const convertImage = (file: string, folderPath: string, outputFormat: string) => {
const extension = extname(file).toLowerCase();
if (extension !== '.heic') return;

const inputPath = join(folderPath, file);
const outputFilename = `${basename(file, extension)}.${outputFormat}`;
const outputPath = join(folderPath, outputFilename);

const conversionFunction = {
'png': convertHeicToPng,
'jpg': convertHeicToJpg,
'jpeg': convertHeicToJpg
}[outputFormat];

if (!conversionFunction) {
console.log(`Unsupported format: ${outputFormat}`);
return;
}

return handleError(async () => {
await conversionFunction(inputPath, outputPath);
console.log(`Converted ${file} to ${outputFilename}`);
});
};
10 changes: 10 additions & 0 deletions src/conversors/to-jpg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import sharp from "sharp";
import { handleError } from "../lib/error-handler";

export const convertHeicToJpg = (inputPath: string, outputPath: string) => {
return handleError(async () => {
return await sharp(inputPath)
.jpeg({ quality: 100 })
.toFile(outputPath)
});
};
10 changes: 10 additions & 0 deletions src/conversors/to-png.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import sharp from "sharp";
import { handleError } from "../lib/error-handler";

export const convertHeicToPng = (inputPath: string, outputPath: string) => {
return handleError(async () => {
await sharp(inputPath)
.png({ quality: 100, compressionLevel: 0 })
.toFile(outputPath);
});
};
22 changes: 22 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env node

import { Command } from 'commander';
import { convertImagesInFolder } from './conversors/conversor';
import { getPackageVersion } from './lib/versioning';

const program = new Command();

program
.version(getPackageVersion())
.description('Convert HEIC images to PNG or JPG')
.requiredOption('-f, --format <type>', 'Output format (png or jpg)')
.requiredOption('-p, --path <folder>', 'Path to the folder containing HEIC images')
.action((options) => {
const { format, path: folderPath } = options;

convertImagesInFolder(folderPath, format)
.then(() => console.log('Conversion complete'))
.catch(error => console.error('Error during conversion:', error));
});

program.parse(process.argv);

0 comments on commit bc3e47a

Please sign in to comment.