imagemin
Advanced tools
Comparing version 1.0.5 to 2.0.0
43
cli.js
@@ -95,23 +95,21 @@ #!/usr/bin/env node | ||
* | ||
* @param {Buffer} input | ||
* @param {Object} opts | ||
* @param {Array|Buffer|String} src | ||
* @param {String} dest | ||
* @api private | ||
*/ | ||
function run(input, opt) { | ||
function run(src, dest) { | ||
var imagemin = new Imagemin() | ||
.src(input) | ||
.src(src) | ||
.use(Imagemin.gifsicle(opts)) | ||
.use(Imagemin.jpegtran(opts)) | ||
.use(Imagemin.optipng(opts)) | ||
.use(Imagemin.pngquant(opts)) | ||
.use(Imagemin.svgo()); | ||
if (process.stdout.isTTY) { | ||
var name = path.basename(opt.input); | ||
var out = path.join(opt.output ? opt.output : 'build', name); | ||
imagemin.dest(path.join(out)); | ||
imagemin.dest(dest ? dest : 'build'); | ||
} | ||
imagemin.optimize(function (err, file) { | ||
imagemin.run(function (err, files) { | ||
if (err) { | ||
@@ -123,3 +121,5 @@ console.error(err); | ||
if (!process.stdout.isTTY) { | ||
process.stdout.write(file.contents); | ||
files.forEach(function (file) { | ||
process.stdout.write(file.contents); | ||
}); | ||
} | ||
@@ -134,6 +134,6 @@ }); | ||
if (process.stdin.isTTY) { | ||
var input = opts.argv.remain; | ||
var output; | ||
var src = opts.argv.remain; | ||
var dest; | ||
if (!input.length) { | ||
if (!src.length) { | ||
help(); | ||
@@ -143,19 +143,10 @@ return; | ||
if (!isFile(input[input.length - 1])) { | ||
output = input[input.length - 1]; | ||
input.pop(); | ||
if (!isFile(src[src.length - 1])) { | ||
dest = src[src.length - 1]; | ||
src.pop(); | ||
} | ||
input.forEach(function (file) { | ||
fs.readFile(file, function (err, data) { | ||
if (err) { | ||
console.error(err); | ||
process.exit(1); | ||
} | ||
run(data, { input: file, output: output }); | ||
}); | ||
}); | ||
run(src, dest); | ||
} else { | ||
stdin.buffer(run); | ||
} |
163
index.js
'use strict'; | ||
var fs = require('fs-extra'); | ||
var mode = require('stat-mode'); | ||
var Ware = require('ware'); | ||
var combine = require('stream-combiner'); | ||
var concat = require('concat-stream'); | ||
var File = require('vinyl'); | ||
var fs = require('vinyl-fs'); | ||
var through = require('through2'); | ||
@@ -18,14 +20,18 @@ /** | ||
this.ware = new Ware(); | ||
this.streams = []; | ||
} | ||
/** | ||
* Add a plugin to the middleware stack | ||
* Get or set the source files | ||
* | ||
* @param {Function} plugin | ||
* @param {Array|Buffer|String} file | ||
* @api public | ||
*/ | ||
Imagemin.prototype.use = function (plugin) { | ||
this.ware.use(plugin); | ||
Imagemin.prototype.src = function (file) { | ||
if (!arguments.length) { | ||
return this._src; | ||
} | ||
this._src = file; | ||
return this; | ||
@@ -35,14 +41,14 @@ }; | ||
/** | ||
* Get or set the source file | ||
* Get or set the destination folder | ||
* | ||
* @param {String|Buffer} file | ||
* @param {String} dir | ||
* @api public | ||
*/ | ||
Imagemin.prototype.src = function (file) { | ||
Imagemin.prototype.dest = function (dir) { | ||
if (!arguments.length) { | ||
return this._src; | ||
return this._dest; | ||
} | ||
this._src = file; | ||
this._dest = dir; | ||
return this; | ||
@@ -52,14 +58,10 @@ }; | ||
/** | ||
* Get or set the destination file | ||
* Add a plugin to the middleware stack | ||
* | ||
* @param {String} file | ||
* @param {Function} plugin | ||
* @api public | ||
*/ | ||
Imagemin.prototype.dest = function (file) { | ||
if (!arguments.length) { | ||
return this._dest; | ||
} | ||
this._dest = file; | ||
Imagemin.prototype.use = function (plugin) { | ||
this.streams.push(plugin); | ||
return this; | ||
@@ -69,3 +71,3 @@ }; | ||
/** | ||
* Optimize file | ||
* Optimize files | ||
* | ||
@@ -76,51 +78,21 @@ * @param {Function} cb | ||
Imagemin.prototype.optimize = function (cb) { | ||
Imagemin.prototype.run = function (cb) { | ||
cb = cb || function () {}; | ||
var self = this; | ||
this.streams.unshift(this.read(this.src())); | ||
this._read(function (err, file) { | ||
if (err) { | ||
cb(err); | ||
return; | ||
} | ||
if (this.dest()) { | ||
this.streams.push(fs.dest(this.dest())); | ||
} | ||
if (!file || !file.contents) { | ||
cb(); | ||
return; | ||
} | ||
var pipe = combine(this.streams); | ||
var end = concat(function (files) { | ||
cb(null, files); | ||
}); | ||
var buf = file.contents; | ||
self._run(file, function (err, file) { | ||
if (err) { | ||
cb(err); | ||
return; | ||
} | ||
if (file.contents.length >= buf.length) { | ||
file.contents = buf; | ||
} | ||
self._write(file, function (err) { | ||
if (err) { | ||
cb(err); | ||
return; | ||
} | ||
cb(null, file); | ||
}); | ||
}); | ||
pipe.on('error', function (err) { | ||
cb(err); | ||
return; | ||
}); | ||
}; | ||
/** | ||
* Run a file through the middleware | ||
* | ||
* @param {Object} file | ||
* @param {Function} cb | ||
* @api private | ||
*/ | ||
Imagemin.prototype._run = function (file, cb) { | ||
this.ware.run(file, this, cb); | ||
pipe.pipe(end); | ||
}; | ||
@@ -131,65 +103,20 @@ | ||
* | ||
* @param {Function} cb | ||
* @param {Array|Buffer|String} src | ||
* @api private | ||
*/ | ||
Imagemin.prototype._read = function (cb) { | ||
var file = {}; | ||
var src = this.src(); | ||
Imagemin.prototype.read = function (src) { | ||
if (Buffer.isBuffer(src)) { | ||
file.contents = src; | ||
cb(null, file); | ||
return; | ||
} | ||
fs.stat(src, function (err, stats) { | ||
if (err) { | ||
cb(err); | ||
return; | ||
} | ||
if (!stats.isFile()) { | ||
cb(); | ||
return; | ||
} | ||
fs.readFile(src, function (err, buf) { | ||
if (err) { | ||
cb(err); | ||
return; | ||
} | ||
file.contents = buf; | ||
file.mode = mode(stats).toOctal(); | ||
var stream = through.obj(function (file, enc, cb) { | ||
cb(null, file); | ||
}); | ||
}); | ||
}; | ||
/** | ||
* Write file to destination | ||
* | ||
* @param {Object} file | ||
* @param {Function} cb | ||
* @api private | ||
*/ | ||
stream.end(new File({ | ||
contents: src | ||
})); | ||
Imagemin.prototype._write = function (file, cb) { | ||
var dest = this.dest(); | ||
if (!dest) { | ||
cb(); | ||
return; | ||
return stream; | ||
} | ||
fs.outputFile(dest, file.contents, function (err) { | ||
if (err) { | ||
cb(err); | ||
return; | ||
} | ||
cb(); | ||
}); | ||
return fs.src(src); | ||
}; | ||
@@ -196,0 +123,0 @@ |
{ | ||
"name": "imagemin", | ||
"version": "1.0.5", | ||
"version": "2.0.0", | ||
"description": "Minify images", | ||
@@ -35,19 +35,20 @@ "license": "MIT", | ||
"dependencies": { | ||
"fs-extra": "^0.11.0", | ||
"concat-stream": "^1.4.6", | ||
"get-stdin": "^3.0.0", | ||
"nopt": "^3.0.1", | ||
"stat-mode": "^0.2.0", | ||
"tempfile": "^1.0.0", | ||
"ware": "^0.3.0" | ||
"stream-combiner": "^0.2.1", | ||
"through2": "^0.6.1", | ||
"vinyl": "^0.4.3", | ||
"vinyl-fs": "^0.3.7" | ||
}, | ||
"devDependencies": { | ||
"ava": "0.0.4" | ||
"ava": "^0.0.4" | ||
}, | ||
"optionalDependencies": { | ||
"imagemin-gifsicle": "^1.0.0", | ||
"imagemin-jpegtran": "^1.0.0", | ||
"imagemin-optipng": "^1.0.0", | ||
"imagemin-pngquant": "^1.0.1", | ||
"imagemin-svgo": "^1.0.2" | ||
"imagemin-gifsicle": "^2.0.0", | ||
"imagemin-jpegtran": "^2.0.0", | ||
"imagemin-optipng": "^2.0.0", | ||
"imagemin-pngquant": "^2.0.0", | ||
"imagemin-svgo": "^2.0.0" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# imagemin [![Build Status](https://travis-ci.org/imagemin/imagemin.svg?branch=master)](https://travis-ci.org/imagemin/imagemin) | ||
# imagemin [![Build Status](http://img.shields.io/travis/imagemin/imagemin.svg?style=flat)](https://travis-ci.org/imagemin/imagemin) [![Build status](https://ci.appveyor.com/api/projects/status/wlnem7wef63k4n1t)](https://ci.appveyor.com/project/ShinnosukeWatanabe/imagemin) | ||
@@ -19,7 +19,7 @@ > Minify images seamlessly with Node.js | ||
var imagemin = new Imagemin() | ||
.src('foo.jpg') | ||
.dest('foo-optimized.jpg') | ||
.src('images/*.{gif,jpg,png,svg}') | ||
.dest('build/images') | ||
.use(Imagemin.jpegtran({ progressive: true })); | ||
imagemin.optimize(function (err, file) { | ||
imagemin.run(function (err, files) { | ||
if (err) { | ||
@@ -29,4 +29,4 @@ throw err; | ||
console.log(file); | ||
// => { contents: <Buffer 89 50 4e ...>, mode: '0644' } | ||
console.log(files[0]); | ||
// => { contents: <Buffer 89 50 4e ...> } | ||
}); | ||
@@ -44,8 +44,9 @@ ``` | ||
Set the file to be optimized. Can be a `Buffer` or the path to a file. | ||
Set the files to be optimized. Takes a buffer, glob string or an array of glob strings | ||
as argument. | ||
### .dest(file) | ||
### .dest(folder) | ||
Set the destination to where your file will be written. If you don't set any destination | ||
the file won't be written. | ||
Set the destination folder to where your file will be written. If you don't set | ||
any destination no files will be written. | ||
@@ -56,5 +57,5 @@ ### .use(plugin) | ||
### .optimize(cb) | ||
### .run(cb) | ||
Optimize your file with the given settings. | ||
Optimize your files with the given settings. | ||
@@ -61,0 +62,0 @@ ## Plugins |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
160
2
8704
12
228
+ Addedconcat-stream@^1.4.6
+ Addedstream-combiner@^0.2.1
+ Addedthrough2@^0.6.1
+ Addedvinyl@^0.4.3
+ Addedvinyl-fs@^0.3.7
+ Addedimagemin-gifsicle@2.0.1(transitive)
+ Addedimagemin-jpegtran@2.0.0(transitive)
+ Addedimagemin-optipng@2.0.1(transitive)
+ Addedimagemin-pngquant@2.0.1(transitive)
+ Addedimagemin-svgo@2.1.0(transitive)
- Removedfs-extra@^0.11.0
- Removedstat-mode@^0.2.0
- Removedtempfile@^1.0.0
- Removedware@^0.3.0
- Removedfs-extra@0.11.1(transitive)
- Removedimagemin-gifsicle@1.0.0(transitive)
- Removedimagemin-jpegtran@1.0.0(transitive)
- Removedimagemin-optipng@1.0.0(transitive)
- Removedimagemin-pngquant@1.0.2(transitive)
- Removedimagemin-svgo@1.0.2(transitive)
- Removedjsonfile@2.4.0(transitive)
- Removedncp@0.6.0(transitive)
- Removedware@0.3.0(transitive)