From ec8e33621c1b8afb0cc9cdf61c1844c773eb341d Mon Sep 17 00:00:00 2001 From: coderaiser Date: Thu, 11 Jul 2013 15:27:08 +0000 Subject: [PATCH] refactor(socket) add condition on cd error --- lib/server/socket.js | 62 ++++++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/lib/server/socket.js b/lib/server/socket.js index 35ad5f83..c05afab9 100644 --- a/lib/server/socket.js +++ b/lib/server/socket.js @@ -96,7 +96,7 @@ function onMessage(pConnNum, pSocket){ return function(pCommand) { var lMsg, lJSON, lWinCommand, lExec_func, lDir, - lHome, lError; + lHome, lError, lRet; Util.log('#' + pConnNum + ': ' + pCommand); @@ -139,7 +139,9 @@ process.chdir(lDir); }); - if (lError) { + if (!lError) + lRet = true; + else { lJSON = { stderr: Util.stringifyJSON( lError ) }; @@ -150,23 +152,25 @@ } } - /* if we on windows and command is build in - * change code page to unicode becouse - * windows use unicode on non English versions - */ - if(WIN32){ - lWinCommand = pCommand.toUpperCase(); + if (!lRet) { + /* if we on windows and command is build in + * change code page to unicode becouse + * windows use unicode on non English versions + */ + if(WIN32){ + lWinCommand = pCommand.toUpperCase(); + + if( Win32Commands.indexOf(lWinCommand) >= 0 ) + pCommand = 'chcp 65001 |' + pCommand; + } - if( Win32Commands.indexOf(lWinCommand) >= 0 ) - pCommand = 'chcp 65001 |' + pCommand; + if(!ClientFuncs[pConnNum]) + ClientFuncs[pConnNum] = getExec(pSocket, pConnNum); + + lExec_func = ClientFuncs[pConnNum]; + + exec(pCommand, lExec_func); } - - if(!ClientFuncs[pConnNum]) - ClientFuncs[pConnNum] = getExec(pSocket, pConnNum); - - lExec_func = ClientFuncs[pConnNum]; - - exec(pCommand, lExec_func); }; } @@ -177,19 +181,27 @@ */ function getExec(pSocket, pConnNum){ return function(pError, pStdout, pStderr) { - var lErrorJSON = pStderr || pError, - lError = lErrorJSON && Util.stringifyJSON( lErrorJSON ), - lExec = { - stdout : pStdout, - stderr : lError - }, + var lErrorStr, lExecStr, lExec, + lError = pStderr || pError; - lExec_str = JSON.stringify(lExec); + + if (lError) + if ( Util.isString(lError) ) + lErrorStr = lError; + else + lErrorStr = Util.stringifyJSON( lError ); + + lExec = { + stdout : pStdout, + stderr : lError + }; + + lExecStr = JSON.stringify(lExec); log(pConnNum, pError, 'error'); log(pConnNum, pStderr, 'stderror'); - pSocket.send(lExec_str); + pSocket.send(lExecStr); }; }