Skip to content

Commit

Permalink
Update script and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
scriptex committed Apr 2, 2018
1 parent 2297483 commit 049ff3e
Show file tree
Hide file tree
Showing 7 changed files with 2,867 additions and 35 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,5 @@ typings/
# dotenv environment variables file
.env

# Misc
.DS_Store
Binary file added assets/dest/unicorn.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/src/unicorn.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
75 changes: 41 additions & 34 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
/**
* Internal dependencies
*/
const fs = require('fs');
const path = require('path');
const { join } = require('path');
const { readdirSync } = require('fs');

/**
* External dependencies
*/
const sharp = require('sharp');

/**
* Imagemin and its plugins
* Imagemin dependencies
*/
const imagemin = require('imagemin');
const imageminMozjpeg = require('imagemin-mozjpeg');
const imageminGifsicle = require('imagemin-gifsicle');
const imageminPNGquant = require('imagemin-pngquant');

const imageMinPlugins = [
/**
* Imagemin settings
*
* @type {Array}
*/
const plugins = [
imageminGifsicle({
interlaced: true
}),
Expand All @@ -28,7 +33,7 @@ const imageMinPlugins = [

imageminPNGquant({
speed: 1,
quality: 90
quality: 70
})
];

Expand All @@ -41,44 +46,46 @@ const imageMinPlugins = [
*/
const isImage = file => file.match(/\.(jpeg|jpg|gif|png)$/);

/**
* Resize an image using Sharp
*
* @param {Object} params
* @param {String} file
*
* @return {Promise}
*/
const resizeImage = (params, file) =>
sharp(join(params.src, file))
.resize(params.width, params.height)
.toFile(join(params.dest, file));

/**
* Optimize images in a folder
*
* @param {String} folder
*
* @return {Void}
*/
const optimizeImages = folder => {
imagemin([`${folder}/*.{jpeg,jpg,gif,png}`], folder, { plugins });
};

/**
* Resize images
*
* @param {Object} params Settings
*
* @return {Void}
* @return {Promise}
*/
const resize = params => {
const resize = async params => {
const { src, dest, width, height } = params;
const resized = [];

fs.readdir(src, (err, files) => {
/**
* Process all files
*/
files.filter(isImage).forEach(file => {
const srcPath = path.join(src, file);
const destPath = path.join(dest, file);
const files = (await Promise.all(await readdirSync(src)))
.filter(isImage)
.map(file => resizeImage(params, file));

/**
* Resize files and add result to promises array
*/
resized.push(
sharp(srcPath)
.resize(width, height)
.toFile(destPath)
);
});
optimizeImages(dest);

/**
* When all files are resized, optimize them
*/
Promise.all(resized).then(() => {
imagemin([`${dest}/*.{jpeg,jpg,gif,png}`], dest, {
plugins: imageMinPlugins
});
});
});
return Promise.all(files);
};

module.exports = resize;
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{
"name": "@three11/optisize",
"version": "0.1.0",
"version": "0.2.0",
"description": "Resize and optimize images in a folder with nodejs",
"main": "index.js",
"scripts": {
"test": "node test.js"
},
"bin": {
"optisize": "cli.js"
},
Expand Down Expand Up @@ -45,5 +48,9 @@
"imagemin-pngquant": "^5.1.0",
"sharp": "^0.20.1",
"yargs": "^11.0.0"
},
"devDependencies": {
"image-size": "^0.6.2",
"tape": "^4.9.0"
}
}
75 changes: 75 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* Internal dependencies
*/
const { resolve } = require('path');

/**
* External dependencies
*/
const sizeOf = require('image-size');

/**
* Test environment
*/
const src = resolve(__dirname, 'assets/src');
const dest = resolve(__dirname, 'assets/dest');

const tape = require('tape');
const resize = require('./');

const original = sizeOf('assets/src/unicorn.png');
const originalAspectRatio = original.width / original.height;

tape('dimensions should match', t => {
resize({
src,
dest
}).then(files => {
t.equal(original.width, files[0].width);
t.equal(original.height, files[0].height);
});

t.end();
});

tape('width should be 500', t => {
resize({
src,
dest,
width: 500
}).then(files => {
const { width, height } = files[0];

t.equal(500, width);
});

t.end();
});

tape('should keep aspect ratio', t => {
resize({
src,
dest,
width: 500
}).then(files => {
const { width, height } = files[0];

t.equal(originalAspectRatio, width / height);
});

t.end();
});

tape('height should be 200', t => {
resize({
src,
dest,
height: 200
}).then(files => {
const { width, height } = files[0];

t.equal(200, height);
});

t.end();
});
Loading

0 comments on commit 049ff3e

Please sign in to comment.