Skip to content

Commit

Permalink
fix: output profit in red if file size increased (#1162)
Browse files Browse the repository at this point in the history
  • Loading branch information
gimler committed Nov 27, 2023
1 parent 4d15ef2 commit 3f2bcb4
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions lib/svgo/coa.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const regSVGFile = /\.svg$/i;

/**
* Synchronously check if path is a directory. Tolerant to errors like ENOENT.
*
* @param {string} path
*/
function checkIsDir(path) {
Expand Down Expand Up @@ -276,6 +277,7 @@ async function action(args, opts, command) {

/**
* Optimize SVG files in a directory.
*
* @param {Object} config options
* @param {string} dir input directory
* @param {string} output output directory
Expand All @@ -292,6 +294,7 @@ function optimizeFolder(config, dir, output) {

/**
* Process given files, take only SVG.
*
* @param {Object} config options
* @param {string} dir input directory
* @param {Array} files list of file names in the directory
Expand All @@ -318,7 +321,8 @@ function processDirectory(config, dir, files, output) {
}

/**
* Get svg files descriptions
* Get SVG files descriptions.
*
* @param {Object} config options
* @param {string} dir input directory
* @param {Array} files list of file names in the directory
Expand Down Expand Up @@ -360,6 +364,7 @@ function getFilesDescriptions(config, dir, files, output) {

/**
* Read SVG file and pass to processing.
*
* @param {Object} config options
* @param {string} file
* @param {string} output
Expand All @@ -375,6 +380,7 @@ function optimizeFile(config, file, output) {

/**
* Optimize SVG data.
*
* @param {Object} config options
* @param {string} data SVG content to optimize
* @param {string} output where to write optimized file
Expand Down Expand Up @@ -425,6 +431,7 @@ function processSVGData(config, info, data, output, input) {

/**
* Write result of an optimization.
*
* @param {string} input
* @param {string} output output file name. '-' for stdout
* @param {string} data data to write
Expand All @@ -444,34 +451,37 @@ function writeOutput(input, output, data) {
}

/**
* Write a time taken by optimization.
* Write time taken to optimize.
*
* @param {number} time time in milliseconds.
*/
function printTimeInfo(time) {
console.log(`Done in ${time} ms!`);
}

/**
* Write optimizing information in human readable format.
* Write optimizing stats in a human-readable format.
*
* @param {number} inBytes size before optimization.
* @param {number} outBytes size after optimization.
*/
function printProfitInfo(inBytes, outBytes) {
var profitPercents = 100 - (outBytes * 100) / inBytes;
const profitPercent = 100 - (outBytes * 100) / inBytes;
/** @type {[string, Function]} */
const ui = profitPercent < 0 ? ['+', colors.red] : ['-', colors.green];

console.log(
Math.round((inBytes / 1024) * 1000) / 1000 +
' KiB' +
(profitPercents < 0 ? ' + ' : ' - ') +
colors.green(Math.abs(Math.round(profitPercents * 10) / 10) + '%') +
' = ' +
Math.round((outBytes / 1024) * 1000) / 1000 +
' KiB'
Math.round((inBytes / 1024) * 1000) / 1000 + ' KiB',
ui[0],
ui[1](Math.abs(Math.round(profitPercent * 10) / 10) + '%'),
'=',
Math.round((outBytes / 1024) * 1000) / 1000 + ' KiB'
);
}

/**
* Check for errors, if it's a dir optimize the dir.
*
* @param {Object} config
* @param {string} input
* @param {string} output
Expand All @@ -491,6 +501,7 @@ function checkOptimizeFileError(config, input, output, error) {

/**
* Check for saving file error. If the output is a dir, then write file there.
*
* @param {string} input
* @param {string} output
* @param {string} data
Expand All @@ -509,9 +520,7 @@ function checkWriteFileError(input, output, data, error) {
}
}

/**
* Show list of available plugins with short description.
*/
/** Show list of available plugins with short description. */
function showAvailablePlugins() {
const list = builtin
.sort((a, b) => a.name.localeCompare(b.name))
Expand Down

0 comments on commit 3f2bcb4

Please sign in to comment.