From b3d633cb1c4f47df9f08fa18d6fe25ef71d27439 Mon Sep 17 00:00:00 2001 From: anseki Date: Wed, 25 Mar 2015 16:56:43 +0900 Subject: [PATCH] It's no longer only the shell since v0.7.8, and change the names. --- lib/readline-sync.js | 72 ++++++++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 33 deletions(-) diff --git a/lib/readline-sync.js b/lib/readline-sync.js index 0251cfa..b37e9c4 100644 --- a/lib/readline-sync.js +++ b/lib/readline-sync.js @@ -10,8 +10,7 @@ var IS_WIN = process.platform === 'win32', - SHELL_PATH = IS_WIN ? 'cscript.exe' : '/bin/sh', - SHELL_CMD = __dirname + (IS_WIN ? '\\read.cs.js' : '/read.sh'), + ALGORITHM_CIPHER = 'aes-256-cbc', ALGORITHM_HASH = 'sha256', DEFAULT_ERR_MSG = 'The platform doesn\'t support interactive reading', @@ -25,16 +24,16 @@ var bufSize = 1024, print, mask = '*', - useShell = false, + useExt = false, fdR = 'none', fdW, ttyR, isRawMode = false, - tempdir, salt = 0; + extHostPath, extScriptPath, tempdir, salt = 0; -function _readlineSync(options) { // options.display is string +function readlineSync(options) { // display, mask are string var input = '', isEditable, displayInput; - function tryShell() { - var res = _readlineShell(options); + function tryExt() { + var res = readlineExt(options); if (res.error) { throw res.error; } return res.input; } @@ -111,7 +110,7 @@ function _readlineSync(options) { // options.display is string } })(); - // Call before tryShell() + // Call before tryExt() if (options.display !== '' && typeof print === 'function') { print(options.display, encoding); } @@ -120,9 +119,9 @@ function _readlineSync(options) { // options.display is string (function() { // try read var buffer, reqSize, readSize, chunk; - if (useShell || !ttyR || + if (useExt || !ttyR || typeof fdW !== 'number' && (options.display !== '' || !isEditable)) { - input = tryShell(); + input = tryExt(); return; } @@ -132,7 +131,7 @@ function _readlineSync(options) { // options.display is string } if (!setRawMode(!isEditable)) { - input = tryShell(); + input = tryExt(); return; } buffer = new Buffer((reqSize = options.keyIn ? 1 : bufSize)); @@ -145,7 +144,7 @@ function _readlineSync(options) { // options.display is string } catch (e) { if (e.code === 'EOF') { break; } setRawMode(false); - input += tryShell(); + input += tryExt(); return; } @@ -157,8 +156,8 @@ function _readlineSync(options) { // options.display is string if (!isEditable && (displayInput = chunk.replace(/[\r\n]/g, '')) !== '') { if (options.noEchoBack) { - displayInput = mask === '' ? '' : - (new Array(displayInput.length + 1)).join(mask); + displayInput = options.mask === '' ? '' : + (new Array(displayInput.length + 1)).join(options.mask); } if (displayInput !== '') { fs.writeSync(fdW, displayInput); } } @@ -175,14 +174,15 @@ function _readlineSync(options) { // options.display is string if (typeof print === 'function') { displayInput = input.replace(/[\r\n]/g, ''); print((options.noEchoBack ? - displayInput.replace(/./g, mask) : displayInput) + '\n', encoding); + (new Array(displayInput.length + 1)).join(options.mask) : displayInput) + + '\n', encoding); } return options.noTrim || options.keyIn ? input.replace(/[\r\n]+$/, '') : input.trim(); } -function _readlineShell(options) { +function readlineExt(options) { var cmdArgs = [], execArgs, res = {}, execOptions = { env: process.env, @@ -192,6 +192,9 @@ function _readlineShell(options) { encoding: encoding }; + extHostPath = extHostPath || (IS_WIN ? 'cscript.exe' : '/bin/sh'); + extScriptPath = extScriptPath || (__dirname + (IS_WIN ? '\\read.cs.js' : '/read.sh')); + // To send any text to crazy Windows shell safely. function encodeDOS(arg) { return arg.replace(/[^\w\u0080-\uFFFF]/g, function(chr) { @@ -207,21 +210,21 @@ function _readlineShell(options) { } if (childProc.execFileSync) { - execArgs = (IS_WIN ? ['//nologo', SHELL_CMD] : [SHELL_CMD]).concat(cmdArgs); + execArgs = (IS_WIN ? ['//nologo', extScriptPath] : [extScriptPath]).concat(cmdArgs); try { - res.input = childProc.execFileSync(SHELL_PATH, execArgs, execOptions); + res.input = childProc.execFileSync(extHostPath, execArgs, execOptions); } catch (e) { // non-zero exit code res.error = new Error(DEFAULT_ERR_MSG); res.error.method = 'execFileSync'; - res.error.command = SHELL_CMD; + res.error.command = extScriptPath; res.error.args = cmdArgs; - res.error.shellMessage = e.stderr.trim(); + res.error.ExtMessage = e.stderr.trim(); res.error.code = e.code; res.error.signal = e.signal; res.error.exitCode = e.status; } } else { - res = _execSyncByFile(cmdArgs, execOptions); + res = _execFileSync(cmdArgs, execOptions); } if (!res.error) { res.input = res.input.replace(/^'|'$/g, ''); @@ -232,7 +235,7 @@ function _readlineShell(options) { } // piping via files (node v0.10-) -function _execSyncByFile(cmdArgs, execOptions) { +function _execFileSync(cmdArgs, execOptions) { function getTempfile(name) { var path = require('path'), filepath, suffix = '', fd; @@ -274,7 +277,7 @@ function _execSyncByFile(cmdArgs, execOptions) { // `()` for ignore space by echo execArgs = ['/V:ON', '/S', '/C', '(%Q%' + interpreter + '%Q% /V:ON /S /C %Q%' + - '%Q%' + SHELL_PATH + '%Q% //nologo %Q%' + SHELL_CMD + '%Q%' + + '%Q%' + extHostPath + '%Q% //nologo %Q%' + extScriptPath + '%Q%' + cmdArgs.map(function(arg) { return ' %Q%' + arg + '%Q%'; }).join('') + ' & (echo !ERRORLEVEL!)>%Q%' + pathExit + '%Q%%Q%) 2>%Q%' + pathStderr + '%Q%' + ' |%Q%' + process.execPath + '%Q% %Q%' + __dirname + '\\encrypt.js%Q%' + @@ -282,10 +285,10 @@ function _execSyncByFile(cmdArgs, execOptions) { ' >%Q%' + pathStdout + '%Q%' + ' & (echo 1)>%Q%' + pathDone + '%Q%']; } else { - interpreter = SHELL_PATH; + interpreter = extHostPath; execArgs = ['-c', // Use `()`, not `{}` for `-c` (text param) - '("' + SHELL_PATH + '" "' + SHELL_CMD + '"' + + '("' + extHostPath + '" "' + extScriptPath + '"' + cmdArgs.map(function(arg) { return " '" + arg.replace(/'/g, "'\\''") + "'"; }).join('') + '; echo $?>"' + pathExit + '") 2>"' + pathStderr + '"' + @@ -298,7 +301,7 @@ function _execSyncByFile(cmdArgs, execOptions) { childProc.spawn(interpreter, execArgs, execOptions); } catch (e) { res.error = new Error(e.message); - res.error.method = '_execSyncByFile - spawn'; + res.error.method = '_execFileSync - spawn'; res.error.interpreter = interpreter; } @@ -309,10 +312,10 @@ function _execSyncByFile(cmdArgs, execOptions) { decipher.final(encoding); } else { res.error = new Error(DEFAULT_ERR_MSG); - res.error.method = '_execSyncByFile'; - res.error.command = SHELL_CMD; + res.error.method = '_execFileSync'; + res.error.command = extScriptPath; res.error.args = cmdArgs; - res.error.shellMessage = fs.readFileSync(pathStderr, {encoding: encoding}).trim(); + res.error.ExtMessage = fs.readFileSync(pathStderr, {encoding: encoding}).trim(); res.error.exitCode = +exitCode; } @@ -325,7 +328,7 @@ function _execSyncByFile(cmdArgs, execOptions) { } // for dev -exports._useShellSet = function(use) { useShell = use; }; +exports._useExtSet = function(use) { useExt = use; }; exports.setPrint = function(fnc) { print = fnc; }; @@ -364,10 +367,11 @@ exports.prompt = function(options) { var readOptions = { display: promptText + '', noEchoBack: !!(options && options.noEchoBack), + mask: mask, keyIn: false, noTrim: !!(options && options.noTrim) }; - return _readlineSync(readOptions); + return readlineSync(readOptions); }; exports.question = function(query, options) { @@ -376,10 +380,11 @@ exports.question = function(query, options) { display: query != null ? query + '' : '', /* jshint eqnull:false */ noEchoBack: !!(options && options.noEchoBack), + mask: mask, keyIn: false, noTrim: !!(options && options.noTrim) }; - return _readlineSync(readOptions); + return readlineSync(readOptions); }; exports.keyIn = function(query, options) { @@ -388,8 +393,9 @@ exports.keyIn = function(query, options) { display: query != null ? query + '' : '', /* jshint eqnull:false */ noEchoBack: !!(options && options.noEchoBack), + mask: mask, keyIn: true, noTrim: true }; - return _readlineSync(readOptions); + return readlineSync(readOptions); };