From 7aea0483a63a1822bdcc1fe962c9b83e4590eb83 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Fri, 31 Jan 2014 06:01:46 -0500 Subject: [PATCH] refactor(commander) readDir: params -> error, files, params --- lib/server/commander.js | 88 ++++++++++++++++++++--------------------- 1 file changed, 42 insertions(+), 46 deletions(-) diff --git a/lib/server/commander.js b/lib/server/commander.js index d65bf156..07c4943d 100644 --- a/lib/server/commander.js +++ b/lib/server/commander.js @@ -22,13 +22,15 @@ exports.getDirContent = function(path, callback) { var ret = Util.isString(path); - if (ret) - fs.readdir(path, Util.call(readDir, { - callback: callback, - path : path - })); - else + if (!ret) Util.exec(callback, "First parameter should be a string"); + else + fs.readdir(path, function(error, files) { + readDir(error, files, { + callback : callback, + path : path + }); + }); return ret; }; @@ -39,49 +41,43 @@ * @param pError * @param pFiles */ - function readDir(params) { + function readDir(error, files, params) { var i, n, stats, filesData, fill, name, fileParams, - p, c, files, dirPath, - ret = checkParams(params); - ret = ret && Util.checkObj(params.params, ['path']); + + p = params, + dirPath = getDirPath(p.path); - if (ret) { - p = params, - c = params.params, - files = p.data, - dirPath = getDirPath(c.path); + if (error) + Util.exec(p.callback, error.toString()); + else { + /* Получаем информацию о файлах */ + n = files.length, + stats = {}, - if (!p.error && files) { - /* Получаем информацию о файлах */ - n = files.length, - stats = {}, - - filesData = { - files : files, - stats : stats, - callback : c.callback, - path : c.path - }, - - fill = Util.retFunc(fillJSON, filesData); - - if (n) - for (i = 0; i < n; i++) { - name = dirPath + files[i], - - fileParams = { - callback : fill, - count : n, - name : files[i], - stats : stats, - }; - - fs.stat(name, Util.call(getFilesStat, fileParams)); - } - else - fillJSON(filesData); - } else - Util.exec(c.callback, p.error.toString()); + filesData = { + files : files, + stats : stats, + callback : p.callback, + path : p.path + }, + + fill = Util.retFunc(fillJSON, filesData); + + if (n) + for (i = 0; i < n; i++) { + name = dirPath + files[i], + + fileParams = { + callback : fill, + count : n, + name : files[i], + stats : stats, + }; + + fs.stat(name, Util.call(getFilesStat, fileParams)); + } + else + fillJSON(filesData); } }