Skip to content

Instantly share code, notes, and snippets.

@yaronuliel
Created March 29, 2016 16:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yaronuliel/2f076e49f92998893f1b4004ebf9de6e to your computer and use it in GitHub Desktop.
Save yaronuliel/2f076e49f92998893f1b4004ebf9de6e to your computer and use it in GitHub Desktop.
#!/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

#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