From fc50c9636817fa6242d5003499d67f6534d7d496 Mon Sep 17 00:00:00 2001 From: anseki Date: Wed, 4 Mar 2015 02:30:41 +0900 Subject: [PATCH] Fix: Can't get error from shell --- lib/read.bat | 28 +++++++++---- lib/read.cs.js | 12 +++++- lib/read.sh | 15 ++++--- lib/readline-sync.js | 98 +++++++++++++++++++++++++++----------------- package.json | 2 +- 5 files changed, 99 insertions(+), 56 deletions(-) diff --git a/lib/read.bat b/lib/read.bat index a4d1997..035bfc5 100644 --- a/lib/read.bat +++ b/lib/read.bat @@ -1,9 +1,13 @@ @echo off setlocal +setlocal ENABLEDELAYEDEXPANSION + if "%1"=="noechoback" ( call :exprog + if ERRORLEVEL 1 exit /b 1 ) else ( set /p INPUT=CON + if ERRORLEVEL 1 exit /b 1 ) set /p ="'%INPUT%'"NUL 2>&1 - :: Win <7 and CON'); while (oExec.Status === 0) { WScript.Sleep(100); } diff --git a/lib/read.sh b/lib/read.sh index bbaa43d..39ddc57 100644 --- a/lib/read.sh +++ b/lib/read.sh @@ -1,24 +1,23 @@ -silentRead() { +silent_read() { stty --file=/dev/tty -echo echonl 2>/dev/null || \ stty -F /dev/tty -echo echonl 2>/dev/null || \ - stty -f /dev/tty -echo echonl 2>/dev/null || \ - exit 1 - IFS= read -r INPUT /dev/null || \ stty -F /dev/tty echo -echonl 2>/dev/null || \ - stty -f /dev/tty echo -echonl 2>/dev/null + stty -f /dev/tty echo -echonl || exit 1 } if [ "$1" = "noechoback" ]; then # Try `-s` option. *ksh have it that not `--silent`. Therefore, don't try it. if [ -n "$BASH_VERSION" ] || [ -n "$ZSH_VERSION" ]; then - IFS= read -rs INPUT /dev/null || silentRead + IFS= read -rs INPUT /dev/null || silent_read printf '\n' >/dev/tty else - silentRead + silent_read fi else - IFS= read -r INPUT /dev/null || exit 1 + IFS= read -r INPUT %Q%' + pathStatus + '%Q%%Q%) 2>%Q%' + pathStderr + '%Q%' + ' |%Q%' + process.execPath + '%Q% %Q%' + __dirname + '\\encrypt.js%Q%' + ' %Q%' + ALGORITHM_CIPHER + '%Q% %Q%' + password + '%Q%' + - ' >%Q%' + pathStdout + '%Q%' + - ' & (echo !ERRORLEVEL!)>%Q%' + pathStatus + '%Q% & (echo 1)>%Q%' + pathDone + '%Q%']; + ' >%Q%' + pathStdout + '%Q%' + + ' & (echo 1)>%Q%' + pathDone + '%Q%']; } else { - commandArgs = ['-c', - 'DATA=`(' + SHELL_PATH + ' "' + SHELL_COMMAND + '" ' + args.join(' ') + ')`;' + - ' RTN=$?; if [ $RTN -eq 0 ]; then (printf \'%s\' "$DATA"' + + cmdArgs = ['-c', + '(' + SHELL_PATH + ' "' + SHELL_CMD + '" ' + args.join(' ') + + '; echo $?>"' + pathStatus + '") 2>"' + pathStderr + '"' + ' |"' + process.execPath + '" "' + __dirname + '/encrypt.js"' + ' "' + ALGORITHM_CIPHER + '" "' + password + '"' + - ' >"' + pathStdout + '") fi;' + - ' expr $RTN + $? >"' + pathStatus + '"; echo 1 >"' + pathDone + '"']; + ' >"' + pathStdout + '"' + + '; echo 1 >"' + pathDone + '"']; } - childProc.spawn(SHELL_PATH, commandArgs, execOptions); + childProc.spawn(SHELL_PATH, cmdArgs, execOptions); while (fs.readFileSync(pathDone, {encoding: encoding}).trim() !== '1') {} if (fs.readFileSync(pathStatus, {encoding: encoding}).trim() === '0') { - shellStdout = + res.stdout = decipher.update(fs.readFileSync(pathStdout, {encoding: 'binary'}), 'hex', encoding) + decipher.final(encoding); - shellStdout = shellStdout.replace(/^'|'$/g, ''); + } else { + res = {error: true, props: { + method: '_execSyncByFile', + command: SHELL_CMD, args: args, + stderr: fs.readFileSync(pathStderr, {encoding: encoding}).trim() + }}; } fs.unlinkSync(pathStdout); + fs.unlinkSync(pathStderr); fs.unlinkSync(pathStatus); fs.unlinkSync(pathDone); - return shellStdout; + return res; } // for dev diff --git a/package.json b/package.json index acd261c..c9469bd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "readline-sync", - "version": "0.7.3", + "version": "0.7.4", "title": "readlineSync", "description": "Synchronous Readline", "keywords": [