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