From fbd553361511de940878a9e7760b673b2b58a91f Mon Sep 17 00:00:00 2001 From: yhsunshining Date: Fri, 18 Sep 2015 11:24:02 +0800 Subject: [PATCH 1/4] add dir recursion; add relative path --- tinypng.js | 49 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/tinypng.js b/tinypng.js index ebc1c93..3fb9ce2 100755 --- a/tinypng.js +++ b/tinypng.js @@ -4,8 +4,15 @@ var fs = require('fs'), https = require('https'), colors = require('colors'), pjson = require('./package.json'); +var path = require("path"); -var options, options_path = __dirname + '/settings.json', files, files_io = [], file_count = 0; +var options, + options_path = __dirname + '/settings.json', + files=[], + files_io = [], + file_count = 0, + regPng = /\.png/i, + regAll = /\.jpg|\.jpeg|\.png/i; /** * Get default options from settings file @@ -110,28 +117,39 @@ var parseArgvs = function() { } checkArg(); - filterFiles(); + filterFiles(files); return true; }; /** * Filter selected files from directories and wrong file extentions - * - * @returns {object} + * @param {Array} files + * @returns {Array} */ -var filterFiles = function() { - for(var i = 0, l = files.length; i < l; i++) { - var file = files[i]; - if(fs.existsSync(file) && fs.statSync(file).isFile()) { - if ((!options.allow_nonpng && file.slice(-4) === '.png' ) || options.allow_nonpng) { - var pair = [ file, file ]; - if(!options.allow_rewrite) { - pair[1] = postfixedName(file); +var filterFiles = function(files) { + for(var i = 0, l = files.length; i < l; i++) { + var file = path.resolve(files[i] || ""); + if(fs.existsSync(file)) { + if(fs.statSync(file).isFile()){ + if ((!options.allow_nonpng && file.match(regPng) ) || (options.allow_nonpng && file.match(regAll))) { + var pair = [ file, file ]; + + if(!options.allow_rewrite) { + pair[1] = postfixedName(file); + } + + files_io.push(pair); } - - files_io.push(pair); + } + else if(fs.statSync(file).isDirectory()){ + var items = fs.readdirSync(file); + var dir=[]; + for(var i=0,len=items.length;i 0) { makeTiny(); } From cefa3bcf5c1dd8d981e0450ac8cf1badeeeffbf5 Mon Sep 17 00:00:00 2001 From: yhsunshining Date: Fri, 18 Sep 2015 11:40:05 +0800 Subject: [PATCH 2/4] async upload and download --- tinypng.js | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/tinypng.js b/tinypng.js index 3fb9ce2..efdc296 100755 --- a/tinypng.js +++ b/tinypng.js @@ -249,19 +249,20 @@ var exit = function(code) { /** * Compress and save image + * @param {number} offset * @returns {*} */ -var makeTiny = function() { - var pair = files_io[file_count]; +var makeTiny = function(offset) { + var pair = files_io[offset]; - if(!pair) { - return logMessage('Compression complete!'); - } +// if(!pair) { +// return logMessage('Compression complete!'); +// } var input = pair[0]; var output = pair[1]; - process.stdout.write(input + " → ".grey); +// process.stdout.write(input + " → ".grey); var req_options = require("url").parse("https://api.tinypng.com/shrink"); req_options.auth = "api:" + options.api_key; @@ -274,10 +275,10 @@ var makeTiny = function() { if(d.error) { process.stdout.write("error".red + "\n"); - logError(d.error + ": " + d.message); - exit(1); + logError(input + " → ".grey+d.error + ": " + d.message); + //exit(1); } else { - process.stdout.write("-" + ((1 - d.output.ratio) * 100).toFixed(1) + "%" + " → ".grey); +// process.stdout.write("-" + ((1 - d.output.ratio) * 100).toFixed(1) + "%" + " → ".grey); } }); @@ -285,9 +286,9 @@ var makeTiny = function() { https.get(res.headers.location, function(res) { res.pipe(fs.createWriteStream(output)); - process.stdout.write(output.yellow + "\n"); + process.stdout.write(output.yellow + " success".green + "\n"); file_count++; - makeTiny(); +// makeTiny(); }); } @@ -304,10 +305,19 @@ var run = function() { getOptions(); parseArgvs(); checkApiKey(); - if(files_io.length > 0) { - makeTiny(); + console.log("start waiting··· " ) + } + for(var i=0,len=files_io.length;i=len) { + logMessage('Compression complete!') + break; + } } + //if(files_io.length > 0) { + // makeTiny(); + //} }; run(); From 893529ea9959b5db060d1f7218a3c3fc59682a6b Mon Sep 17 00:00:00 2001 From: yhsunshining Date: Fri, 18 Sep 2015 17:35:29 +0800 Subject: [PATCH 3/4] file null --- tinypng.js | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/tinypng.js b/tinypng.js index efdc296..ec27e6c 100755 --- a/tinypng.js +++ b/tinypng.js @@ -1,4 +1,4 @@ -#!/usr/bin/env node +//#!/usr/bin/env node var fs = require('fs'), https = require('https'), @@ -8,7 +8,7 @@ var path = require("path"); var options, options_path = __dirname + '/settings.json', - files=[], + files, files_io = [], file_count = 0, regPng = /\.png/i, @@ -103,7 +103,6 @@ var parseArgvs = function() { files = argvs; break; } - if(!files) { checkArg(); } @@ -130,6 +129,7 @@ var parseArgvs = function() { var filterFiles = function(files) { for(var i = 0, l = files.length; i < l; i++) { + var file = path.resolve(files[i] || ""); if(fs.existsSync(file)) { if(fs.statSync(file).isFile()){ @@ -199,12 +199,12 @@ var logHelp = function() { ( options.api_key === "" ? "Warning! API key is not defined.".yellow : "Current API key: " + options.api_key ) + "\n" + "\n" + "Options:\n" + - " -k, --api-key \tSet default TinyPNG API key.\n" + + " -k, --api-key\t\tSet default TinyPNG API key.\n" + " -r, --allow-rewrite\tRewrite the original files with compressed data.\n" + " -n, --allow-nonpng \tAllow you to compress files without .png extention.\n" + - " -p, --postfix \tPostfix for compressed files when rewriting disabled.\n" + - " -h, --help \tThis message.\n" + - " -v, --version \tShow version." + + " -p, --postfix\t\tPostfix for compressed files when rewriting disabled.\n" + + " -h, --help\t\tThis message.\n" + + " -v, --version\t\tShow version." + "\n"; console.log(message); @@ -223,7 +223,7 @@ var logVersion = function() { * @param {*} message */ var logError = function(message) { - console.error('>_<'.red, message); + console.error('>_<'.red, message.red); }; /** @@ -232,7 +232,7 @@ var logError = function(message) { * @param {*} message */ var logMessage = function(message) { - console.log('*Ü*'.green, message); + console.log('*Ü*'.green, message.green); }; /** @@ -272,7 +272,6 @@ var makeTiny = function(offset) { res.on('data', function(d) { d = JSON.parse(d); - if(d.error) { process.stdout.write("error".red + "\n"); logError(input + " → ".grey+d.error + ": " + d.message); @@ -284,6 +283,7 @@ var makeTiny = function(offset) { if (res.statusCode === 201) { https.get(res.headers.location, function(res) { + console.log(res) res.pipe(fs.createWriteStream(output)); process.stdout.write(output.yellow + " success".green + "\n"); @@ -293,7 +293,7 @@ var makeTiny = function(offset) { } }); - + fs.createReadStream(input).pipe(request); }; @@ -306,15 +306,19 @@ var run = function() { parseArgvs(); checkApiKey(); if(files_io.length > 0) { - console.log("start waiting··· " ) - } - for(var i=0,len=files_io.length;i=len) { - logMessage('Compression complete!') - break; + logMessage("start waiting··· " ); + for(var i=0,len=files_io.length;i=len) { + logMessage('Compression complete!') + break; + } } } + else { + logError("File not found."); + } + //if(files_io.length > 0) { // makeTiny(); //} From b0968edcce910c32503cf53996a2708eeb1d92d5 Mon Sep 17 00:00:00 2001 From: yhsunshining Date: Fri, 18 Sep 2015 19:31:46 +0800 Subject: [PATCH 4/4] fuzzy matching use glob --- package.json | 41 ++++++++++++++++++++++++++++++++++++++--- tinypng.js | 27 ++++++++++++++++++++------- 2 files changed, 58 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index debd425..02587d2 100644 --- a/package.json +++ b/package.json @@ -13,11 +13,46 @@ "tinypng": "tinypng.js" }, "dependencies": { - "colors": "^0.6.2" + "colors": "^0.6.2", + "glob" : "5.0.14" }, "repository": { "type": "git", - "url": "https://github.com/ozio/tinypng.git" + "url": "git+https://github.com/ozio/tinypng.git" }, - "keywords": ["png", "compress", "image", "tinypng", "node"] + "keywords": [ + "png", + "compress", + "image", + "tinypng", + "node" + ], + "gitHead": "18ca3e756da0b21d110876e1742936f040fedde8", + "bugs": { + "url": "https://github.com/ozio/tinypng/issues" + }, + "homepage": "https://github.com/ozio/tinypng#readme", + "_id": "node-tinypng@0.0.4", + "scripts": {}, + "_shasum": "691c92dd248b6583dc7e91ee2e8b449f643f0240", + "_from": "node-tinypng@*", + "_npmVersion": "2.8.3", + "_nodeVersion": "0.12.2", + "_npmUser": { + "name": "mr_ozio", + "email": "i@mr-ozio.ru" + }, + "dist": { + "shasum": "691c92dd248b6583dc7e91ee2e8b449f643f0240", + "tarball": "http://registry.npmjs.org/node-tinypng/-/node-tinypng-0.0.4.tgz" + }, + "maintainers": [ + { + "name": "mr_ozio", + "email": "i@mr-ozio.ru" + } + ], + "directories": {}, + "_resolved": "https://registry.npmjs.org/node-tinypng/-/node-tinypng-0.0.4.tgz", + "readme": "ERROR: No README data found!" } diff --git a/tinypng.js b/tinypng.js index ec27e6c..b29aea9 100755 --- a/tinypng.js +++ b/tinypng.js @@ -1,16 +1,19 @@ //#!/usr/bin/env node +'usr strick'; var fs = require('fs'), https = require('https'), colors = require('colors'), pjson = require('./package.json'); var path = require("path"); +var glob = require("glob"); var options, options_path = __dirname + '/settings.json', files, files_io = [], file_count = 0, + file_success = 0, regPng = /\.png/i, regAll = /\.jpg|\.jpeg|\.png/i; @@ -116,6 +119,12 @@ var parseArgvs = function() { } checkArg(); + var srcs = []; + files.forEach(function (item) { + srcs = srcs.concat(glob.sync(item)); + }); + files = srcs; + console.log(files) filterFiles(files); return true; @@ -254,7 +263,7 @@ var exit = function(code) { */ var makeTiny = function(offset) { var pair = files_io[offset]; - + // if(!pair) { // return logMessage('Compression complete!'); // } @@ -275,6 +284,10 @@ var makeTiny = function(offset) { if(d.error) { process.stdout.write("error".red + "\n"); logError(input + " → ".grey+d.error + ": " + d.message); + file_count++; + if(file_count==files_io.length) { + logMessage('Total '+file_count+'files, '+file_success+' files success.'); + } //exit(1); } else { // process.stdout.write("-" + ((1 - d.output.ratio) * 100).toFixed(1) + "%" + " → ".grey); @@ -283,12 +296,15 @@ var makeTiny = function(offset) { if (res.statusCode === 201) { https.get(res.headers.location, function(res) { - console.log(res) + res.pipe(fs.createWriteStream(output)); process.stdout.write(output.yellow + " success".green + "\n"); file_count++; -// makeTiny(); + file_success++; + if(file_count==files_io.length) { + logMessage('Total '+file_count+'files, '+file_success+' files success.'); + } }); } @@ -309,10 +325,7 @@ var run = function() { logMessage("start waiting··· " ); for(var i=0,len=files_io.length;i=len) { - logMessage('Compression complete!') - break; - } + } } else {