Commit 565f305d authored by Philipp Nowinski's avatar Philipp Nowinski
Browse files

[FEATURE] complete images task

parent cc84b82d
......@@ -17,10 +17,40 @@ module.exports = class Watch extends Task {
/**
* Runner function
*/
async run() {
async run(_subTask) {
if (_subTask === 'uploaded') {
this._optimizeUploadedImages();
} else {
this._optimizeExtensionImages();
}
}
/**
* Optimize all user uploaded images
*/
async _optimizeUploadedImages() {
this._config.images.optimize.forEach(async imagesPath => {
const files = await globby([`${imagesPath}/**/*.{png,jpg,gif,svg}`]);
this._optimize(files, imagesPath);
});
}
/**
* Optimize images on extension level
*/
async _optimizeExtensionImages() {
const imagesPath = this._getFullPath(this._config.directories.images);
const files = await globby([`${imagesPath}/**/*.{png,jpg,gif,svg}`]);
files.forEach(async _image => {
this._optimize(files, imagesPath);
}
/**
* Optimize a set of given images
*
* @param {Array} _files An array of filenames
*/
async _optimize(_files, _imagesPath) {
_files.forEach(async _image => {
let buffer = fs.readFileSync(_image);
let result = await imagemin.buffer(buffer, {
plugins: [
......@@ -37,7 +67,7 @@ module.exports = class Watch extends Task {
let saved = buffer.length - result.length;
info = `(saved ${prettyBytes(saved)} ~ ${Math.round((saved / buffer.length) * 100)}%)`;
}
let message = `${chalk.white(_image.replace(`${imagesPath}/`, ''))} ${chalk.magenta(`${info}`)}`;
let message = `${chalk.white(_image.replace(`${_imagesPath}/`, ''))} ${chalk.magenta(`${info}`)}`;
fs.writeFileSync(_image, result);
this._logger.success(message);
});
......
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