Added vinyl and tslib

parent bc74c620
Pipeline #43 failed with stages
......@@ -9,12 +9,15 @@
"private": false,
"devDependencies": {
"@types/imagemin": "^6.0.0",
"@types/vinyl": "^2.0.2",
"colors": "latest",
"imagemin": "^6.0.0",
"imagemin-jpegtran": "^5.0.2",
"imagemin-pngquant": "^6.0.0",
"imagemin-webp": "^4.1.0",
"typescript": "^3.1.5"
"tslib": "^1.9.3",
"typescript": "^3.1.5",
"vinyl": "^2.2.0"
},
"dependencies": {
"@types/filesize": "^3.6.0",
......
import * as sharp from "sharp";
import path from "path";
import sharp from "sharp";
import {consoleError, consoleInfo, consoleSpecial, consoleSuccess} from "./Helper";
import {OutputInfo} from "sharp";
import * as filesize from "filesize";
import Vinyl from "vinyl";
import filesize from "filesize";
const fileExt = require("file-extension");
......@@ -11,45 +14,63 @@ const outputPath: string = "./img/output/";
const inputFile: string = `${inputPath}lena.png`;
const image: sharp.Sharp = sharp(inputFile, {failOnError: true});
let fileSizes: { original: number, jpeg: number, webp: number } = {original: 0, jpeg: 0, webp: 0};
//Read input file metadata
const inputFileFormat: string = fileExt(inputFile);
if (inputFileFormat == ("jpeg" || "jpg")) {
readOriginalImage(image.clone()).then(() => {
consoleSuccess("Original image inspected!");
}).then(() => {
convertImage("WEBP", image.clone()).then(() => {
consoleSuccess("Operation completed!");
consoleSuccess(filesize(fileSizes.original - fileSizes.webp));
});
});
}
export class ImageConverter {
if (inputFileFormat == "png") {
consoleInfo("PNG file detected");
readOriginalImage(image.clone()).then(() => {
consoleSuccess("Original image inspected!");
}).then(() => {
convertImage("JPEG", image.clone()).then(() => {
consoleSuccess("Operation completed!");
consoleSuccess(filesize(fileSizes.original - fileSizes.webp));
});
}).then(() => {
convertImage("WEBP", image.clone()).then(() => {
consoleSuccess("Operation completed!");
consoleSuccess(filesize(fileSizes.original - fileSizes.webp));
});
});
}
private filename: string = "";
private inputPath: string = "";
private outputPath: string = "";
//If PNG convert to jpeg
constructor(inputFile: string, outputPath: string) {
//Convert to WebP
const file: Vinyl = new Vinyl({
base: path.dirname(inputFile),
path: path.basename(inputFile),
ext: path.extname(inputFile)
});
const image: sharp.Sharp = sharp(inputFile, {failOnError: true});
this.inputPath = file.dirname;
this.filename = file.base;
switch (fileExt(file.path)) {
case "jpeg":
case "jpg":
consoleInfo("JPEG file detected");
readOriginalImage(image.clone()).then(() => {
consoleSuccess("Original image inspected!");
}).then(() => {
convertImage("WEBP", image.clone()).then(() => {
consoleSuccess("Operation completed!");
consoleSuccess(filesize(fileSizes.original - fileSizes.webp));
});
});
break;
case "png":
consoleInfo("PNG file detected");
readOriginalImage(image.clone()).then(() => {
consoleSuccess("Original image inspected!");
}).then(() => {
convertImage("JPEG", image.clone()).then(() => {
consoleSuccess("Operation completed!");
consoleSuccess(filesize(fileSizes.original - fileSizes.webp));
});
}).then(() => {
convertImage("WEBP", image.clone()).then(() => {
consoleSuccess("Operation completed!");
consoleSuccess(filesize(fileSizes.original - fileSizes.webp));
});
});
break;
default:
throw Error("Unknown file extension!");
}
}
}
async function readOriginalImage(img: sharp.Sharp): Promise<void> {
......@@ -66,7 +87,7 @@ async function readOriginalImage(img: sharp.Sharp): Promise<void> {
}
async function convertImage(format: "WEBP" | "JPEG", image: sharp.Sharp): Promise<void> {
consoleSpecial("Converting started!");
consoleSpecial(`${format} Converting started!`);
switch (format) {
case "JPEG":
......@@ -88,8 +109,6 @@ async function convertImage(format: "WEBP" | "JPEG", image: sharp.Sharp): Promis
nearLossless: false
})
.toFile(`${outputPath}lena.webp`).then(info => {
consoleInfo(`Image size: ${info.width}x${info.height}`);
fileSizes.webp = info.size;
consoleInfo(`File size: ${filesize(fileSizes.webp)}`);
}).catch(err => {
......@@ -100,10 +119,9 @@ async function convertImage(format: "WEBP" | "JPEG", image: sharp.Sharp): Promis
async function convertJPEG(img: sharp.Sharp): Promise<void> {
await img.metadata()
.then( (): Promise<void> => {
.then((): Promise<void> => {
return img
.jpeg({
})
.jpeg({})
.toFile(`${outputPath}lena.jpeg`).then(info => {
consoleInfo(`Image size: ${info.width}x${info.height}`);
......@@ -114,4 +132,6 @@ async function convertImage(format: "WEBP" | "JPEG", image: sharp.Sharp): Promis
});
});
}
}
\ No newline at end of file
}
new ImageConverter(inputFile, outputPath);
\ No newline at end of file
{
"compilerOptions": {
"charset": "utf8",
"module": "commonjs",
"moduleResolution": "node",
"target": "es2018",
"sourceMap": true,
"strict": true,
"noEmitOnError": true,
"outDir": "./dist",
"baseUrl": ".",
"esModuleInterop": true,
"importHelpers": true,
"typeRoots": [
"./node_modules/@types"
],
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment