From c2e7350542797d9dae419e6d780f8ed6d231b3e9 Mon Sep 17 00:00:00 2001 From: Evgeniy Gavryushin Date: Wed, 3 Sep 2014 20:03:54 +0400 Subject: [PATCH] Refactor the code --- .cmb/config.js | 1 - lib/bench-fs.js | 88 ++++++++++++++++++++++++++++++ lib/{logger.js => bench-logger.js} | 0 lib/cli.js | 2 +- lib/{compare.js => index.js} | 60 ++++++++++---------- lib/utils.js | 56 +++++-------------- 6 files changed, 136 insertions(+), 71 deletions(-) create mode 100644 lib/bench-fs.js rename lib/{logger.js => bench-logger.js} (100%) rename lib/{compare.js => index.js} (72%) diff --git a/.cmb/config.js b/.cmb/config.js index 2501f05..ff69997 100644 --- a/.cmb/config.js +++ b/.cmb/config.js @@ -1,5 +1,4 @@ var path = require('path'), - fs = require('fs'), utils = require('../lib/utils'), minimize = utils.minimize, archive = utils.archive; diff --git a/lib/bench-fs.js b/lib/bench-fs.js new file mode 100644 index 0000000..b4f52d9 --- /dev/null +++ b/lib/bench-fs.js @@ -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 +} diff --git a/lib/logger.js b/lib/bench-logger.js similarity index 100% rename from lib/logger.js rename to lib/bench-logger.js diff --git a/lib/cli.js b/lib/cli.js index 555c078..3272bc5 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -1,5 +1,5 @@ var path = require('path'), - bench = require('./compare'); + bench = require('./index'); module.exports = require('coa').Cmd() .name(process.argv[1]) diff --git a/lib/compare.js b/lib/index.js similarity index 72% rename from lib/compare.js rename to lib/index.js index 1bae75b..1250247 100644 --- a/lib/compare.js +++ b/lib/index.js @@ -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, @@ -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; @@ -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 = {}; @@ -106,13 +115,13 @@ 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; @@ -120,14 +129,14 @@ exports.compare = function (options) { _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; @@ -137,11 +146,11 @@ 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]; @@ -149,7 +158,7 @@ exports.compare = function (options) { 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, ' ') + '|'; }); }); @@ -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); } diff --git a/lib/utils.js b/lib/utils.js index f17be6e..449bbd9 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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 @@ -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; }; @@ -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 }