Add reading shell through files (pipe).
This commit is contained in:
parent
bf3341383d
commit
7afb24a972
3 changed files with 64 additions and 20 deletions
12
lib/read.bat
12
lib/read.bat
|
@ -1,6 +1,6 @@
|
|||
@ECHO OFF
|
||||
SETLOCAL
|
||||
SET /P LINE=
|
||||
SET /P DUM=%LINE%<NUL
|
||||
ENDLOCAL
|
||||
EXIT /B 0
|
||||
@echo off
|
||||
setlocal
|
||||
set /p LINE=<CON >CON
|
||||
set /p DUM="%LINE%"<NUL
|
||||
endlocal
|
||||
exit /b 0
|
||||
|
|
|
@ -15,16 +15,7 @@ var promptText = '> ',
|
|||
stdin = process.stdin,
|
||||
stdout = process.stdout,
|
||||
buffer = new Buffer(BUF_SIZE),
|
||||
useShell = true;
|
||||
|
||||
function _readlineShell() {
|
||||
// Win isn't supported by sync-exec v0.3.2
|
||||
var command = /* require('os').platform() === 'win32' ?
|
||||
'cmd "' + __dirname + '\\read.bat' : */
|
||||
'sh "' + __dirname + '/read.sh"',
|
||||
resExec = require('sync-exec')(command); // instead of execSync (node v0.12+)
|
||||
return resExec.status === 0 && !resExec.stderr ? (resExec.stdout + '') : false;
|
||||
}
|
||||
useShell = true, tempdir;
|
||||
|
||||
function _readlineSync(display) {
|
||||
var input = '', rsize, err;
|
||||
|
@ -66,6 +57,62 @@ function _readlineSync(display) {
|
|||
return input.trim();
|
||||
}
|
||||
|
||||
function _readlineShell() {
|
||||
// piping via files instead of execSync (node v0.12+)
|
||||
var shellPath, args, shellStdout,
|
||||
pathStdout = getTempfile('readline-sync.stdout'),
|
||||
pathStatus = getTempfile('readline-sync.status'),
|
||||
pathDone = getTempfile('readline-sync.done');
|
||||
|
||||
if (process.platform === 'win32') {
|
||||
// The quote (") is escaped by node before parsed by shell. Then use ENV{Q}.
|
||||
shellPath = 'cmd.exe';
|
||||
args = ['/V:ON', '/S', '/C', '%Q%' + __dirname + '\\read.bat%Q% >%Q%' + pathStdout +
|
||||
'%Q% & (ECHO !ERRORLEVEL!)>%Q%' + pathStatus + '%Q% & (ECHO 1)>%Q%' + pathDone + '%Q%'];
|
||||
} else {
|
||||
shellPath = '/bin/sh';
|
||||
args = ['-c', '(' + shellPath + ' "' + __dirname + '/read.sh") >"' + pathStdout +
|
||||
'"; echo $? >"' + pathStatus + '"; echo 1 >"' + pathDone + '"'];
|
||||
}
|
||||
|
||||
stdin.pause(); // re-start in child process
|
||||
require('child_process').execFile(shellPath, args, {env: {Q: '"'}});
|
||||
|
||||
while (true) {
|
||||
if (fs.readFileSync(pathDone, {encoding: encoding}).trim() === '1') { break; }
|
||||
}
|
||||
if (fs.readFileSync(pathStatus, {encoding: encoding}).trim() === '0') {
|
||||
shellStdout = fs.readFileSync(pathStdout, {encoding: encoding});
|
||||
}
|
||||
|
||||
fs.unlinkSync(pathStdout);
|
||||
fs.unlinkSync(pathStatus);
|
||||
fs.unlinkSync(pathDone);
|
||||
return shellStdout;
|
||||
}
|
||||
|
||||
function getTempfile(name) {
|
||||
var path = require('path'), filepath, suffix = '', fd;
|
||||
tempdir = tempdir || require('os').tmpdir();
|
||||
|
||||
while (true) {
|
||||
filepath = path.join(tempdir, name + suffix);
|
||||
try {
|
||||
fd = fs.openSync(filepath, 'wx');
|
||||
} catch (e) {
|
||||
if (e.code === 'EEXIST') {
|
||||
suffix++;
|
||||
continue;
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
fs.closeSync(fd);
|
||||
break;
|
||||
}
|
||||
return filepath;
|
||||
}
|
||||
|
||||
// for dev
|
||||
exports.useShellSet = function(use) { useShell = use; };
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"name": "readline-sync",
|
||||
"description": "Synchronous Readline",
|
||||
"version": "0.2.3",
|
||||
"version": "0.2.4",
|
||||
"homepage": "https://github.com/anseki/readline-sync",
|
||||
"author": {
|
||||
"name": "anseki"
|
||||
|
@ -23,9 +23,6 @@
|
|||
"engines": {
|
||||
"node": ">= 0.8.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"sync-exec": "~0.3.2"
|
||||
},
|
||||
"keywords": [
|
||||
"readline",
|
||||
"synchronous",
|
||||
|
|
Loading…
Reference in a new issue