asset-converter
Advanced tools
Comparing version 1.0.4 to 1.0.5
@@ -9,3 +9,3 @@ #!/usr/bin/env node | ||
import {convertPng, optiPng, reducePngQuality, convertJpeg, optiJpeg, reduceJpegQuality,addMetadata, convertToWebp} from './convert.js' | ||
import {removeIfHeavier, isHeavier, get_file_info, getFileType, color, checkMetadata, checkIfAllImgExist} from './utils.js' | ||
import {removeIfHeavier, isHeavier, get_file_info, getFileType, color, checkMetadata, checkIfAllImgExist, isImage, isOptimized, destroyAllLowDpiFile} from './utils.js' | ||
import {check_flags, get_flags, displayHelp} from './cli.js' | ||
@@ -16,2 +16,4 @@ | ||
const createDirectoryAndFiles = async (filePath, newDirName, fileType, ext, flags) => { | ||
@@ -62,10 +64,7 @@ let newDirPath = path.join(filePath.substr(0, filePath.lastIndexOf('/')), newDirName) | ||
const convertAsset = async (fileInfo, files, flags) => { | ||
if ((fileInfo.ext === "jpeg" || fileInfo.ext === "jpg" || fileInfo.ext === "png") | ||
&& (fileInfo.fileType === "image/png" || fileInfo.fileType === "image/jpeg")) { | ||
if (isImage(fileInfo)) { | ||
let newDirName = fileInfo.fileName.substr(0, fileInfo.fileName.lastIndexOf('.')) | ||
let parentDir = path.basename(path.dirname(fileInfo.filePath)) | ||
if ((await checkMetadata(fileInfo.filePath,) === true) && (flags.f === false)){ | ||
console.log(color.orange, "File already optimize:", fileInfo.filePath); | ||
if (await isOptimized(fileInfo, flags)) | ||
return ; | ||
} | ||
let res; | ||
@@ -85,9 +84,6 @@ if (newDirName !== parentDir && newDirName !== parentDir + "-lowdpi") { | ||
let newDirPath = fileInfo.filePath.substr(0, fileInfo.filePath.lastIndexOf('/')) | ||
if ((fileInfo.ext === "jpeg" || fileInfo.ext === "jpg" || fileInfo.ext === "png") | ||
&& (fileInfo.fileType === "image/png" || fileInfo.fileType === "image/jpeg")) { | ||
if (isImage(fileInfo)) { | ||
if (fileInfo.fileType === "image/png") { | ||
if ((await checkMetadata(fileInfo.filePath,) === true) && flags.f === false){ | ||
console.log(color.orange, "File already optimize:", fileInfo.filePath); | ||
if (await isOptimized(fileInfo, flags)) | ||
return ; | ||
} | ||
let destinationStream = await optiPng(fileInfo.filePath, fileInfo.filePath + ".tmp") | ||
@@ -99,6 +95,4 @@ await new Promise((resolve) => { | ||
else if (fileInfo.fileType === "image/jpeg") { | ||
if ( (await checkMetadata(fileInfo.filePath,) === true) && (flags.f === false)){ | ||
console.log(color.orange, "File already optimize:", fileInfo.filePath); | ||
if (await isOptimized(fileInfo, flags)) | ||
return ; | ||
} | ||
await optiJpeg(fileInfo.filePath, fileInfo.filePath + ".tmp") | ||
@@ -113,13 +107,11 @@ } | ||
const updateAsset = async (fileInfo, flags) => { | ||
if ((fileInfo.ext === "jpeg" || fileInfo.ext === "jpg" || fileInfo.ext === "png") | ||
&& (fileInfo.fileType === "image/png" || fileInfo.fileType === "image/jpeg")){ | ||
if (isImage(fileInfo)){ | ||
if ((fileInfo.fileName).search("-lowdpi") !== -1) | ||
return; | ||
if ( (await checkMetadata(fileInfo.filePath,) === true) && (flags.f === false)){ | ||
console.log(color.orange, "File already optimize:", fileInfo.filePath); | ||
if ((await isOptimized(fileInfo, flags))) | ||
return ; | ||
} | ||
try { | ||
const newDirPath = path.dirname(fileInfo.filePath) | ||
if (fileInfo.fileType === "image/png"){ | ||
await destroyAllLowDpiFile(fileInfo, newDirPath); | ||
await reducePngQuality(fileInfo.filePath, newDirPath) | ||
@@ -131,3 +123,2 @@ await convertToWebp(fileInfo.filePath, newDirPath) | ||
}); | ||
fs.unlinkSync(fileInfo.filePath) | ||
@@ -138,2 +129,3 @@ fs.renameSync(fileInfo.filePath + ".tmp", fileInfo.filePath) | ||
else if (fileInfo.fileType == "image/jpeg"){ | ||
await destroyAllLowDpiFile(fileInfo, newDirPath); | ||
let lowdpiFileName = path.basename(newDirPath) + "-lowdpi." + fileInfo.ext; | ||
@@ -145,3 +137,7 @@ await reduceJpegQuality(fileInfo.filePath, newDirPath, lowdpiFileName); | ||
fs.renameSync(fileInfo.filePath + ".tmp", fileInfo.filePath) | ||
await addMetadata(fileInfo.filePath, "opti"); | ||
} | ||
console.log(color.green, "File optimized:", fileInfo.filePath) | ||
console.log(color.green, "File created or replaced:", newDirPath + '/' + path.basename(newDirPath) + "-lowdpi." + fileInfo.ext) | ||
console.log(color.green, "File created or replaced:", newDirPath + '/' + path.basename(newDirPath) + "webp"); | ||
} | ||
@@ -148,0 +144,0 @@ catch (e) { |
{ | ||
"name": "asset-converter", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "Boost your web performance by converting and optimizing image assets with this robust Node.js tool.", | ||
@@ -5,0 +5,0 @@ "main": "replace_asset.js", |
@@ -114,4 +114,4 @@ # Asset Converter | ||
- [Augustin Lorain](https://github.com/GusFiveO) - [LinkedIn](https://www.linkedin.com/in/augustin-lorain-b45b9a264/) | ||
- [Khelif Raphael](https://github.com/raphifou15) - [LinkedIn](https://www.linkedin.com/in/rapha%C3%ABl-khelif-a93727112/) | ||
- [Raphael Khelif](https://github.com/raphifou15) - [LinkedIn](https://www.linkedin.com/in/rapha%C3%ABl-khelif-a93727112/) | ||
We also want to thank the open-source community for their contributions and support. |
32
utils.js
@@ -94,1 +94,33 @@ import path from 'path' | ||
export const isImage = (fileInfo) => { | ||
if ((fileInfo.ext === "jpeg" || fileInfo.ext === "jpg" || fileInfo.ext === "png") | ||
&& (fileInfo.fileType === "image/png" || fileInfo.fileType === "image/jpeg")) | ||
return true; | ||
return false; | ||
} | ||
export const isOptimized = async(fileInfo, flags) => { | ||
if ((await checkMetadata(fileInfo.filePath,) === true) && (flags.f === false)){ | ||
console.log(color.orange, "File already optimize:", fileInfo.filePath); | ||
return true; | ||
} | ||
return false; | ||
} | ||
export const destroyAllLowDpiFile = async (fileInfo, newDirPath) =>{ | ||
const parentPath = path.dirname(fileInfo.filePath) | ||
try{ | ||
await fs.promises.access(parentPath + '/' + path.basename(newDirPath) + "-lowdpi." + "png", fs.constants.F_OK) | ||
fs.unlinkSync(path.resolve(parentPath + '/' + path.basename(newDirPath) + "-lowdpi." + "png")) | ||
}catch(e){;} | ||
try{ | ||
await fs.promises.access(parentPath + '/' + path.basename(newDirPath) + "-lowdpi." + "jpeg", fs.constants.F_OK); | ||
fs.unlinkSync(path.resolve(parentPath + '/' + path.basename(newDirPath) + "-lowdpi." + "jpeg")) | ||
}catch(e){;} | ||
try{ | ||
await fs.promises.access(parentPath + '/' + path.basename(newDirPath) + "-lowdpi." + "jpg", fs.constants.F_OK) | ||
fs.unlinkSync(path.resolve(parentPath + '/' + path.basename(newDirPath) + "-lowdpi." + "jpg")) | ||
}catch(e){;} | ||
} |
471
21556
6