Comparing version 0.9.8 to 0.9.9
@@ -106,2 +106,5 @@ /** | ||
use: true | ||
}, | ||
pngquant: { | ||
use: false | ||
} | ||
@@ -143,2 +146,5 @@ }; | ||
}, | ||
pngquant: { | ||
use: false | ||
}, | ||
include: [ constant.EXTENSION_JS, constant.EXTENSION_CSS, constant.REQUIRE_BEEZ_JS ], | ||
@@ -153,1 +159,6 @@ exclude: [] | ||
}; | ||
constant.PROG_OPTIPNG = 'optipng'; | ||
constant.PROG_JPEGOPTIM = 'jpegoptim'; | ||
constant.PROG_PNGQUANT = 'pngquant'; | ||
constant.PROG_CONVERT = 'convert'; |
@@ -59,4 +59,8 @@ /** | ||
options = options || ''; | ||
var proc = spawn('optipng', [options, filepath]); | ||
var args = _.compact([filepath].concat(options.replace(/\ \ /g, ' ').split(' '))); | ||
logger.debug('cmd:', constant.PROG_OPTIPNG, args.join(' ')); | ||
var proc = spawn(constant.PROG_OPTIPNG, args); | ||
proc.stdout.on('data', function (data) { | ||
@@ -97,3 +101,8 @@ logger.message(data.toString()); | ||
options = options || '--strip-all'; | ||
var proc = spawn('jpegoptim', [options, filepath]); | ||
var args = _.compact([filepath].concat(options.replace(/\ \ /g, ' ').split(' '))); | ||
logger.debug('cmd:', constant.PROG_PNGQUANT, args.join(' ')); | ||
var proc = spawn(constant.PROG_JPEGOPTIM, args); | ||
var err = ''; | ||
@@ -121,3 +130,48 @@ proc.stdin.pause(); | ||
/** | ||
* pngquant | ||
* | ||
* @name pngquant | ||
* @memberof image | ||
* @method | ||
* @param {String} filepath PNG file path. | ||
* @param {String} options pngquant options. | ||
* @param {function} callback | ||
* @example | ||
* var filepath = './test/image/logo.png'; | ||
*/ | ||
pngquant: function (filepath, options, callback) { | ||
if (typeof options === 'function') { | ||
callback = options; | ||
options = undefined; | ||
} | ||
options = options || '--ext .png -f -v'; // defualt) overwrite file. | ||
var args = _.compact([filepath].concat(options.replace(/\ \ /g, ' ').split(' '))); | ||
logger.debug('cmd:', constant.PROG_PNGQUANT, args.join(' ')); | ||
var proc = spawn(constant.PROG_PNGQUANT, args); | ||
var err = ''; | ||
proc.stdin.pause(); | ||
proc.stdout.on('data', function (data) { | ||
logger.message(data.toString()); | ||
}); | ||
proc.stderr.on('data', function (data) { | ||
err += data; | ||
logger.message(data.toString()); | ||
}); | ||
proc.addListener('exit', function (e) { | ||
proc.stdin.resume(); | ||
if (0 !== e) { | ||
return callback(e, err); | ||
} | ||
callback(); | ||
}); | ||
}, | ||
/** | ||
* | ||
* @param {String} filepath Image file path. | ||
@@ -170,5 +224,8 @@ * @param {Object} options | ||
* jpegoptim: { use: true, options: "--strip-all"} | ||
* pngquant: { use: true, options: "--ext .png -f -v"} | ||
* } | ||
*/ | ||
optim: function (path, options, callback) { | ||
var self = this; | ||
if (typeof options === 'function') { | ||
@@ -192,4 +249,22 @@ callback = options; | ||
logger.message('optipng:', path); | ||
callback && callback(null, path); | ||
if (!options.pngquant.use) { | ||
logger.debug('skip pngquant:', path); | ||
callback && callback(null, path); | ||
return; | ||
} | ||
self.pngquant(path, options.pngquant.options, function(err1) { // pngquant | ||
if (err1) { | ||
logger.error('pngquant error.', err1); | ||
callback && callback(err1); | ||
return; | ||
} | ||
logger.message('pngquant:', path); | ||
callback && callback(null, path); | ||
}); | ||
}); | ||
@@ -232,2 +307,3 @@ } else if (REG_JPEGOPTIM_FILE.test(path) && options.jpegoptim) { // jpegoptim | ||
* jpegoptim: { use: true, options: "--strip-all"} | ||
* pngquant: { use: true, options: "--ext .png -f -v"} | ||
* } | ||
@@ -234,0 +310,0 @@ */ |
{ | ||
"name": "beezlib", | ||
"version": "0.9.8", | ||
"description": "", | ||
"version": "0.9.9", | ||
"description": "The utility library for Node.js using beez projects.", | ||
"keywords": [ | ||
@@ -6,0 +6,0 @@ "beez", |
@@ -0,1 +1,2 @@ | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
@@ -10,2 +11,3 @@ var should = require('should'); | ||
describe('beezlib.image', function () { | ||
it('getSize/resize', function (done) { | ||
@@ -217,3 +219,32 @@ var src = 'test/image/logo.png'; | ||
it('pngquant success', function(done) { | ||
var filepath = "test/image/logo.png"; | ||
var options = ' --ext -test.png -v '; | ||
beezlib.image.pngquant(filepath, options, function(err, res) { | ||
if (err) { | ||
should.fail(err); | ||
} | ||
fs.unlinkSync("test/image/logo-test.png"); | ||
done(); | ||
}); | ||
}); | ||
it('pngquant failure', function(done) { | ||
var filepath = "test/image/logo.png"; | ||
var options = '--fail'; | ||
beezlib.image.pngquant(filepath, options, function(err, res) { | ||
if (err) { | ||
done(); | ||
} else { | ||
should.fail(err); | ||
done(); | ||
} | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3385
396580
10