2013-08-29 12:55:23 +02:00
|
|
|
/*
|
|
|
|
* readlineSync
|
|
|
|
* https://github.com/anseki/readline-sync
|
|
|
|
*
|
2015-01-26 17:11:25 +01:00
|
|
|
* Copyright (c) 2015 anseki
|
2013-08-29 12:55:23 +02:00
|
|
|
* Licensed under the MIT license.
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2014-07-13 06:55:17 +02:00
|
|
|
var
|
2015-02-11 23:39:59 +01:00
|
|
|
IS_WIN = process.platform === 'win32',
|
|
|
|
SHELL_PATH = IS_WIN ? 'cmd.exe' : '/bin/sh',
|
2015-03-03 18:30:41 +01:00
|
|
|
SHELL_CMD = __dirname + (IS_WIN ? '\\read.bat' : '/read.sh'),
|
2015-02-11 23:39:59 +01:00
|
|
|
|
2014-07-13 06:55:17 +02:00
|
|
|
ALGORITHM_CIPHER = 'aes-256-cbc',
|
|
|
|
ALGORITHM_HASH = 'sha256',
|
|
|
|
|
2013-08-29 19:26:22 +02:00
|
|
|
fs = require('fs'),
|
2015-02-11 20:44:05 +01:00
|
|
|
childProc = require('child_process'),
|
2013-08-29 19:26:22 +02:00
|
|
|
stdin = process.stdin,
|
|
|
|
stdout = process.stdout,
|
2015-02-11 20:44:05 +01:00
|
|
|
|
|
|
|
promptText = '> ',
|
|
|
|
encoding = 'utf8',
|
2015-02-22 14:23:46 +01:00
|
|
|
bufSize = 1024,
|
2015-03-02 14:44:36 +01:00
|
|
|
useShell = false, print, tempdir, salt = 0;
|
2014-06-27 03:06:29 +02:00
|
|
|
|
2014-07-12 00:37:25 +02:00
|
|
|
function _readlineSync(display, options) {
|
2015-03-03 18:30:41 +01:00
|
|
|
var input = '', res, buffer = new Buffer(bufSize), rsize;
|
2015-03-02 14:44:36 +01:00
|
|
|
options = options || {};
|
2013-08-29 19:26:22 +02:00
|
|
|
|
2014-07-13 14:30:04 +02:00
|
|
|
if (display !== '') { // null and undefined were excluded.
|
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
|
|
|
|
2015-03-02 14:44:36 +01:00
|
|
|
if (useShell || options.noEchoBack) { // Try reading via shell
|
2013-12-18 03:51:32 +01:00
|
|
|
|
2015-03-03 18:30:41 +01:00
|
|
|
res = _readlineShell(options);
|
|
|
|
if (res.error) {
|
2014-07-12 01:16:11 +02:00
|
|
|
if (display !== '') { stdout.write('\n', encoding); } // Return from prompt line.
|
2015-03-03 18:30:41 +01:00
|
|
|
throw newError(res.props);
|
2014-07-12 00:37:25 +02:00
|
|
|
}
|
2015-03-03 18:30:41 +01:00
|
|
|
input = res.stdout;
|
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 {
|
2015-02-22 12:57:07 +01:00
|
|
|
rsize = fs.readSync(stdin.fd, buffer, 0, bufSize);
|
2014-07-12 00:37:25 +02:00
|
|
|
} catch (e) {
|
2015-03-03 18:30:41 +01:00
|
|
|
if (e.code === 'EOF') { break; }
|
2014-07-12 00:37:25 +02:00
|
|
|
|
2015-03-02 14:44:36 +01:00
|
|
|
// Try reading via shell
|
2015-03-03 18:30:41 +01:00
|
|
|
res = _readlineShell(options);
|
|
|
|
if (res.error) {
|
|
|
|
if (display !== '') { stdout.write('\n', encoding); } // Return from prompt line.
|
|
|
|
throw newError(res.props);
|
2014-07-12 00:37:25 +02:00
|
|
|
}
|
2015-03-03 18:30:41 +01:00
|
|
|
input += res.stdout;
|
|
|
|
break;
|
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
|
|
|
}
|
|
|
|
|
2015-03-02 14:44:36 +01:00
|
|
|
return options.noTrim ? input.replace(/[\r\n]+$/, '') : input.trim();
|
2014-06-27 03:06:29 +02:00
|
|
|
}
|
|
|
|
|
2015-03-03 18:30:41 +01:00
|
|
|
function newError(props) {
|
|
|
|
var err = new Error('The platform doesn\'t support interactive reading from stdin'),
|
|
|
|
key;
|
|
|
|
if (props) {
|
|
|
|
for (key in props) {
|
|
|
|
if (props.hasOwnProperty(key)) { err[key] = props[key]; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2015-03-02 14:44:36 +01:00
|
|
|
function _readlineShell(options) {
|
2015-03-03 18:30:41 +01:00
|
|
|
var args = [], res = {},
|
2015-03-02 14:44:36 +01:00
|
|
|
execOptions = {
|
2015-02-11 23:39:59 +01:00
|
|
|
env: process.env,
|
|
|
|
stdio: [stdin], // ScriptPW needs piped stdin
|
|
|
|
encoding: encoding
|
2015-03-02 14:44:36 +01:00
|
|
|
};
|
2015-02-11 23:39:59 +01:00
|
|
|
|
2015-03-02 14:44:36 +01:00
|
|
|
if (options.noEchoBack) {
|
|
|
|
args.push('noechoback');
|
2015-02-11 23:39:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
stdin.pause(); // re-start in child process
|
|
|
|
if (childProc.execFileSync) {
|
2015-03-03 18:30:41 +01:00
|
|
|
try {
|
|
|
|
res.stdout = childProc.execFileSync(SHELL_PATH,
|
|
|
|
(IS_WIN ? ['/C', SHELL_CMD] : [SHELL_CMD]).concat(args), execOptions);
|
|
|
|
} catch (e) { // non-zero exit code
|
|
|
|
res = {error: true, props: {
|
|
|
|
method: 'execFileSync',
|
|
|
|
command: SHELL_CMD, args: args, stderr: e.stderr.trim()
|
|
|
|
}};
|
|
|
|
}
|
2015-02-11 23:39:59 +01:00
|
|
|
} else {
|
2015-03-03 18:30:41 +01:00
|
|
|
res = _execSyncByFile(args, execOptions);
|
2015-02-11 23:39:59 +01:00
|
|
|
}
|
2015-03-03 18:30:41 +01:00
|
|
|
if (!res.error) { res.stdout = res.stdout.replace(/^'|'$/g, ''); }
|
2015-02-11 23:39:59 +01:00
|
|
|
|
2015-03-03 18:30:41 +01:00
|
|
|
return res;
|
2015-02-11 23:39:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// piping via files (node v0.10-)
|
2015-03-02 14:44:36 +01:00
|
|
|
function _execSyncByFile(args, execOptions) {
|
2015-02-11 23:39:59 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-03-03 18:30:41 +01:00
|
|
|
var cmdArgs, res = {},
|
2014-06-29 00:00:15 +02:00
|
|
|
pathStdout = getTempfile('readline-sync.stdout'),
|
2015-03-03 18:30:41 +01:00
|
|
|
pathStderr = getTempfile('readline-sync.stderr'),
|
2014-06-29 00:00:15 +02:00
|
|
|
pathStatus = getTempfile('readline-sync.status'),
|
2014-07-12 00:37:25 +02:00
|
|
|
pathDone = getTempfile('readline-sync.done'),
|
2014-07-13 06:55:17 +02:00
|
|
|
crypto = require('crypto'), shasum, decipher, password;
|
|
|
|
|
|
|
|
shasum = crypto.createHash(ALGORITHM_HASH);
|
|
|
|
shasum.update('' + process.pid + (salt++) + Math.random());
|
|
|
|
password = shasum.digest('hex');
|
|
|
|
decipher = crypto.createDecipher(ALGORITHM_CIPHER, password);
|
2014-06-29 00:00:15 +02:00
|
|
|
|
2015-03-02 14:44:36 +01:00
|
|
|
if (IS_WIN) {
|
|
|
|
// The quote (") is escaped by node before parsed by shell. Then use ENV{Q}.
|
|
|
|
process.env.Q = '"';
|
2015-03-03 18:30:41 +01:00
|
|
|
// `()` for ignore space by echo
|
|
|
|
cmdArgs = ['/V:ON', '/S', '/C',
|
|
|
|
'(' + SHELL_PATH + ' /V:ON /S /C %Q%%Q%' + SHELL_CMD + '%Q% ' + args.join(' ') +
|
|
|
|
' & (echo !ERRORLEVEL!)>%Q%' + pathStatus + '%Q%%Q%) 2>%Q%' + pathStderr + '%Q%' +
|
2015-03-02 14:44:36 +01:00
|
|
|
' |%Q%' + process.execPath + '%Q% %Q%' + __dirname + '\\encrypt.js%Q%' +
|
|
|
|
' %Q%' + ALGORITHM_CIPHER + '%Q% %Q%' + password + '%Q%' +
|
2015-03-03 18:30:41 +01:00
|
|
|
' >%Q%' + pathStdout + '%Q%' +
|
|
|
|
' & (echo 1)>%Q%' + pathDone + '%Q%'];
|
2015-03-02 14:44:36 +01:00
|
|
|
} else {
|
2015-03-03 18:30:41 +01:00
|
|
|
cmdArgs = ['-c',
|
|
|
|
'(' + SHELL_PATH + ' "' + SHELL_CMD + '" ' + args.join(' ') +
|
|
|
|
'; echo $?>"' + pathStatus + '") 2>"' + pathStderr + '"' +
|
2015-03-02 14:44:36 +01:00
|
|
|
' |"' + process.execPath + '" "' + __dirname + '/encrypt.js"' +
|
|
|
|
' "' + ALGORITHM_CIPHER + '" "' + password + '"' +
|
2015-03-03 18:30:41 +01:00
|
|
|
' >"' + pathStdout + '"' +
|
|
|
|
'; echo 1 >"' + pathDone + '"'];
|
2015-03-02 14:44:36 +01:00
|
|
|
}
|
2015-03-03 18:30:41 +01:00
|
|
|
childProc.spawn(SHELL_PATH, cmdArgs, execOptions);
|
2014-06-29 00:00:15 +02:00
|
|
|
|
2014-07-13 08:32:02 +02:00
|
|
|
while (fs.readFileSync(pathDone, {encoding: encoding}).trim() !== '1') {}
|
2014-06-29 00:00:15 +02:00
|
|
|
if (fs.readFileSync(pathStatus, {encoding: encoding}).trim() === '0') {
|
2015-03-03 18:30:41 +01:00
|
|
|
res.stdout =
|
2014-07-13 06:55:17 +02:00
|
|
|
decipher.update(fs.readFileSync(pathStdout, {encoding: 'binary'}), 'hex', encoding) +
|
|
|
|
decipher.final(encoding);
|
2015-03-03 18:30:41 +01:00
|
|
|
} else {
|
|
|
|
res = {error: true, props: {
|
|
|
|
method: '_execSyncByFile',
|
|
|
|
command: SHELL_CMD, args: args,
|
|
|
|
stderr: fs.readFileSync(pathStderr, {encoding: encoding}).trim()
|
|
|
|
}};
|
2014-06-29 00:00:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fs.unlinkSync(pathStdout);
|
2015-03-03 18:30:41 +01:00
|
|
|
fs.unlinkSync(pathStderr);
|
2014-06-29 00:00:15 +02:00
|
|
|
fs.unlinkSync(pathStatus);
|
|
|
|
fs.unlinkSync(pathDone);
|
|
|
|
|
2015-03-03 18:30:41 +01:00
|
|
|
return res;
|
2014-06-29 00:00:15 +02:00
|
|
|
}
|
|
|
|
|
2014-06-27 03:06:29 +02:00
|
|
|
// for dev
|
2015-03-02 14:44:36 +01:00
|
|
|
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 */
|
2014-07-13 14:30:04 +02:00
|
|
|
if (newPrompt != null) {
|
2014-07-12 01:16:11 +02:00
|
|
|
/* jshint eqnull:false */
|
2014-07-13 14:30:04 +02:00
|
|
|
promptText = newPrompt;
|
|
|
|
}
|
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
|
|
|
};
|
|
|
|
|
2015-02-22 12:57:07 +01:00
|
|
|
exports.setBufferSize = function(newBufSize) {
|
|
|
|
if (typeof newBufSize === 'number') {
|
|
|
|
bufSize = newBufSize;
|
|
|
|
}
|
|
|
|
return bufSize;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
};
|
2015-03-01 15:46:52 +01:00
|
|
|
|
2015-03-04 12:58:55 +01:00
|
|
|
exports.keyIn = function(query) {
|
2015-03-01 15:46:52 +01:00
|
|
|
return _readlineSync(
|
|
|
|
/* jshint eqnull:true */
|
2015-03-04 12:58:55 +01:00
|
|
|
query != null ? query : '',
|
2015-03-01 15:46:52 +01:00
|
|
|
/* jshint eqnull:false */
|
|
|
|
{keyIn: true});
|
|
|
|
};
|