Skip to content

Instantly share code, notes, and snippets.

@yaronuliel
Created Mar 29, 2016
Embed
What would you like to do?
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
function error(message) {
console.error(message);
process.exit();
}
if (process.argv.length != 4) {
error("Use: shrink FROM_PATH TO_PATH");
}
var from = path.resolve(process.argv[2]);
var to = path.resolve(process.argv[3]);
if(!fs.existsSync(from) || !fs.lstatSync(from).isDirectory()) {
error(process.argv[2] + " is not a valid directory");
}
if(!fs.existsSync(to) || !fs.lstatSync(to).isDirectory()) {
error(process.argv[3] + " is not a valid directory");
}
const gulp = require('gulp');
const imagemin = require('gulp-imagemin');
const pngquant = require('imagemin-pngquant'); // $ npm i -D imagemin-pngquant
gulp.task('default', function(){
return gulp.src([from + '/**/*.png',from + '/**/*.jpg',from + '/**/*.jpeg',from + '/**/*.gif'])
.pipe(imagemin({
progressive: true,
svgoPlugins: [
{removeViewBox: false},
{cleanupIDs: false}
],
use: [pngquant()]
}))
.pipe(gulp.dest(to));
});
gulp.start('default');
console.log('Starting to minify png and jpg from ' + from);
@yaronuliel
Copy link
Author

yaronuliel commented Mar 29, 2016

#Install dependencies

npm install gulp
npm install gulp-imagemin
npm install imagemin-pngquant

#give it execute permission
chmod +x ./shrink

#run
./shrink IMAGES_DIR OUTPUT_DIR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment