Skip to content

Commit

Permalink
Use COA; add 'output' option
Browse files Browse the repository at this point in the history
  • Loading branch information
eGavr committed Sep 3, 2014
1 parent 914cf30 commit 870eb79
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 161 deletions.
2 changes: 1 addition & 1 deletion bin/compare-minimizers
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env node

require('../lib/index.js')
require('../lib/cli.js')
27 changes: 27 additions & 0 deletions lib/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var path = require('path'),
bench = require('./compare');

module.exports = require('coa').Cmd()
.name(process.argv[1])
.helpful()
.title('Сompares the work of CSS minimizers')
.opt()
.name('version')
.title('Shows the version number')
.short('v').long('version')
.flag()
.only()
.act(function () {
var p = require('../package.json');
return p.name + ' ' + p.version;
})
.end()
.opt()
.name('output')
.title('Path to the result output file')
.short('o').long('output')
.end()
.act(function (opts) {
bench.compare(opts);
})
.run(process.argv.slice(2));
175 changes: 175 additions & 0 deletions lib/compare.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
var fs = require('fs'),
path = require('path'),
shell = require('shelljs'),
bench = require('../.cmb/config'),
utils = require('./utils'),
benchLogger = require('./logger'),
minimizers = bench.minimizers,
archivers = bench.archivers,
toRawCSS = bench.paths.toRawCSS,
toMinCSS = bench.paths.toMinCSS,
toArchCSS = bench.paths.toArchCSS;

require('colors');

exports.compare = function (options) {
var outputToFile = options.output;

shell.exec('ulimit -n 8192');

// constants
var MAX_TIME = Number.MAX_VALUE;

var listOfMinimizers = Object.keys(minimizers) || [],
listOfArchivers = Object.keys(archivers) || [],
listOfMinLen = listOfMinimizers.length,
listOfArchLen = listOfArchivers.length;

// makes dirs
listOfMinimizers.forEach(function (minimizer) {
utils.mkdir(path.join(toMinCSS, minimizer));
listOfArchivers.forEach(function (archiver) {
utils.mkdir(path.join(toArchCSS, archiver, 'raw'));
utils.mkdir(path.join(toArchCSS, archiver, minimizer));
})
});

// Ok! Let's GO!
var output = '\n' + (outputToFile ? 'Result:' : 'Result:'.bold.green) + '\n',
cssFiles = fs.readdirSync(toRawCSS);

cssFiles.forEach(function (cssFile) {
var pathToRawFile = path.join(toRawCSS, cssFile),
rawFileSize = fs.statSync(pathToRawFile).size;

// minimizes and archives the file by tools which are specified in 'config.js'
var res = {};

if (listOfMinLen) {
res.min = {};
} else {
benchLogger('e', '[e] Spicify minimizers in \'.cmb/config.js\'', '')
process.exit(1);
}
listOfArchLen && (res.arch = {});

listOfMinimizers.forEach(function (minimizer) {
var pathToMinCSS = path.join(toMinCSS, minimizer, cssFile),
minimizeMethod = minimizers[minimizer];

res.min[minimizer] = minimizeMethod(minimizer, pathToRawFile, pathToMinCSS);

listOfArchivers.forEach(function (archiver) {
var pathToArchCSS = path.join(toArchCSS, archiver),
pathToRawArchCSS = path.join(pathToArchCSS, 'raw'),
pathToMinArchCSS = path.join(pathToArchCSS, minimizer),
archiveScript = archivers[archiver];

if (!res.arch.hasOwnProperty(archiver)) {
res.arch[archiver] = {};
}
if (!res.arch.hasOwnProperty('raw')) {
res.arch[archiver].raw = archiveScript(cssFile, pathToRawFile, pathToRawArchCSS);
}
res.arch[archiver][minimizer] = archiveScript(cssFile, pathToMinCSS, pathToMinArchCSS);
});
});

// gets the best and the fastest results
var minSize = rawFileSize,
maxSize = rawFileSize,
minTime = MAX_TIME;

listOfMinimizers.forEach(function (minimizer) {
var currMinSize = res.min[minimizer].size,
currMinTime = res.min[minimizer].time;

minSize = Math.min(minSize, currMinSize);
maxSize = Math.max(maxSize, currMinSize);
minTime = Math.min(minTime, currMinTime);
});

var minArchSize = {},
maxArchSize = {};

listOfArchivers.forEach(function (archiver) {
minArchSize[archiver] = res.arch[archiver].raw;
maxArchSize[archiver] = res.arch[archiver].raw;
listOfMinimizers.forEach(function (minimizer) {
var currArchSize = res.arch[archiver][minimizer];

minArchSize[archiver] = Math.min(minArchSize[archiver], currArchSize);
maxArchSize[archiver] = Math.max(maxArchSize[archiver], currArchSize);
});
});

// logs the test file
var _output =
(outputToFile ? '\n---> ' : '\n---> '.bold.green) +
cssFile + utils.addSymbols(40, cssFile.length, ' ') + ' --> ' +
(outputToFile ? rawFileSize : (rawFileSize + '').bold) +
' b' + utils.addSymbols(listOfArchLen ? 22 - (rawFileSize + '').length : 2, 0, ' '),

_outputLen =
('\n---> ' + cssFile + utils.addSymbols(40, cssFile.length, ' ') +
' --> ' + rawFileSize + ' b' + utils.addSymbols(listOfArchLen ? 22 - (rawFileSize + '').length : 2, 0, ' ')).length;

listOfArchivers.forEach(function (archiver) {
var currArchSize = res.arch[archiver].raw;

_output +=
' + ' + archiver + ' > ' +
(!outputToFile && minArchSize[archiver] === currArchSize ? (currArchSize + '').green : currArchSize) + ' b' +
utils.addSymbols((maxArchSize[archiver] + '').length + 1, (currArchSize + '').length, ' ') + '|';

_outputLen +=
(' + ' + archiver + ' > ' + currArchSize + ' b' +
utils.addSymbols((maxArchSize[archiver] + '').length + 1, (currArchSize + '').length, ' ') + '|').length;
});

_output += '\n' + utils.addSymbols(_outputLen - 1, 0, '-');

output += _output;

// logs the results of the test
listOfMinimizers.forEach(function (minimizer) {
var currMinSize = res.min[minimizer].size,
currMinTime = res.min[minimizer].time;

output +=
'\n > was minimized by ' + minimizer + utils.addSymbols(20, minimizer.length, ' ') + '--> ' +
(!outputToFile && minSize === currMinSize ? (currMinSize + '').bold.green : currMinSize) + ' b' +
utils.addSymbols((maxSize + '').length + 1, (currMinSize + '').length, ' ') + '| ' +
(!outputToFile && minTime === currMinTime ? (currMinTime + '').bold.green : currMinTime) + ' ms' +
utils.addSymbols(19 - (rawFileSize + '').length, (currMinTime + ' ms').length, ' ');

listOfArchivers.forEach(function (archiver) {
var currArchSize = res.arch[archiver][minimizer];

output +=
' + ' + archiver + ' > ' +
(!outputToFile && minArchSize[archiver] === currArchSize ? (currArchSize + '').bold.green : currArchSize) + ' b' +
utils.addSymbols((maxArchSize[archiver] + '').length + 1, (currArchSize + '').length, ' ') + '|';
});
});

output += '\n';
});

benchLogger('i', '[c] Everything was compared', '');

if (outputToFile) {
try {
benchLogger('i', '[r] Writing the result to the file --> ', outputToFile);
fs.writeFileSync(outputToFile, output);
} catch(err) {
benchLogger('e', '[e] Can not write to the file --> ', outputToFile)
throw err;
}
} else {
benchLogger('i', '[r] Printing the result', '');
benchLogger('o', output);
}

return output;
}
153 changes: 0 additions & 153 deletions lib/index.js

This file was deleted.

Loading

0 comments on commit 870eb79

Please sign in to comment.