diff --git a/lib/logger.js b/lib/logger.js new file mode 100644 index 0000000..0fecf82 --- /dev/null +++ b/lib/logger.js @@ -0,0 +1,34 @@ +require('colors'); + +/** + * Returns current time in an appropriate form + * @example 00:00:00.000 + */ +function getCurrTime() { + var now = new Date(), + _hours = now.getHours() + '', + _minutes = now.getMinutes() + '', + _seconds = now.getSeconds() + '', + _milliseconds = now.getMilliseconds() + '', + hours = (_hours.length < 2 ? '0' : '') + _hours, + minutes = (_minutes.length < 2 ? '0' : '') + _minutes, + seconds = (_seconds.length < 2 ? '0' : '') + _seconds, + milliSecLen = _milliseconds.length, + milliseconds = (milliSecLen < 3 ? (milliSecLen === 2 ? '0' : '00') : '') + _milliseconds; + + return (hours + ':' + minutes + ':' + seconds + '.' + milliseconds).grey; +} + +/** + * Logs the text + * @param {String} - type + * @param {String} - text + * @param {String} - description + */ +module.exports = function (type, text, description) { + if (type === 'o') { + console.log(text); + } else if (type === 'i' || type === 'e') { + console.log(getCurrTime() + ' - ' + (type === 'i' ? 'info'.green : 'error'.red) + ': ' + text + description.grey); + } +} diff --git a/lib/utils.js b/lib/utils.js index f612164..f17be6e 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,27 +1,7 @@ var fs = require('fs'), path = require('path'), - shell = require('shelljs'); - -require('colors'); - -/** - * Returns current time in an appropriate form - * @example 00:00:00.000 - */ -function getCurrTime() { - var now = new Date(), - _hours = now.getHours() + '', - _minutes = now.getMinutes() + '', - _seconds = now.getSeconds() + '', - _milliseconds = now.getMilliseconds() + '', - hours = (_hours.length < 2 ? '0' : '') + _hours, - minutes = (_minutes.length < 2 ? '0' : '') + _minutes, - seconds = (_seconds.length < 2 ? '0' : '') + _seconds, - milliSecLen = _milliseconds.length, - milliseconds = (milliSecLen < 3 ? (milliSecLen === 2 ? '0' : '00') : '') + _milliseconds; - - return (hours + ':' + minutes + ':' + seconds + '.' + milliseconds).grey; -} + shell = require('shelljs'), + benchLogger = require('./logger'); /** * Makes directories @@ -37,7 +17,7 @@ function mkdir(_path) { fs.mkdirSync(part); } catch(err) { if (err && err.code !== 'EEXIST') { - console.log(getCurrTime() + ' - ' + 'error'.red + ': [e] Can not create the folder --> ' + part); + benchLogger('e', '[e] Can not create the folder --> ', part); throw err; } } @@ -68,10 +48,7 @@ function minimize(minimizer, minMethod) { * @returns {Object} */ return function (minimizerName, inputFile, outputFile) { - console.log( - getCurrTime() + ' - ' + 'info'.green + ': [m] Minimization by ' + minimizerName + ' --> ' + - (inputFile + ' -> ' + outputFile).grey - ); + benchLogger('i', '[m] Minimization by ' + minimizerName + ' --> ', inputFile + ' -> ' + outputFile); var css = fs.readFileSync(inputFile, 'utf-8'), res = {}; @@ -107,7 +84,7 @@ function archive(command, fileSuffix) { .replace(/\[FILE_PATH\]/g, filePath) .replace(/\[TO_ARCH_CSS\]/g, toArchCSS); - console.log(getCurrTime() + ' - ' + 'info'.green + ': [a] ' + 'Exec bash script --> ' + _command.grey); + benchLogger('i', '[a] Exec bash script --> ', _command); shell.exec(_command); @@ -116,7 +93,6 @@ function archive(command, fileSuffix) { } module.exports = { - getCurrTime: getCurrTime, mkdir: mkdir, addSymbols: addSymbols, minimize: minimize,