From 7afb24a972ab296b86da048bd6f387231e5a9197 Mon Sep 17 00:00:00 2001 From: anseki Date: Sun, 29 Jun 2014 07:00:15 +0900 Subject: [PATCH] Add reading shell through files (pipe). --- lib/read.bat | 12 ++++---- lib/readline-sync.js | 67 +++++++++++++++++++++++++++++++++++++------- package.json | 5 +--- 3 files changed, 64 insertions(+), 20 deletions(-) diff --git a/lib/read.bat b/lib/read.bat index 942cfba..9d2b9e6 100644 --- a/lib/read.bat +++ b/lib/read.bat @@ -1,6 +1,6 @@ -@ECHO OFF -SETLOCAL -SET /P LINE= -SET /P DUM=%LINE%CON +set /p DUM="%LINE%"%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; }; diff --git a/package.json b/package.json index 1a8c30a..f3653b4 100644 --- a/package.json +++ b/package.json @@ -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",