Skip to content

Commit

Permalink
Refactor the code
Browse files Browse the repository at this point in the history
  • Loading branch information
eGavr committed Sep 3, 2014
1 parent 20389e0 commit c2e7350
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 71 deletions.
1 change: 0 additions & 1 deletion .cmb/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var path = require('path'),
fs = require('fs'),
utils = require('../lib/utils'),
minimize = utils.minimize,
archive = utils.archive;
Expand Down
88 changes: 88 additions & 0 deletions lib/bench-fs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
var fs = require('fs'),
_path = require('path'),
benchLogger = require('./bench-logger');

/**
* Makes directories
* @param {Array} - list of paths
*/
function mkdir(path) {
var splitPath = path.split(_path.sep),
part = '';

splitPath.forEach(function (nextPart) {
part = _path.join(part, nextPart)
try {
fs.mkdirSync(part);
} catch(err) {
if (err && err.code !== 'EEXIST') {
benchLogger('e', '[e] Can not create the folder --> ', part);
throw err;
}
}
});
}

/*
* Returns a list of files in a dir
* @param {String} - path to a dir
* @returns {Array}
*/
function readDir(path) {
try {
return fs.readdirSync(path);
} catch(err) {
benchLogger('e', '[e] Can not read the dir --> ', path);
throw err;
}
}

/**
* Returns the content of a file in 'utf-8' encoding
* @param {String} - path to a file
* @return {String}
*/
function readFile(path) {
try {
return fs.readFileSync(path, 'utf-8');
} catch(err) {
benchLogger('e', '[e] Can not read the file --> ', path);
throw err;
}
}

/**
* Writes to the file
* @param {String} - path
* @param {String} - content
*/
function writeFile(path, content) {
try {
fs.writeFileSync(path, content);
} catch(err) {
benchLogger('e', '[e] Can not write to the file --> ', path);
throw err;
}
}

/**
* Returns a size of a given file
* @param {String} - path
* @returns {Number}
*/
function getFileSize(path) {
try {
return fs.statSync(path).size;
} catch(err) {
benchLogger('e', '[e] Can not read the file --> ', path);
throw err;
}
}

module.exports = {
mkdir: mkdir,
readDir: readDir,
readFile: readFile,
writeFile: writeFile,
getFileSize: getFileSize
}
File renamed without changes.
2 changes: 1 addition & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var path = require('path'),
bench = require('./compare');
bench = require('./index');

module.exports = require('coa').Cmd()
.name(process.argv[1])
Expand Down
60 changes: 32 additions & 28 deletions lib/compare.js → lib/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
var fs = require('fs'),
path = require('path'),
var path = require('path'),
shell = require('shelljs'),
bench = require('../.cmb/config'),
utils = require('./utils'),
benchLogger = require('./logger'),
fs = require('./bench-fs'),
benchLogger = require('./bench-logger'),
minimizers = bench.minimizers,
archivers = bench.archivers,
toRawCSS = bench.paths.toRawCSS,
Expand All @@ -12,6 +11,16 @@ var fs = require('fs'),

require('colors');

/**
* Adds symbols for pretty logging
* @param {String} - string in the log
* @param {Number} - interval
* @returns {String}
*/
function addSymbols(interval, nameLen, symbol) {
return new Array(interval - nameLen + 1).join(symbol);
}

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

Expand All @@ -27,20 +36,20 @@ exports.compare = function (options) {

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

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

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

// minimizes and archives the file by tools which are specified in 'config.js'
var res = {};
Expand Down Expand Up @@ -106,28 +115,28 @@ exports.compare = function (options) {
// logs the test file
var _output =
(outputToFile ? '\n---> ' : '\n---> '.bold.green) +
cssFile + utils.addSymbols(40, cssFile.length, ' ') + ' --> ' +
cssFile + addSymbols(40, cssFile.length, ' ') + ' --> ' +
(outputToFile ? rawFileSize : (rawFileSize + '').bold) +
' b' + utils.addSymbols(listOfArchLen ? 22 - (rawFileSize + '').length : 2, 0, ' '),
' b' + 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;
('\n---> ' + cssFile + addSymbols(40, cssFile.length, ' ') +
' --> ' + rawFileSize + ' b' + 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, ' ') + '|';
addSymbols((maxArchSize[archiver] + '').length + 1, (currArchSize + '').length, ' ') + '|';

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

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

output += _output;

Expand All @@ -137,19 +146,19 @@ exports.compare = function (options) {
currMinTime = res.min[minimizer].time;

output +=
'\n > was minimized by ' + minimizer + utils.addSymbols(20, minimizer.length, ' ') + '--> ' +
'\n > was minimized by ' + minimizer + addSymbols(20, minimizer.length, ' ') + '--> ' +
(!outputToFile && minSize === currMinSize ? (currMinSize + '').bold.green : currMinSize) + ' b' +
utils.addSymbols((maxSize + '').length + 1, (currMinSize + '').length, ' ') + '| ' +
addSymbols((maxSize + '').length + 1, (currMinSize + '').length, ' ') + '| ' +
(!outputToFile && minTime === currMinTime ? (currMinTime + '').bold.green : currMinTime) + ' ms' +
utils.addSymbols(19 - (rawFileSize + '').length, (currMinTime + ' ms').length, ' ');
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, ' ') + '|';
addSymbols((maxArchSize[archiver] + '').length + 1, (currArchSize + '').length, ' ') + '|';
});
});

Expand All @@ -159,15 +168,10 @@ exports.compare = function (options) {
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;
}
benchLogger('i', '[r] Writing the result to the file --> ', outputToFile);
fs.writeFile(outputToFile, output);
} else {
benchLogger('i', '[r] Printing the result', '');
benchLogger('i', '[r] Logging the result', '');
benchLogger('o', output);
}

Expand Down
56 changes: 15 additions & 41 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,7 @@
var fs = require('fs'),
path = require('path'),
var path = require('path'),
shell = require('shelljs'),
benchLogger = require('./logger');

/**
* Makes directories
* @param {Array} - list of paths
*/
function mkdir(_path) {
var splitPath = _path.split(path.sep),
part = '';

splitPath.forEach(function (nextPart) {
part = path.join(part, nextPart)
try {
fs.mkdirSync(part);
} catch(err) {
if (err && err.code !== 'EEXIST') {
benchLogger('e', '[e] Can not create the folder --> ', part);
throw err;
}
}
});
}

/**
* Adds spaces for pretty logging
* @param {String} - string in the log
* @param {Number} - interval
* @returns {String}
*/
function addSymbols(interval, nameLen, symbol) {
return new Array(interval - nameLen + 1).join(symbol);
}
fs = require('./bench-fs'),
benchLogger = require('./bench-logger');

/**
* Returns a function which can minimize CSS
Expand All @@ -50,16 +19,23 @@ function minimize(minimizer, minMethod) {
return function (minimizerName, inputFile, outputFile) {
benchLogger('i', '[m] Minimization by ' + minimizerName + ' --> ', inputFile + ' -> ' + outputFile);

var css = fs.readFileSync(inputFile, 'utf-8'),
var css = fs.readFile(inputFile),
res = {};

start = Date.now();
min = minimizer[minMethod](css);

try {
min = minimizer[minMethod](css);
} catch(err) {
benchLogger('e', '[e] Minimization failed', '');
throw err;
}

end = Date.now();
res.time = end - start;

fs.writeFileSync(outputFile, min);
res.size = fs.statSync(outputFile).size;
fs.writeFile(outputFile, min);
res.size = fs.getFileSize(outputFile);

return res;
};
Expand Down Expand Up @@ -88,13 +64,11 @@ function archive(command, fileSuffix) {

shell.exec(_command);

return fs.statSync(path.join(toArchCSS, fileName + fileSuffix)).size;
return fs.getFileSize(path.join(toArchCSS, fileName + fileSuffix));
};
}

module.exports = {
mkdir: mkdir,
addSymbols: addSymbols,
minimize: minimize,
archive: archive
}

0 comments on commit c2e7350

Please sign in to comment.