2013-08-29 12:55:23 +02:00
|
|
|
/*
|
|
|
|
* readlineSync
|
|
|
|
* https://github.com/anseki/readline-sync
|
|
|
|
*
|
2014-06-27 03:06:29 +02:00
|
|
|
* Copyright (c) 2014 anseki
|
2013-08-29 12:55:23 +02:00
|
|
|
* Licensed under the MIT license.
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2013-08-29 19:26:22 +02:00
|
|
|
var promptText = '> ',
|
|
|
|
encoding = 'utf8',
|
|
|
|
BUF_SIZE = 256,
|
|
|
|
fs = require('fs'),
|
|
|
|
stdin = process.stdin,
|
|
|
|
stdout = process.stdout,
|
2014-06-27 03:06:29 +02:00
|
|
|
buffer = new Buffer(BUF_SIZE),
|
2014-07-11 23:25:59 +02:00
|
|
|
useShell = true, print, tempdir;
|
2014-06-27 03:06:29 +02:00
|
|
|
|
2014-07-12 00:37:25 +02:00
|
|
|
function _readlineSync(display, options) {
|
2013-12-18 03:51:32 +01:00
|
|
|
var input = '', rsize, err;
|
2013-08-29 19:26:22 +02:00
|
|
|
|
2014-07-12 01:16:11 +02:00
|
|
|
if (display !== '') {
|
2014-07-11 23:25:59 +02:00
|
|
|
if (typeof print === 'function') { print(display, encoding); }
|
2014-07-12 01:16:11 +02:00
|
|
|
stdout.write(display + '', encoding);
|
2014-07-11 23:25:59 +02:00
|
|
|
}
|
2013-08-29 12:55:23 +02:00
|
|
|
|
2014-07-12 00:37:25 +02:00
|
|
|
if (options && options.noEchoBack) { // Try reading via shell
|
2013-12-18 03:51:32 +01:00
|
|
|
|
2014-07-12 00:37:25 +02:00
|
|
|
input = _readlineShell(true);
|
|
|
|
if (typeof input !== 'string') {
|
2014-07-12 01:16:11 +02:00
|
|
|
if (display !== '') { stdout.write('\n', encoding); } // Return from prompt line.
|
2014-07-12 00:37:25 +02:00
|
|
|
throw new Error('Can\'t read via shell');
|
|
|
|
}
|
2014-06-27 03:06:29 +02:00
|
|
|
|
2014-07-12 00:37:25 +02:00
|
|
|
} else {
|
2014-06-27 03:06:29 +02:00
|
|
|
|
2014-07-12 00:37:25 +02:00
|
|
|
stdin.resume();
|
|
|
|
while (true) {
|
|
|
|
rsize = 0;
|
|
|
|
|
|
|
|
try {
|
|
|
|
rsize = fs.readSync(stdin.fd, buffer, 0, BUF_SIZE);
|
|
|
|
} catch (e) {
|
|
|
|
if (e.code === 'EOF') { break; } // pipe
|
|
|
|
|
|
|
|
if (useShell) {
|
|
|
|
// Try reading via shell
|
|
|
|
input = _readlineShell();
|
|
|
|
if (typeof input === 'string') { break; }
|
|
|
|
}
|
|
|
|
|
|
|
|
// Give up...
|
|
|
|
if (e.code === 'EAGAIN') { // EAGAIN, resource temporarily unavailable
|
|
|
|
// util can't inherit Error.
|
|
|
|
err = new Error('The platform doesn\'t support interactive reading from stdin');
|
|
|
|
err.errno = e.errno;
|
|
|
|
err.code = e.code;
|
|
|
|
}
|
2014-07-12 01:16:11 +02:00
|
|
|
if (display !== '') { stdout.write('\n', encoding); } // Return from prompt line.
|
2014-07-12 00:37:25 +02:00
|
|
|
throw err || e;
|
2013-12-17 18:18:39 +01:00
|
|
|
}
|
2014-07-12 00:37:25 +02:00
|
|
|
|
|
|
|
if (rsize === 0) { break; }
|
|
|
|
input += buffer.toString(encoding, 0, rsize);
|
|
|
|
if (/[\r\n]$/.test(input)) { break; }
|
2013-12-17 18:18:39 +01:00
|
|
|
}
|
2014-07-12 00:37:25 +02:00
|
|
|
stdin.pause();
|
2013-12-18 03:51:32 +01:00
|
|
|
|
2013-08-29 12:55:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return input.trim();
|
2014-06-27 03:06:29 +02:00
|
|
|
}
|
|
|
|
|
2014-07-12 00:37:25 +02:00
|
|
|
function _readlineShell(noEchoBack) {
|
2014-06-29 00:00:15 +02:00
|
|
|
// piping via files instead of execSync (node v0.12+)
|
|
|
|
var shellPath, args, shellStdout,
|
|
|
|
pathStdout = getTempfile('readline-sync.stdout'),
|
|
|
|
pathStatus = getTempfile('readline-sync.status'),
|
2014-07-12 00:37:25 +02:00
|
|
|
pathDone = getTempfile('readline-sync.done'),
|
|
|
|
optEchoBack = noEchoBack ? ' noechoback' : '';
|
2014-06-29 00:00:15 +02:00
|
|
|
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
// The quote (") is escaped by node before parsed by shell. Then use ENV{Q}.
|
|
|
|
shellPath = 'cmd.exe';
|
2014-07-12 00:37:25 +02:00
|
|
|
args = ['/V:ON', '/S', '/C',
|
|
|
|
'%Q%' + __dirname + '\\read.bat%Q%' + optEchoBack + ' >%Q%' + pathStdout +
|
2014-06-29 10:41:12 +02:00
|
|
|
'%Q% & (echo !ERRORLEVEL!)>%Q%' + pathStatus + '%Q% & (echo 1)>%Q%' + pathDone + '%Q%'];
|
2014-06-29 00:00:15 +02:00
|
|
|
} else {
|
|
|
|
shellPath = '/bin/sh';
|
2014-07-12 00:37:25 +02:00
|
|
|
args = ['-c', '(' + shellPath + ' "' + __dirname + '/read.sh"' + optEchoBack + ') >"' + pathStdout +
|
2014-06-29 00:00:15 +02:00
|
|
|
'"; 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;
|
|
|
|
}
|
|
|
|
|
2014-06-27 03:06:29 +02:00
|
|
|
// for dev
|
|
|
|
exports.useShellSet = function(use) { useShell = use; };
|
2013-08-29 19:26:22 +02:00
|
|
|
|
2014-07-11 23:25:59 +02:00
|
|
|
exports.setPrint = function(fnc) { print = fnc; };
|
|
|
|
|
2013-08-29 19:26:22 +02:00
|
|
|
exports.setPrompt = function(newPrompt) {
|
2014-07-12 01:16:11 +02:00
|
|
|
/* jshint eqnull:true */
|
|
|
|
promptText = newPrompt != null ? newPrompt : '';
|
|
|
|
/* jshint eqnull:false */
|
2014-07-12 01:56:21 +02:00
|
|
|
return promptText;
|
2013-08-29 19:26:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.setEncoding = function(newEncoding) {
|
|
|
|
if (typeof newEncoding === 'string') {
|
|
|
|
encoding = newEncoding;
|
|
|
|
}
|
2014-07-12 01:56:21 +02:00
|
|
|
return encoding;
|
2013-08-29 19:26:22 +02:00
|
|
|
};
|
|
|
|
|
2014-07-12 00:37:25 +02:00
|
|
|
exports.prompt = function(options) {
|
|
|
|
return _readlineSync(promptText, options);
|
2013-08-29 19:26:22 +02:00
|
|
|
};
|
|
|
|
|
2014-07-12 00:37:25 +02:00
|
|
|
exports.question = function(query, options) {
|
2014-07-12 01:16:11 +02:00
|
|
|
return _readlineSync(
|
|
|
|
/* jshint eqnull:true */
|
|
|
|
query != null ? query : '',
|
|
|
|
/* jshint eqnull:false */
|
|
|
|
options);
|
2013-08-29 19:26:22 +02:00
|
|
|
};
|