-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |