Skip to content

Commit

Permalink
lib/svgo/coa: refactoring, colors and fix svg#70
Browse files Browse the repository at this point in the history
  • Loading branch information
deepsweet committed Dec 17, 2012
1 parent 74bbdb4 commit 8bf3f40
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 44 deletions.
99 changes: 56 additions & 43 deletions lib/svgo/coa.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

require('colors');

var FS = require('fs'),
QFS = require('q-fs'),
PATH = require('path'),
Expand Down Expand Up @@ -99,7 +101,6 @@ module.exports = require('coa').Cmd()
output = args && args.output ? args.output : opts.output,
string = opts.string,
folder = opts.folder,
startTime = Date.now(),
svgo;

if (
Expand Down Expand Up @@ -133,60 +134,73 @@ module.exports = require('coa').Cmd()

return svgo.then(function(result) {

if (result.info.outBytes >= result.info.inBytes) {
// --datauri
if (opts.datauri) {
// convert to Data URI base64 string
result.data = encodeSVGDatauri(result.data);
}

UTIL.puts('\nThere is nothing to optimize!\n');
// stdout
if (output === '-' || (input === '-' && !output)) {
process.stdout.write(result.data + '\n');
}

} else {
// file
else {

// --datauri
if (opts.datauri) {
// convert to Data URI base64 string
result.data = encodeSVGDatauri(result.data);
// if input is from file - overwrite it
if (!output && input) {
output = input;
}

// stdout
if (output === '-' || (input === '-' && !output)) {
process.stdout.write(result.data + '\n');
}
UTIL.puts('\r');
saveFileAndPrintInfo(result, output, opts.pretty);

// file
else {
}

// if input is from file - overwrite it
if (!output && input) {
output = input;
}
})
.done();

});

/**
* Save file and print info.
*
* @param {Object} result SVGO result
* @param {String} output output filename
* @param {Boolean} pretty is pretty printed?
*/
function saveFileAndPrintInfo(result, output, pretty) {

// output file
output = FS.createWriteStream(output, { encoding: 'utf8' });
output.write(result.data);
output.end();
if (!pretty && result.info.outBytes >= result.info.inBytes) {

// print time info
printTimeInfo(startTime, Date.now());
UTIL.puts('There is nothing to optimize!\n'.yellow);

// print optimization profit info
printProfitInfo(result.info.inBytes, result.info.outBytes);
} else {

}
// output file
output = FS.createWriteStream(output, { encoding: 'utf8' });
output.write(result.data);
output.end();

}
// print time info
printTimeInfo(result.info.time);

})
.done();
// print optimization profit info
printProfitInfo(result.info.inBytes, result.info.outBytes);

});
}

}

/**
* Print time info.
*
* @param {Date} startTime start time
* @param {Date} endTime end time
* @param {Number} time working time in ms
*/
function printTimeInfo(startTime, endTime) {
function printTimeInfo(time) {

UTIL.puts('\nDone in ' + (endTime - startTime) + ' ms!\n');
UTIL.puts('Done in ' + time + ' ms!');

}

Expand All @@ -201,8 +215,9 @@ function printProfitInfo(inBytes, outBytes) {
var profitPercents = 100 - outBytes * 100 / inBytes;

UTIL.puts(
(Math.round((inBytes / 1024) * 1000) / 1000) + ' KiB - ' +
(Math.round(profitPercents * 10) / 10) + '% = ' +
(Math.round((inBytes / 1024) * 1000) / 1000) + ' KiB' +
(profitPercents < 0 ? ' + ' : ' - ') +
String(Math.abs((Math.round(profitPercents * 10) / 10)) + '%').green + ' = ' +
(Math.round((outBytes / 1024) * 1000) / 1000) + ' KiB\n'
);

Expand Down Expand Up @@ -242,19 +257,17 @@ function optimizeFolder(folder, opts) {
return svgo.fromFile(item)
.then(function(result) {

var output = FS.createWriteStream(item, { encoding: 'utf8' });
output.write(result.data);
output.end();

UTIL.puts(filename + ':');
printProfitInfo(result.info.inBytes, result.info.outBytes);
saveFileAndPrintInfo(result, item, opts.pretty);

});

}

})
.done();
.fail(function(e) {
UTIL.puts(filename + ':\n' + String('Error! "' + e.message + '"').red + '\n');
});

});

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"coa": "~0.3.7",
"inherit": "",
"node.extend": "",
"yamljs": "~0.1.3"
"yamljs": "~0.1.3",
"colors": "~0.6.0"
},
"devDependencies": {
"mocha": "~1.7.0",
Expand Down

0 comments on commit 8bf3f40

Please sign in to comment.