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',
|
2015-03-25 08:56:43 +01:00
|
|
|
|
2014-07-13 06:55:17 +02:00
|
|
|
ALGORITHM_CIPHER = 'aes-256-cbc',
|
|
|
|
ALGORITHM_HASH = 'sha256',
|
2015-03-06 07:23:32 +01:00
|
|
|
DEFAULT_ERR_MSG = 'The platform doesn\'t support interactive reading',
|
2014-07-13 06:55:17 +02:00
|
|
|
|
2013-08-29 19:26:22 +02:00
|
|
|
fs = require('fs'),
|
2015-03-20 04:03:58 +01:00
|
|
|
TTY = process.binding('tty_wrap').TTY,
|
2015-02-11 20:44:05 +01:00
|
|
|
childProc = require('child_process'),
|
|
|
|
|
2015-04-04 17:37:42 +02:00
|
|
|
defaultOptions = {
|
2015-04-07 11:38:14 +02:00
|
|
|
prompt: '> ',
|
|
|
|
hideEchoBack: false,
|
|
|
|
mask: '*',
|
|
|
|
limit: [],
|
|
|
|
limitMessage: 'Input another, please.${( [)limit(])}',
|
|
|
|
caseSensitive: false,
|
|
|
|
keepWhitespace: false,
|
|
|
|
encoding: 'utf8',
|
|
|
|
bufferSize: 1024,
|
|
|
|
print: void 0,
|
|
|
|
trueValue: [],
|
|
|
|
falseValue: []
|
2015-04-04 17:37:42 +02:00
|
|
|
},
|
2015-03-25 08:56:43 +01:00
|
|
|
useExt = false,
|
2014-06-27 03:06:29 +02:00
|
|
|
|
2015-03-20 04:03:58 +01:00
|
|
|
fdR = 'none', fdW, ttyR, isRawMode = false,
|
2015-03-27 12:25:59 +01:00
|
|
|
extHostPath, extHostArgs, tempdir, salt = 0;
|
2013-08-29 12:55:23 +02:00
|
|
|
|
2015-03-27 12:25:59 +01:00
|
|
|
/*
|
2015-04-07 11:38:14 +02:00
|
|
|
display: string
|
|
|
|
keyIn: boolean
|
|
|
|
hideEchoBack: boolean
|
|
|
|
mask: string
|
|
|
|
limit: string (pattern)
|
|
|
|
caseSensitive: boolean
|
|
|
|
keepWhitespace: boolean
|
2015-04-05 08:39:16 +02:00
|
|
|
encoding, bufferSize, print
|
2015-03-27 12:25:59 +01:00
|
|
|
*/
|
2015-04-05 13:05:23 +02:00
|
|
|
function _readlineSync(options) {
|
2015-04-05 11:11:30 +02:00
|
|
|
var input = '', displaySave = options.display,
|
2015-04-07 11:38:14 +02:00
|
|
|
silent = !options.display &&
|
|
|
|
options.keyIn && options.hideEchoBack && !options.mask;
|
2013-12-18 03:51:32 +01:00
|
|
|
|
2015-03-25 08:56:43 +01:00
|
|
|
function tryExt() {
|
|
|
|
var res = readlineExt(options);
|
2015-03-07 16:19:40 +01:00
|
|
|
if (res.error) { throw res.error; }
|
2015-03-20 04:03:58 +01:00
|
|
|
return res.input;
|
|
|
|
}
|
|
|
|
|
2015-03-31 13:27:02 +02:00
|
|
|
(function() { // open TTY
|
2015-03-20 04:03:58 +01:00
|
|
|
var fsB, constants;
|
|
|
|
|
|
|
|
function getFsB() {
|
|
|
|
if (!fsB) {
|
|
|
|
fsB = process.binding('fs'); // For raw device path
|
|
|
|
constants = process.binding('constants');
|
|
|
|
}
|
|
|
|
return fsB;
|
|
|
|
}
|
2015-03-07 16:19:40 +01:00
|
|
|
|
2015-03-20 04:03:58 +01:00
|
|
|
if (typeof fdR !== 'string') { return; }
|
|
|
|
fdR = null;
|
|
|
|
|
|
|
|
if (IS_WIN) {
|
|
|
|
if (process.stdin.isTTY) {
|
|
|
|
fdR = process.stdin.fd;
|
|
|
|
ttyR = process.stdin._handle;
|
|
|
|
} else {
|
2015-03-07 16:19:40 +01:00
|
|
|
try {
|
2015-03-20 04:03:58 +01:00
|
|
|
// The stream by fs.openSync('\\\\.\\CON', 'r') can't switch to raw mode.
|
|
|
|
// 'CONIN$' might fail on XP, 2000, 7 (x86).
|
|
|
|
fdR = getFsB().open('CONIN$', constants.O_RDWR, parseInt('0666', 8));
|
|
|
|
ttyR = new TTY(fdR, true);
|
2015-03-07 16:19:40 +01:00
|
|
|
} catch (e) {}
|
2015-03-20 04:03:58 +01:00
|
|
|
}
|
2015-03-07 16:19:40 +01:00
|
|
|
|
2015-03-20 04:03:58 +01:00
|
|
|
if (process.stdout.isTTY) {
|
|
|
|
fdW = process.stdout.fd;
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
fdW = fs.openSync('\\\\.\\CON', 'w');
|
|
|
|
} catch (e) {}
|
|
|
|
if (typeof fdW !== 'number') { // Retry
|
2015-03-07 16:19:40 +01:00
|
|
|
try {
|
2015-03-20 04:03:58 +01:00
|
|
|
fdW = getFsB().open('CONOUT$', constants.O_RDWR, parseInt('0666', 8));
|
2015-03-07 16:19:40 +01:00
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
}
|
2014-07-12 00:37:25 +02:00
|
|
|
|
2015-03-11 09:06:27 +01:00
|
|
|
} else {
|
2015-03-20 04:03:58 +01:00
|
|
|
if (process.stdin.isTTY) {
|
|
|
|
try {
|
|
|
|
fdR = fs.openSync('/dev/tty', 'r'); // device file, not process.stdin
|
|
|
|
ttyR = process.stdin._handle;
|
|
|
|
} catch (e) {}
|
|
|
|
} else {
|
|
|
|
// Node v0.12 read() fails.
|
|
|
|
try {
|
|
|
|
fdR = fs.openSync('/dev/tty', 'r');
|
|
|
|
ttyR = new TTY(fdR, false);
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (process.stdout.isTTY) {
|
|
|
|
fdW = process.stdout.fd;
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
fdW = fs.openSync('/dev/tty', 'w');
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
2015-03-07 16:19:40 +01:00
|
|
|
}
|
2015-03-20 04:03:58 +01:00
|
|
|
})();
|
|
|
|
|
|
|
|
(function() { // try read
|
2015-04-03 18:14:55 +02:00
|
|
|
var atEol, limit,
|
2015-04-07 11:38:14 +02:00
|
|
|
isCooked = !options.hideEchoBack && !options.keyIn,
|
2015-04-01 09:34:31 +02:00
|
|
|
buffer, reqSize, readSize, chunk, line;
|
2015-03-25 13:22:44 +01:00
|
|
|
|
|
|
|
// Node v0.10- returns an error if same mode is set.
|
|
|
|
function setRawMode(mode) {
|
|
|
|
if (mode === isRawMode) { return true; }
|
|
|
|
if (ttyR.setRawMode(mode) !== 0) { return false; }
|
|
|
|
isRawMode = mode;
|
|
|
|
return true;
|
|
|
|
}
|
2015-03-07 16:19:40 +01:00
|
|
|
|
2015-04-04 17:37:42 +02:00
|
|
|
if (useExt || !ttyR ||
|
|
|
|
typeof fdW !== 'number' && (options.display || !isCooked)) {
|
2015-03-25 08:56:43 +01:00
|
|
|
input = tryExt();
|
2015-03-20 04:03:58 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-31 13:27:02 +02:00
|
|
|
if (options.display) {
|
|
|
|
fs.writeSync(fdW, options.display);
|
2015-03-20 04:03:58 +01:00
|
|
|
options.display = '';
|
|
|
|
}
|
|
|
|
|
2015-03-29 16:45:55 +02:00
|
|
|
if (!setRawMode(!isCooked)) {
|
2015-03-25 08:56:43 +01:00
|
|
|
input = tryExt();
|
2015-03-20 04:03:58 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-04-05 08:39:16 +02:00
|
|
|
buffer = new Buffer((reqSize = options.keyIn ? 1 : options.bufferSize));
|
2015-03-20 04:03:58 +01:00
|
|
|
|
2015-04-03 18:14:55 +02:00
|
|
|
if (options.keyIn && options.limit) {
|
2015-04-04 17:37:42 +02:00
|
|
|
limit = new RegExp('[^' + options.limit + ']',
|
|
|
|
'g' + (options.caseSensitive ? '' : 'i'));
|
2015-03-31 13:27:02 +02:00
|
|
|
}
|
|
|
|
|
2015-03-20 04:03:58 +01:00
|
|
|
while (true) {
|
|
|
|
readSize = 0;
|
|
|
|
try {
|
|
|
|
readSize = fs.readSync(fdR, buffer, 0, reqSize);
|
|
|
|
} catch (e) {
|
2015-03-29 16:45:55 +02:00
|
|
|
if (e.code !== 'EOF') {
|
|
|
|
setRawMode(false);
|
|
|
|
input += tryExt();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2015-04-04 17:37:42 +02:00
|
|
|
chunk = readSize > 0 ? buffer.toString(options.encoding, 0, readSize) : '\n';
|
2015-03-29 16:45:55 +02:00
|
|
|
|
2015-04-04 17:37:42 +02:00
|
|
|
if (chunk &&
|
|
|
|
typeof(line = (chunk.match(/^(.*?)[\r\n]/) || [])[1]) === 'string') {
|
2015-03-29 16:45:55 +02:00
|
|
|
chunk = line;
|
2015-04-01 09:34:31 +02:00
|
|
|
atEol = true;
|
2013-12-17 18:18:39 +01:00
|
|
|
}
|
2014-07-12 00:37:25 +02:00
|
|
|
|
2015-04-03 18:14:55 +02:00
|
|
|
// other ctrl-chars
|
|
|
|
if (chunk) { chunk = chunk.replace(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/g, ''); }
|
|
|
|
if (chunk && limit) { chunk = chunk.replace(limit, ''); }
|
2015-03-20 04:03:58 +01:00
|
|
|
|
2015-04-03 18:14:55 +02:00
|
|
|
if (chunk) {
|
|
|
|
if (!isCooked) {
|
2015-04-07 11:38:14 +02:00
|
|
|
if (!options.hideEchoBack) {
|
2015-04-03 18:14:55 +02:00
|
|
|
fs.writeSync(fdW, chunk);
|
|
|
|
} else if (options.mask) {
|
|
|
|
fs.writeSync(fdW, (new Array(chunk.length + 1)).join(options.mask));
|
|
|
|
}
|
2015-03-22 06:11:55 +01:00
|
|
|
}
|
2015-04-03 18:14:55 +02:00
|
|
|
input += chunk;
|
2015-03-20 04:03:58 +01:00
|
|
|
}
|
|
|
|
|
2015-04-01 09:34:31 +02:00
|
|
|
if (!options.keyIn && atEol ||
|
|
|
|
options.keyIn && input.length >= reqSize) { break; }
|
2013-12-17 18:18:39 +01:00
|
|
|
}
|
2013-12-18 03:51:32 +01:00
|
|
|
|
2015-03-31 13:27:02 +02:00
|
|
|
if (!isCooked && !silent) { fs.writeSync(fdW, '\n'); }
|
2015-03-20 04:03:58 +01:00
|
|
|
setRawMode(false);
|
|
|
|
})();
|
|
|
|
|
2015-04-05 07:45:42 +02:00
|
|
|
if (options.print && !silent) { // must at least write '\n'
|
2015-04-07 11:38:14 +02:00
|
|
|
options.print(displaySave + (options.hideEchoBack ?
|
2015-04-04 17:37:42 +02:00
|
|
|
(new Array(input.length + 1)).join(options.mask) : input) + '\n',
|
|
|
|
options.encoding);
|
2013-08-29 12:55:23 +02:00
|
|
|
}
|
|
|
|
|
2015-04-07 11:38:14 +02:00
|
|
|
return (options.keepWhitespace || options.keyIn ? input : input.trim());
|
2014-06-27 03:06:29 +02:00
|
|
|
}
|
|
|
|
|
2015-03-25 08:56:43 +01:00
|
|
|
function readlineExt(options) {
|
2015-03-27 12:25:59 +01:00
|
|
|
var hostArgs, res = {},
|
2015-04-04 17:37:42 +02:00
|
|
|
execOptions = {env: process.env, encoding: options.encoding};
|
2015-03-25 08:56:43 +01:00
|
|
|
|
2015-03-27 12:25:59 +01:00
|
|
|
if (!extHostPath) {
|
|
|
|
if (IS_WIN) {
|
|
|
|
if (process.env.PSModulePath) { // Windows PowerShell
|
|
|
|
extHostPath = 'powershell.exe';
|
|
|
|
extHostArgs = ['-ExecutionPolicy', 'Bypass', '-File', __dirname + '\\read.ps1'];
|
|
|
|
} else { // Windows Script Host
|
|
|
|
extHostPath = 'cscript.exe';
|
|
|
|
extHostArgs = ['//nologo', __dirname + '\\read.cs.js'];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
extHostPath = '/bin/sh';
|
|
|
|
extHostArgs = [__dirname + '/read.sh'];
|
|
|
|
}
|
2015-03-11 09:06:27 +01:00
|
|
|
}
|
2015-03-27 12:25:59 +01:00
|
|
|
if (IS_WIN && !process.env.PSModulePath) { // Windows Script Host
|
|
|
|
// ScriptPW (Win XP and Server2003) needs TTY stream as STDIN.
|
|
|
|
// In this case, If STDIN isn't TTY, an error is thrown.
|
|
|
|
execOptions.stdio = [process.stdin];
|
2015-02-11 23:39:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (childProc.execFileSync) {
|
2015-03-27 12:25:59 +01:00
|
|
|
hostArgs = getHostArgs(options);
|
2015-03-03 18:30:41 +01:00
|
|
|
try {
|
2015-03-27 12:25:59 +01:00
|
|
|
res.input = childProc.execFileSync(extHostPath, hostArgs, execOptions);
|
2015-03-03 18:30:41 +01:00
|
|
|
} catch (e) { // non-zero exit code
|
2015-03-06 07:23:32 +01:00
|
|
|
res.error = new Error(DEFAULT_ERR_MSG);
|
|
|
|
res.error.method = 'execFileSync';
|
2015-03-27 12:25:59 +01:00
|
|
|
res.error.program = extHostPath;
|
|
|
|
res.error.args = hostArgs;
|
|
|
|
res.error.extMessage = e.stderr.trim();
|
|
|
|
res.error.exitCode = e.status;
|
2015-03-17 04:11:46 +01:00
|
|
|
res.error.code = e.code;
|
|
|
|
res.error.signal = e.signal;
|
2015-03-03 18:30:41 +01:00
|
|
|
}
|
2015-02-11 23:39:59 +01:00
|
|
|
} else {
|
2015-03-27 12:25:59 +01:00
|
|
|
res = _execFileSync(options, execOptions);
|
2015-02-11 23:39:59 +01:00
|
|
|
}
|
2015-03-13 10:34:41 +01:00
|
|
|
if (!res.error) {
|
2015-03-30 12:36:31 +02:00
|
|
|
res.input = res.input.replace(/^\s*'|'\s*$/g, '');
|
2015-03-13 10:34:41 +01:00
|
|
|
options.display = '';
|
|
|
|
}
|
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-27 12:25:59 +01:00
|
|
|
function _execFileSync(options, 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-27 12:25:59 +01:00
|
|
|
var hostArgs, shellPath, shellArgs, res = {}, exitCode,
|
2015-03-13 10:34:41 +01:00
|
|
|
pathStdout = getTempfile('readline-sync.stdout'),
|
|
|
|
pathStderr = getTempfile('readline-sync.stderr'),
|
|
|
|
pathExit = getTempfile('readline-sync.exit'),
|
|
|
|
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-27 12:25:59 +01:00
|
|
|
hostArgs = getHostArgs(options);
|
2015-03-02 14:44:36 +01:00
|
|
|
if (IS_WIN) {
|
2015-03-27 12:25:59 +01:00
|
|
|
shellPath = process.env.ComSpec || 'cmd.exe';
|
2015-03-07 16:19:40 +01:00
|
|
|
process.env.Q = '"'; // The quote (") that isn't escaped.
|
2015-03-03 18:30:41 +01:00
|
|
|
// `()` for ignore space by echo
|
2015-03-27 12:25:59 +01:00
|
|
|
shellArgs = ['/V:ON', '/S', '/C',
|
|
|
|
'(%Q%' + shellPath + '%Q% /V:ON /S /C %Q%' +
|
|
|
|
'%Q%' + extHostPath + '%Q%' +
|
|
|
|
hostArgs.map(function(arg) { return ' %Q%' + arg + '%Q%'; }).join('') +
|
2015-03-13 10:34:41 +01:00
|
|
|
' & (echo !ERRORLEVEL!)>%Q%' + pathExit + '%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-04-04 08:50:36 +02:00
|
|
|
shellPath = '/bin/sh';
|
2015-03-27 12:25:59 +01:00
|
|
|
shellArgs = ['-c',
|
2015-03-05 09:39:46 +01:00
|
|
|
// Use `()`, not `{}` for `-c` (text param)
|
2015-03-27 12:25:59 +01:00
|
|
|
'("' + extHostPath + '"' +
|
|
|
|
hostArgs.map(function(arg)
|
2015-03-13 10:34:41 +01:00
|
|
|
{ return " '" + arg.replace(/'/g, "'\\''") + "'"; }).join('') +
|
|
|
|
'; echo $?>"' + pathExit + '") 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-13 10:34:41 +01:00
|
|
|
try {
|
2015-03-27 12:25:59 +01:00
|
|
|
childProc.spawn(shellPath, shellArgs, execOptions);
|
2015-03-13 10:34:41 +01:00
|
|
|
} catch (e) {
|
|
|
|
res.error = new Error(e.message);
|
2015-03-25 08:56:43 +01:00
|
|
|
res.error.method = '_execFileSync - spawn';
|
2015-03-27 12:25:59 +01:00
|
|
|
res.error.program = shellPath;
|
|
|
|
res.error.args = shellArgs;
|
2015-03-13 10:34:41 +01:00
|
|
|
}
|
2014-06-29 00:00:15 +02:00
|
|
|
|
2015-04-04 17:37:42 +02:00
|
|
|
while (fs.readFileSync(pathDone, {encoding: options.encoding}).trim() !== '1') {}
|
|
|
|
if ((exitCode =
|
|
|
|
fs.readFileSync(pathExit, {encoding: options.encoding}).trim()) === '0') {
|
2015-03-06 07:23:32 +01:00
|
|
|
res.input =
|
2015-04-04 17:37:42 +02:00
|
|
|
decipher.update(fs.readFileSync(pathStdout, {encoding: 'binary'}),
|
|
|
|
'hex', options.encoding) +
|
|
|
|
decipher.final(options.encoding);
|
2015-03-03 18:30:41 +01:00
|
|
|
} else {
|
2015-03-06 07:23:32 +01:00
|
|
|
res.error = new Error(DEFAULT_ERR_MSG);
|
2015-03-25 08:56:43 +01:00
|
|
|
res.error.method = '_execFileSync';
|
2015-03-27 12:25:59 +01:00
|
|
|
res.error.program = shellPath;
|
|
|
|
res.error.args = shellArgs;
|
2015-04-04 17:37:42 +02:00
|
|
|
res.error.extMessage =
|
|
|
|
fs.readFileSync(pathStderr, {encoding: options.encoding}).trim();
|
2015-03-17 04:11:46 +01:00
|
|
|
res.error.exitCode = +exitCode;
|
2014-06-29 00:00:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fs.unlinkSync(pathStdout);
|
2015-03-03 18:30:41 +01:00
|
|
|
fs.unlinkSync(pathStderr);
|
2015-03-13 10:34:41 +01:00
|
|
|
fs.unlinkSync(pathExit);
|
2014-06-29 00:00:15 +02:00
|
|
|
fs.unlinkSync(pathDone);
|
|
|
|
|
2015-03-03 18:30:41 +01:00
|
|
|
return res;
|
2014-06-29 00:00:15 +02:00
|
|
|
}
|
|
|
|
|
2015-03-27 12:25:59 +01:00
|
|
|
function getHostArgs(options) {
|
|
|
|
// To send any text to crazy Windows shell safely.
|
2015-04-08 10:33:53 +02:00
|
|
|
function encodeArg(arg) {
|
2015-03-27 12:25:59 +01:00
|
|
|
return arg.replace(/[^\w\u0080-\uFFFF]/g, function(chr) {
|
|
|
|
return '#' + chr.charCodeAt(0) + ';';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return extHostArgs.concat((function(conf) {
|
2015-04-05 07:45:42 +02:00
|
|
|
var args = [];
|
|
|
|
Object.keys(conf).forEach(function(optionName) {
|
|
|
|
if (conf[optionName] === 'boolean') {
|
|
|
|
if (options[optionName]) { args.push('--' + optionName); }
|
|
|
|
} else if (conf[optionName] === 'string') {
|
|
|
|
if (options[optionName]) {
|
2015-04-08 10:33:53 +02:00
|
|
|
args.push('--' + optionName, encodeArg(options[optionName]));
|
2015-03-27 12:25:59 +01:00
|
|
|
}
|
|
|
|
}
|
2015-04-05 07:45:42 +02:00
|
|
|
});
|
2015-03-27 12:25:59 +01:00
|
|
|
return args;
|
|
|
|
})({
|
2015-03-31 13:27:02 +02:00
|
|
|
display: 'string',
|
|
|
|
keyIn: 'boolean',
|
2015-04-07 11:38:14 +02:00
|
|
|
hideEchoBack: 'boolean',
|
2015-03-31 13:27:02 +02:00
|
|
|
mask: 'string',
|
2015-04-03 18:14:55 +02:00
|
|
|
limit: 'string',
|
2015-04-08 10:33:53 +02:00
|
|
|
caseSensitive: 'boolean'
|
2015-03-27 12:25:59 +01:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2015-04-07 11:38:14 +02:00
|
|
|
function flattenArray(array, validator) {
|
|
|
|
var flatArray = [];
|
|
|
|
function parseArray(array) {
|
|
|
|
/* jshint eqnull:true */
|
|
|
|
if (array == null) { return; }
|
|
|
|
/* jshint eqnull:false */
|
|
|
|
else if (Array.isArray(array)) { array.forEach(parseArray); }
|
|
|
|
else if (!validator || validator(array)) { flatArray.push(array); }
|
|
|
|
}
|
|
|
|
parseArray(array);
|
|
|
|
return flatArray;
|
|
|
|
}
|
|
|
|
|
2015-04-05 07:45:42 +02:00
|
|
|
// margeOptions(options1, options2 ... )
|
|
|
|
// margeOptions(true, options1, options2 ... ) // from defaultOptions
|
2015-04-04 17:37:42 +02:00
|
|
|
function margeOptions() {
|
2015-04-05 07:45:42 +02:00
|
|
|
var optionsList = Array.prototype.slice.call(arguments),
|
|
|
|
optionNames, fromDefault;
|
|
|
|
|
|
|
|
if (optionsList.length && typeof optionsList[0] === 'boolean') {
|
|
|
|
fromDefault = optionsList.shift();
|
|
|
|
if (fromDefault) {
|
|
|
|
optionNames = Object.keys(defaultOptions);
|
|
|
|
optionsList.unshift(defaultOptions);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return optionsList.reduce(function(options, optionsPart) {
|
|
|
|
/* jshint eqnull:true */
|
|
|
|
if (optionsPart == null) { return options; }
|
|
|
|
/* jshint eqnull:false */
|
|
|
|
|
2015-04-08 10:33:53 +02:00
|
|
|
// ======== DEPRECATED ========
|
|
|
|
if (optionsPart.hasOwnProperty('noEchoBack')) {
|
|
|
|
optionsPart.hideEchoBack = optionsPart.noEchoBack;
|
|
|
|
delete optionsPart.noEchoBack;
|
|
|
|
}
|
|
|
|
if (optionsPart.hasOwnProperty('noTrim')) {
|
|
|
|
optionsPart.keepWhitespace = optionsPart.noTrim;
|
|
|
|
delete optionsPart.noTrim;
|
|
|
|
}
|
|
|
|
// ======== /DEPRECATED ========
|
|
|
|
|
2015-04-05 07:45:42 +02:00
|
|
|
if (!fromDefault) { optionNames = Object.keys(optionsPart); }
|
|
|
|
optionNames.forEach(function(optionName) {
|
|
|
|
var value;
|
|
|
|
if (!optionsPart.hasOwnProperty(optionName)) { return; }
|
|
|
|
value = optionsPart[optionName];
|
|
|
|
switch (optionName) {
|
|
|
|
// _readlineSync defaultOptions
|
2015-04-07 11:38:14 +02:00
|
|
|
// ================ string
|
2015-04-05 07:45:42 +02:00
|
|
|
case 'mask': // * *
|
|
|
|
case 'encoding': // * *
|
2015-04-05 13:05:23 +02:00
|
|
|
case 'limitMessage': // *
|
2015-04-05 07:45:42 +02:00
|
|
|
/* jshint eqnull:true */
|
2015-04-07 11:38:14 +02:00
|
|
|
value = value != null ? value + '' : '';
|
2015-04-05 07:45:42 +02:00
|
|
|
/* jshint eqnull:false */
|
2015-04-07 11:38:14 +02:00
|
|
|
if (value && optionName === 'mask' || optionName === 'encoding')
|
|
|
|
{ value = value.replace(/[\r\n]/g, ''); }
|
|
|
|
options[optionName] = value;
|
2015-04-05 07:45:42 +02:00
|
|
|
break;
|
2015-04-07 11:38:14 +02:00
|
|
|
// ================ number
|
2015-04-05 08:39:16 +02:00
|
|
|
case 'bufferSize': // * *
|
2015-04-05 07:45:42 +02:00
|
|
|
if (!isNaN(value = parseInt(value, 10)) && typeof value === 'number')
|
|
|
|
{ options[optionName] = value; }
|
|
|
|
break;
|
2015-04-07 11:38:14 +02:00
|
|
|
// ================ boolean
|
|
|
|
case 'hideEchoBack': // * *
|
2015-04-05 07:45:42 +02:00
|
|
|
case 'caseSensitive': // * *
|
2015-04-07 11:38:14 +02:00
|
|
|
case 'keepWhitespace': // * *
|
2015-04-05 07:45:42 +02:00
|
|
|
case 'keyIn': // *
|
|
|
|
options[optionName] = !!value;
|
|
|
|
break;
|
2015-04-07 11:38:14 +02:00
|
|
|
// ================ function
|
2015-04-05 07:45:42 +02:00
|
|
|
case 'print': // * *
|
2015-04-05 08:39:16 +02:00
|
|
|
options[optionName] = typeof value === 'function' ? value : void 0;
|
2015-04-05 07:45:42 +02:00
|
|
|
break;
|
2015-04-07 11:38:14 +02:00
|
|
|
// ================ array
|
2015-04-05 11:11:30 +02:00
|
|
|
case 'limit': // * * readlineExt
|
2015-04-07 11:38:14 +02:00
|
|
|
case 'trueValue': // *
|
|
|
|
case 'falseValue': // *
|
2015-04-05 11:11:30 +02:00
|
|
|
/* jshint eqnull:true */
|
2015-04-07 11:38:14 +02:00
|
|
|
value = value != null ?
|
2015-04-05 11:11:30 +02:00
|
|
|
flattenArray(value, function(value) {
|
|
|
|
return typeof value === 'string' || typeof value === 'number' ||
|
|
|
|
value instanceof RegExp;
|
|
|
|
}) : [];
|
|
|
|
/* jshint eqnull:false */
|
2015-04-07 11:38:14 +02:00
|
|
|
options[optionName] = value.map(function(value) {
|
|
|
|
return typeof value === 'string' ? value.replace(/[\r\n]/g, '') : value;
|
|
|
|
});
|
2015-04-05 11:11:30 +02:00
|
|
|
break;
|
2015-04-07 11:38:14 +02:00
|
|
|
// ================ other
|
2015-04-05 07:45:42 +02:00
|
|
|
case 'prompt': // *
|
2015-04-05 11:11:30 +02:00
|
|
|
case 'display': // * readlineExt
|
2015-04-05 07:45:42 +02:00
|
|
|
/* jshint eqnull:true */
|
|
|
|
options[optionName] = value != null ? value : '';
|
|
|
|
/* jshint eqnull:false */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return options;
|
|
|
|
}, {});
|
2015-04-04 17:37:42 +02:00
|
|
|
}
|
|
|
|
|
2015-04-05 16:14:48 +02:00
|
|
|
function isMatched(res, comps, caseSensitive) {
|
|
|
|
return comps.some(function(comp) {
|
|
|
|
if (typeof comp === 'number') { comp += ''; }
|
|
|
|
return (typeof comp === 'string' ? (
|
2015-04-05 13:05:23 +02:00
|
|
|
caseSensitive ?
|
2015-04-05 16:14:48 +02:00
|
|
|
res === comp : res.toLowerCase() === comp.toLowerCase()
|
2015-04-05 13:05:23 +02:00
|
|
|
) :
|
2015-04-05 16:14:48 +02:00
|
|
|
comp instanceof RegExp ? comp.test(res) : false);
|
2015-04-05 13:05:23 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-04-07 11:38:14 +02:00
|
|
|
function replacePlaceholder(text, generator) {
|
|
|
|
return text.replace(/(\$)?(\$\{(?:\(([\s\S]*?)\))?(\w+|.-.)(?:\(([\s\S]*?)\))?\})/g,
|
|
|
|
function(str, escape, placeholder, pre, param, post) {
|
|
|
|
var text;
|
|
|
|
return escape || typeof(text = generator(param)) !== 'string' ? placeholder :
|
|
|
|
text ? (pre || '') + text + (post || '') : '';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function placeholderInMessage(param, options) {
|
|
|
|
var text, values, group = [], groupClass = -1, charCode = 0, suppressed;
|
|
|
|
switch (param) {
|
|
|
|
case 'hideEchoBack':
|
|
|
|
case 'mask':
|
|
|
|
case 'caseSensitive':
|
|
|
|
case 'keepWhitespace':
|
|
|
|
case 'encoding':
|
|
|
|
case 'bufferSize':
|
|
|
|
case 'input':
|
|
|
|
text = options.hasOwnProperty(param) ? options[param] + '' : '';
|
|
|
|
break;
|
|
|
|
case 'prompt':
|
|
|
|
case 'query':
|
|
|
|
case 'display':
|
|
|
|
text = options.displaySrc + '';
|
|
|
|
break;
|
|
|
|
case 'limit':
|
|
|
|
case 'trueValue':
|
|
|
|
case 'falseValue':
|
|
|
|
values = options[options.hasOwnProperty(param + 'Src') ? param + 'Src' : param];
|
|
|
|
if (options.keyIn) { // suppress
|
|
|
|
values = values.reduce(function(chars, value) {
|
|
|
|
return chars.concat((value + '').split(''));
|
|
|
|
}, [])
|
|
|
|
.reduce(function(groups, curChar) {
|
|
|
|
var curGroupClass, curCharCode;
|
|
|
|
if (!options.caseSensitive) { curChar = curChar.toUpperCase(); }
|
|
|
|
curGroupClass = /^\d$/.test(curChar) ? 1 :
|
|
|
|
/^[A-Z]$/.test(curChar) ? 2 : /^[a-z]$/.test(curChar) ? 3 : 0;
|
|
|
|
curCharCode = curChar.charCodeAt(0);
|
|
|
|
if (curGroupClass && curGroupClass === groupClass &&
|
|
|
|
curCharCode === charCode + 1) {
|
|
|
|
group.push(curChar);
|
|
|
|
} else {
|
|
|
|
if (group.length > 3) { // ellipsis
|
|
|
|
groups.push(group[0] + ' ... ' + group[group.length - 1]);
|
|
|
|
suppressed = true;
|
|
|
|
} else {
|
|
|
|
groups = groups.concat(group);
|
|
|
|
}
|
|
|
|
group = [curChar];
|
|
|
|
groupClass = curGroupClass;
|
|
|
|
}
|
|
|
|
charCode = curCharCode;
|
|
|
|
return groups;
|
|
|
|
}, []);
|
|
|
|
if (group.length > 3) { // ellipsis
|
|
|
|
values.push(group[0] + ' ... ' + group[group.length - 1]);
|
|
|
|
suppressed = true;
|
|
|
|
} else {
|
|
|
|
values = values.concat(group);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
values = values.filter(function(value)
|
|
|
|
{ return typeof value === 'string' || typeof value === 'number'; });
|
|
|
|
}
|
|
|
|
text = values.join(values.length > 2 ? ', ' : suppressed ? ' / ' : '/');
|
|
|
|
break;
|
|
|
|
case 'limitCount':
|
2015-04-08 10:33:53 +02:00
|
|
|
case 'limitCountNotZero':
|
2015-04-07 11:38:14 +02:00
|
|
|
text = options[options.hasOwnProperty('limitSrc') ? 'limitSrc' : 'limit'].length;
|
2015-04-08 10:33:53 +02:00
|
|
|
text = (text ? text : param === 'limitCountNotZero' ? '' : text) + '';
|
2015-04-07 11:38:14 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2015-04-05 13:05:23 +02:00
|
|
|
function readlineWithOptions(options) {
|
2015-04-07 11:38:14 +02:00
|
|
|
var res,
|
|
|
|
generator = function(param) { return placeholderInMessage(param, options); };
|
|
|
|
options.limitSrc = options.limit;
|
|
|
|
options.displaySrc = options.display;
|
2015-04-05 13:05:23 +02:00
|
|
|
options.limit = ''; // for readlineExt
|
2015-04-07 11:38:14 +02:00
|
|
|
options.display = replacePlaceholder(options.display + '', generator);
|
2015-04-05 13:05:23 +02:00
|
|
|
while (true) {
|
|
|
|
res = _readlineSync(options);
|
2015-04-07 11:38:14 +02:00
|
|
|
if (!options.limitSrc.length ||
|
|
|
|
isMatched(res, options.limitSrc, options.caseSensitive)) { break; }
|
|
|
|
options.input = res; // for placeholder
|
2015-04-05 13:05:23 +02:00
|
|
|
options.display += (options.display ? '\n' : '') +
|
2015-04-07 11:38:14 +02:00
|
|
|
(options.limitMessage ?
|
|
|
|
replacePlaceholder(options.limitMessage, generator) + '\n' : '') +
|
|
|
|
replacePlaceholder(options.displaySrc + '', generator);
|
2015-04-05 13:05:23 +02:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-04-05 16:14:48 +02:00
|
|
|
function toBool(res, options) {
|
|
|
|
return (
|
2015-04-07 11:38:14 +02:00
|
|
|
(options.trueValue.length &&
|
|
|
|
isMatched(res, options.trueValue, options.caseSensitive)) ? true :
|
|
|
|
(options.falseValue.length &&
|
|
|
|
isMatched(res, options.falseValue, options.caseSensitive)) ? false : res);
|
2015-04-05 16:14:48 +02:00
|
|
|
}
|
|
|
|
|
2014-06-27 03:06:29 +02:00
|
|
|
// for dev
|
2015-03-25 08:56:43 +01:00
|
|
|
exports._useExtSet = function(use) { useExt = use; };
|
2013-08-29 19:26:22 +02:00
|
|
|
|
2015-04-07 11:38:14 +02:00
|
|
|
exports.setDefault = function(options) {
|
|
|
|
defaultOptions = margeOptions(true, options);
|
|
|
|
return margeOptions(true); // copy
|
|
|
|
};
|
|
|
|
|
2014-07-12 00:37:25 +02:00
|
|
|
exports.prompt = function(options) {
|
2015-04-05 13:05:23 +02:00
|
|
|
var readOptions = margeOptions(true, options), res;
|
|
|
|
readOptions.display = readOptions.prompt;
|
|
|
|
res = readlineWithOptions(readOptions);
|
2015-04-05 16:14:48 +02:00
|
|
|
return toBool(res, readOptions);
|
2013-08-29 19:26:22 +02:00
|
|
|
};
|
|
|
|
|
2014-07-12 00:37:25 +02:00
|
|
|
exports.question = function(query, options) {
|
2015-04-05 07:45:42 +02:00
|
|
|
var readOptions = margeOptions(margeOptions(true, options), {
|
2015-04-07 11:38:14 +02:00
|
|
|
display: query
|
2015-04-05 11:11:30 +02:00
|
|
|
}),
|
2015-04-05 13:05:23 +02:00
|
|
|
res = readlineWithOptions(readOptions);
|
2015-04-05 16:14:48 +02:00
|
|
|
return toBool(res, readOptions);
|
2013-08-29 19:26:22 +02:00
|
|
|
};
|
2015-03-01 15:46:52 +01:00
|
|
|
|
2015-03-07 16:19:40 +01:00
|
|
|
exports.keyIn = function(query, options) {
|
2015-04-05 11:11:30 +02:00
|
|
|
var readOptions = margeOptions(margeOptions(true, options), {
|
2015-04-07 11:38:14 +02:00
|
|
|
display: query,
|
|
|
|
keyIn: true,
|
|
|
|
keepWhitespace: true
|
2015-04-05 11:11:30 +02:00
|
|
|
}), res;
|
2015-04-07 11:38:14 +02:00
|
|
|
|
|
|
|
// char list
|
|
|
|
readOptions.limitSrc = readOptions.limit.filter(function(value)
|
2015-04-05 11:11:30 +02:00
|
|
|
{ return typeof value === 'string' || typeof value === 'number'; })
|
2015-04-07 11:38:14 +02:00
|
|
|
.map(function(text) { // placeholders
|
|
|
|
return replacePlaceholder(text + '', function(param) { // char list
|
|
|
|
var matches = /^(.)-(.)$/.exec(param), text = '', from, to, code, step;
|
|
|
|
if (!matches) { return; }
|
|
|
|
from = matches[1].charCodeAt(0);
|
|
|
|
to = matches[2].charCodeAt(0);
|
|
|
|
step = from < to ? 1 : -1;
|
|
|
|
for (code = from; code !== to + step; code += step)
|
|
|
|
{ text += String.fromCharCode(code); }
|
|
|
|
return text;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
// pattern
|
|
|
|
readOptions.limit = readOptions.limitSrc.join('').replace(/[^A-Za-z0-9_ ]/g, '\\$&');
|
2015-04-05 16:14:48 +02:00
|
|
|
|
2015-04-07 11:38:14 +02:00
|
|
|
['trueValue', 'falseValue'].forEach(function(optionName) {
|
2015-04-05 16:14:48 +02:00
|
|
|
var comps = [];
|
|
|
|
readOptions[optionName].forEach(function(comp) {
|
|
|
|
if (typeof comp === 'string' || typeof comp === 'number') {
|
|
|
|
comps = comps.concat((comp + '').split(''));
|
|
|
|
} else if (comp instanceof RegExp) {
|
|
|
|
comps.push(comp);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
readOptions[optionName] = comps;
|
|
|
|
});
|
|
|
|
|
2015-04-07 11:38:14 +02:00
|
|
|
readOptions.display = replacePlaceholder(readOptions.display + '',
|
|
|
|
function(param) { return placeholderInMessage(param, readOptions); });
|
2015-04-05 07:45:42 +02:00
|
|
|
|
2015-04-07 11:38:14 +02:00
|
|
|
res = _readlineSync(readOptions);
|
|
|
|
|
|
|
|
return toBool(res, readOptions);
|
2015-03-01 15:46:52 +01:00
|
|
|
};
|
2015-04-05 07:45:42 +02:00
|
|
|
|
2015-04-07 11:38:14 +02:00
|
|
|
// ======== DEPRECATED ========
|
|
|
|
function _setOption(optionName, args) {
|
2015-04-05 08:39:16 +02:00
|
|
|
var options;
|
|
|
|
if (args.length) { options = {}; options[optionName] = args[0]; }
|
|
|
|
return exports.setDefault(options)[optionName];
|
|
|
|
}
|
2015-04-07 11:38:14 +02:00
|
|
|
exports.setPrint = function() { return _setOption('print', arguments); };
|
|
|
|
exports.setPrompt = function() { return _setOption('prompt', arguments); };
|
|
|
|
exports.setEncoding = function() { return _setOption('encoding', arguments); };
|
|
|
|
exports.setMask = function() { return _setOption('mask', arguments); };
|
|
|
|
exports.setBufferSize = function() { return _setOption('bufferSize', arguments); };
|