Add reading shell through files (pipe).

This commit is contained in:
anseki 2014-06-29 07:00:15 +09:00
parent bf3341383d
commit 7afb24a972
3 changed files with 64 additions and 20 deletions

View file

@ -1,6 +1,6 @@
@ECHO OFF @echo off
SETLOCAL setlocal
SET /P LINE= set /p LINE=<CON >CON
SET /P DUM=%LINE%<NUL set /p DUM="%LINE%"<NUL
ENDLOCAL endlocal
EXIT /B 0 exit /b 0

View file

@ -15,16 +15,7 @@ var promptText = '> ',
stdin = process.stdin, stdin = process.stdin,
stdout = process.stdout, stdout = process.stdout,
buffer = new Buffer(BUF_SIZE), buffer = new Buffer(BUF_SIZE),
useShell = true; useShell = true, tempdir;
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;
}
function _readlineSync(display) { function _readlineSync(display) {
var input = '', rsize, err; var input = '', rsize, err;
@ -66,6 +57,62 @@ function _readlineSync(display) {
return input.trim(); 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 // for dev
exports.useShellSet = function(use) { useShell = use; }; exports.useShellSet = function(use) { useShell = use; };

View file

@ -1,7 +1,7 @@
{ {
"name": "readline-sync", "name": "readline-sync",
"description": "Synchronous Readline", "description": "Synchronous Readline",
"version": "0.2.3", "version": "0.2.4",
"homepage": "https://github.com/anseki/readline-sync", "homepage": "https://github.com/anseki/readline-sync",
"author": { "author": {
"name": "anseki" "name": "anseki"
@ -23,9 +23,6 @@
"engines": { "engines": {
"node": ">= 0.8.0" "node": ">= 0.8.0"
}, },
"dependencies": {
"sync-exec": "~0.3.2"
},
"keywords": [ "keywords": [
"readline", "readline",
"synchronous", "synchronous",