2013-08-29 12:55:23 +02:00
|
|
|
/*
|
|
|
|
* readlineSync
|
|
|
|
* https://github.com/anseki/readline-sync
|
|
|
|
*
|
2016-01-27 08:42:29 +01:00
|
|
|
* Copyright (c) 2016 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',
|
2016-02-06 05:47:10 +01:00
|
|
|
DEFAULT_ERR_MSG = 'The current environment doesn\'t support interactive reading from TTY.',
|
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-20 07:53:22 +02:00
|
|
|
pathUtil = require('path'),
|
2015-02-11 20:44:05 +01:00
|
|
|
|
2015-04-04 17:37:42 +02:00
|
|
|
defaultOptions = {
|
2016-04-27 15:02:12 +02:00
|
|
|
/* eslint-disable key-spacing */
|
2015-04-07 11:38:14 +02:00
|
|
|
prompt: '> ',
|
|
|
|
hideEchoBack: false,
|
|
|
|
mask: '*',
|
|
|
|
limit: [],
|
2016-02-23 17:20:41 +01:00
|
|
|
limitMessage: 'Input another, please.$<( [)limit(])>',
|
2015-04-14 18:05:24 +02:00
|
|
|
defaultInput: '',
|
2015-04-13 08:55:33 +02:00
|
|
|
trueValue: [],
|
|
|
|
falseValue: [],
|
2015-04-07 11:38:14 +02:00
|
|
|
caseSensitive: false,
|
|
|
|
keepWhitespace: false,
|
|
|
|
encoding: 'utf8',
|
|
|
|
bufferSize: 1024,
|
|
|
|
print: void 0,
|
2015-04-14 18:05:24 +02:00
|
|
|
history: true,
|
|
|
|
cd: false,
|
2015-04-17 11:08:22 +02:00
|
|
|
phContent: void 0,
|
|
|
|
preCheck: void 0
|
2016-04-27 15:02:12 +02:00
|
|
|
/* eslint-enable key-spacing */
|
2015-04-04 17:37:42 +02:00
|
|
|
},
|
2014-06-27 03:06:29 +02:00
|
|
|
|
2015-03-20 04:03:58 +01:00
|
|
|
fdR = 'none', fdW, ttyR, isRawMode = false,
|
2015-04-11 05:11:52 +02:00
|
|
|
extHostPath, extHostArgs, tempdir, salt = 0,
|
2015-04-13 08:55:33 +02:00
|
|
|
lastInput = '', inputHistory = [],
|
2015-04-11 05:11:52 +02:00
|
|
|
_DBG_useExt = false, _DBG_checkOptions = false, _DBG_checkMethod = false;
|
2013-08-29 12:55:23 +02:00
|
|
|
|
2016-04-27 15:02:12 +02:00
|
|
|
function getHostArgs(options) {
|
|
|
|
// Send any text to crazy Windows shell safely.
|
|
|
|
function encodeArg(arg) {
|
|
|
|
return arg.replace(/[^\w\u0080-\uFFFF]/g, function(chr) {
|
|
|
|
return '#' + chr.charCodeAt(0) + ';';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return extHostArgs.concat((function(conf) {
|
|
|
|
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]) {
|
|
|
|
args.push('--' + optionName, encodeArg(options[optionName]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return args;
|
|
|
|
})({
|
|
|
|
/* eslint-disable key-spacing */
|
|
|
|
display: 'string',
|
|
|
|
displayOnly: 'boolean',
|
|
|
|
keyIn: 'boolean',
|
|
|
|
hideEchoBack: 'boolean',
|
|
|
|
mask: 'string',
|
|
|
|
limit: 'string',
|
|
|
|
caseSensitive: 'boolean'
|
|
|
|
/* eslint-enable key-spacing */
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2016-04-28 10:42:26 +02:00
|
|
|
// piping via files (for Node.js v0.10-)
|
2016-04-27 15:02:12 +02:00
|
|
|
function _execFileSync(options, execOptions) {
|
|
|
|
|
|
|
|
function getTempfile(name) {
|
|
|
|
var filepath, suffix = '', fd;
|
|
|
|
tempdir = tempdir || require('os').tmpdir();
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
filepath = pathUtil.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;
|
|
|
|
}
|
|
|
|
|
|
|
|
var hostArgs, shellPath, shellArgs, res = {}, exitCode, extMessage,
|
|
|
|
pathStdout = getTempfile('readline-sync.stdout'),
|
|
|
|
pathStderr = getTempfile('readline-sync.stderr'),
|
|
|
|
pathExit = getTempfile('readline-sync.exit'),
|
|
|
|
pathDone = getTempfile('readline-sync.done'),
|
|
|
|
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);
|
|
|
|
|
|
|
|
hostArgs = getHostArgs(options);
|
|
|
|
if (IS_WIN) {
|
|
|
|
shellPath = process.env.ComSpec || 'cmd.exe';
|
|
|
|
process.env.Q = '"'; // The quote (") that isn't escaped.
|
|
|
|
// `()` for ignore space by echo
|
|
|
|
shellArgs = ['/V:ON', '/S', '/C',
|
|
|
|
'(%Q%' + shellPath + '%Q% /V:ON /S /C %Q%' + /* ESLint bug? */ // eslint-disable-line no-path-concat
|
|
|
|
'%Q%' + extHostPath + '%Q%' +
|
|
|
|
hostArgs.map(function(arg) { return ' %Q%' + arg + '%Q%'; }).join('') +
|
|
|
|
' & (echo !ERRORLEVEL!)>%Q%' + pathExit + '%Q%%Q%) 2>%Q%' + pathStderr + '%Q%' +
|
|
|
|
' |%Q%' + process.execPath + '%Q% %Q%' + __dirname + '\\encrypt.js%Q%' +
|
|
|
|
' %Q%' + ALGORITHM_CIPHER + '%Q% %Q%' + password + '%Q%' +
|
|
|
|
' >%Q%' + pathStdout + '%Q%' +
|
|
|
|
' & (echo 1)>%Q%' + pathDone + '%Q%'];
|
|
|
|
} else {
|
|
|
|
shellPath = '/bin/sh';
|
|
|
|
shellArgs = ['-c',
|
|
|
|
// Use `()`, not `{}` for `-c` (text param)
|
|
|
|
'("' + extHostPath + '"' + /* ESLint bug? */ // eslint-disable-line no-path-concat
|
|
|
|
hostArgs.map(function(arg) { return " '" + arg.replace(/'/g, "'\\''") + "'"; }).join('') +
|
|
|
|
'; echo $?>"' + pathExit + '") 2>"' + pathStderr + '"' +
|
|
|
|
' |"' + process.execPath + '" "' + __dirname + '/encrypt.js"' +
|
|
|
|
' "' + ALGORITHM_CIPHER + '" "' + password + '"' +
|
|
|
|
' >"' + pathStdout + '"' +
|
|
|
|
'; echo 1 >"' + pathDone + '"'];
|
|
|
|
}
|
|
|
|
if (_DBG_checkMethod) { _DBG_checkMethod('_execFileSync', hostArgs); }
|
|
|
|
try {
|
|
|
|
childProc.spawn(shellPath, shellArgs, execOptions);
|
|
|
|
} catch (e) {
|
|
|
|
res.error = new Error(e.message);
|
|
|
|
res.error.method = '_execFileSync - spawn';
|
|
|
|
res.error.program = shellPath;
|
|
|
|
res.error.args = shellArgs;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (fs.readFileSync(pathDone, {encoding: options.encoding}).trim() !== '1') {} // eslint-disable-line no-empty
|
|
|
|
if ((exitCode =
|
|
|
|
fs.readFileSync(pathExit, {encoding: options.encoding}).trim()) === '0') {
|
|
|
|
res.input =
|
|
|
|
decipher.update(fs.readFileSync(pathStdout, {encoding: 'binary'}),
|
|
|
|
'hex', options.encoding) +
|
|
|
|
decipher.final(options.encoding);
|
|
|
|
} else {
|
|
|
|
extMessage = fs.readFileSync(pathStderr, {encoding: options.encoding}).trim();
|
|
|
|
res.error = new Error(DEFAULT_ERR_MSG + (extMessage ? '\n' + extMessage : ''));
|
|
|
|
res.error.method = '_execFileSync';
|
|
|
|
res.error.program = shellPath;
|
|
|
|
res.error.args = shellArgs;
|
|
|
|
res.error.extMessage = extMessage;
|
|
|
|
res.error.exitCode = +exitCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.unlinkSync(pathStdout);
|
|
|
|
fs.unlinkSync(pathStderr);
|
|
|
|
fs.unlinkSync(pathExit);
|
|
|
|
fs.unlinkSync(pathDone);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
function readlineExt(options) {
|
|
|
|
var hostArgs, res = {}, extMessage,
|
|
|
|
execOptions = {env: process.env, encoding: options.encoding};
|
|
|
|
|
|
|
|
if (!extHostPath) {
|
|
|
|
if (IS_WIN) {
|
|
|
|
if (process.env.PSModulePath) { // Windows PowerShell
|
|
|
|
extHostPath = 'powershell.exe';
|
|
|
|
extHostArgs = ['-ExecutionPolicy', 'Bypass', '-File', __dirname + '\\read.ps1']; // eslint-disable-line no-path-concat
|
|
|
|
} else { // Windows Script Host
|
|
|
|
extHostPath = 'cscript.exe';
|
|
|
|
extHostArgs = ['//nologo', __dirname + '\\read.cs.js']; // eslint-disable-line no-path-concat
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
extHostPath = '/bin/sh';
|
|
|
|
extHostArgs = [__dirname + '/read.sh']; // eslint-disable-line no-path-concat
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (childProc.execFileSync) {
|
|
|
|
hostArgs = getHostArgs(options);
|
|
|
|
if (_DBG_checkMethod) { _DBG_checkMethod('execFileSync', hostArgs); }
|
|
|
|
try {
|
|
|
|
res.input = childProc.execFileSync(extHostPath, hostArgs, execOptions);
|
|
|
|
} catch (e) { // non-zero exit code
|
|
|
|
extMessage = e.stderr ? (e.stderr + '').trim() : '';
|
|
|
|
res.error = new Error(DEFAULT_ERR_MSG + (extMessage ? '\n' + extMessage : ''));
|
|
|
|
res.error.method = 'execFileSync';
|
|
|
|
res.error.program = extHostPath;
|
|
|
|
res.error.args = hostArgs;
|
|
|
|
res.error.extMessage = extMessage;
|
|
|
|
res.error.exitCode = e.status;
|
|
|
|
res.error.code = e.code;
|
|
|
|
res.error.signal = e.signal;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
res = _execFileSync(options, execOptions);
|
|
|
|
}
|
|
|
|
if (!res.error) {
|
|
|
|
res.input = res.input.replace(/^\s*'|'\s*$/g, '');
|
|
|
|
options.display = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-03-27 12:25:59 +01:00
|
|
|
/*
|
2015-04-07 11:38:14 +02:00
|
|
|
display: string
|
2015-04-13 08:55:33 +02:00
|
|
|
displayOnly: boolean
|
2015-04-07 11:38:14 +02:00
|
|
|
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-04-11 05:11:52 +02:00
|
|
|
if (_DBG_checkOptions) { _DBG_checkOptions(options); }
|
|
|
|
|
2015-03-31 13:27:02 +02:00
|
|
|
(function() { // open TTY
|
2015-12-24 10:32:18 +01:00
|
|
|
var fsB, constants, verNum;
|
2015-03-20 04:03:58 +01:00
|
|
|
|
|
|
|
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) {
|
2015-08-23 07:24:37 +02:00
|
|
|
// iojs-v2.3.2+ input stream can't read first line. (#18)
|
|
|
|
// ** Don't get process.stdin before check! **
|
2016-06-04 14:18:42 +02:00
|
|
|
// Fixed v5.1.0
|
|
|
|
// Fixed v4.2.4
|
|
|
|
// It regressed again in v5.6.0, it is fixed in v6.2.0.
|
2015-12-24 10:32:18 +01:00
|
|
|
verNum = (function(ver) { // getVerNum
|
2016-04-27 15:02:12 +02:00
|
|
|
var nums = ver.replace(/^\D+/, '').split('.');
|
|
|
|
var verNum = 0;
|
|
|
|
if ((nums[0] = +nums[0])) { verNum += nums[0] * 10000; }
|
|
|
|
if ((nums[1] = +nums[1])) { verNum += nums[1] * 100; }
|
|
|
|
if ((nums[2] = +nums[2])) { verNum += nums[2]; }
|
|
|
|
return verNum;
|
|
|
|
})(process.version);
|
2016-06-04 14:18:42 +02:00
|
|
|
if (!(verNum >= 20302 && verNum < 40204 || verNum >= 50000 && verNum < 50100 || verNum >= 50600 && verNum < 60200) &&
|
2015-12-24 10:32:18 +01:00
|
|
|
process.stdin.isTTY) {
|
2015-04-26 09:05:54 +02:00
|
|
|
process.stdin.pause();
|
2015-03-20 04:03:58 +01:00
|
|
|
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);
|
2016-04-27 15:02:12 +02:00
|
|
|
} catch (e) { /* ignore */ }
|
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');
|
2016-04-27 15:02:12 +02:00
|
|
|
} catch (e) { /* ignore */ }
|
2015-03-20 04:03:58 +01:00
|
|
|
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));
|
2016-04-27 15:02:12 +02:00
|
|
|
} catch (e) { /* ignore */ }
|
2015-03-07 16:19:40 +01:00
|
|
|
}
|
|
|
|
}
|
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) {
|
2015-04-26 09:05:54 +02:00
|
|
|
process.stdin.pause();
|
2015-03-20 04:03:58 +01:00
|
|
|
try {
|
|
|
|
fdR = fs.openSync('/dev/tty', 'r'); // device file, not process.stdin
|
|
|
|
ttyR = process.stdin._handle;
|
2016-04-27 15:02:12 +02:00
|
|
|
} catch (e) { /* ignore */ }
|
2015-03-20 04:03:58 +01:00
|
|
|
} else {
|
2016-04-28 10:42:26 +02:00
|
|
|
// Node.js v0.12 read() fails.
|
2015-03-20 04:03:58 +01:00
|
|
|
try {
|
|
|
|
fdR = fs.openSync('/dev/tty', 'r');
|
|
|
|
ttyR = new TTY(fdR, false);
|
2016-04-27 15:02:12 +02:00
|
|
|
} catch (e) { /* ignore */ }
|
2015-03-20 04:03:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (process.stdout.isTTY) {
|
|
|
|
fdW = process.stdout.fd;
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
fdW = fs.openSync('/dev/tty', 'w');
|
2016-04-27 15:02:12 +02:00
|
|
|
} catch (e) { /* ignore */ }
|
2015-03-20 04:03:58 +01:00
|
|
|
}
|
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
|
|
|
|
2016-04-28 10:42:26 +02:00
|
|
|
// Node.js v0.10- returns an error if same mode is set.
|
2015-03-25 13:22:44 +01:00
|
|
|
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-11 05:11:52 +02:00
|
|
|
if (_DBG_useExt || !ttyR ||
|
2015-04-04 17:37:42 +02:00
|
|
|
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-04-13 08:55:33 +02:00
|
|
|
if (options.displayOnly) { return; }
|
2015-03-20 04:03:58 +01:00
|
|
|
|
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;
|
|
|
|
}
|
2016-04-28 10:42:26 +02:00
|
|
|
|
|
|
|
// https://github.com/nodejs/node/issues/4660
|
|
|
|
// https://github.com/nodejs/node/pull/4682
|
2016-04-27 15:57:16 +02:00
|
|
|
if (Buffer.alloc) {
|
|
|
|
buffer = Buffer.alloc((reqSize = options.keyIn ? 1 : options.bufferSize));
|
|
|
|
} else {
|
|
|
|
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
|
|
|
|
2016-04-27 15:02:12 +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-13 08:55:33 +02:00
|
|
|
if (options.print && !silent) {
|
|
|
|
options.print(displaySave + (options.displayOnly ? '' :
|
|
|
|
(options.hideEchoBack ? (new Array(input.length + 1)).join(options.mask)
|
|
|
|
: input) + '\n'), // must at least write '\n'
|
2015-04-04 17:37:42 +02:00
|
|
|
options.encoding);
|
2013-08-29 12:55:23 +02:00
|
|
|
}
|
|
|
|
|
2015-04-13 08:55:33 +02:00
|
|
|
return options.displayOnly ? '' :
|
|
|
|
(lastInput = options.keepWhitespace || options.keyIn ? input : input.trim());
|
2014-06-27 03:06:29 +02:00
|
|
|
}
|
|
|
|
|
2015-04-07 11:38:14 +02:00
|
|
|
function flattenArray(array, validator) {
|
|
|
|
var flatArray = [];
|
2015-04-11 05:11:52 +02:00
|
|
|
function _flattenArray(array) {
|
2016-04-27 15:02:12 +02:00
|
|
|
if (array == null) { // eslint-disable-line eqeqeq
|
|
|
|
return;
|
|
|
|
} else if (Array.isArray(array)) {
|
|
|
|
array.forEach(_flattenArray);
|
|
|
|
} else if (!validator || validator(array)) {
|
|
|
|
flatArray.push(array);
|
|
|
|
}
|
2015-04-07 11:38:14 +02:00
|
|
|
}
|
2015-04-11 05:11:52 +02:00
|
|
|
_flattenArray(array);
|
2015-04-07 11:38:14 +02:00
|
|
|
return flatArray;
|
|
|
|
}
|
|
|
|
|
2016-02-23 17:20:41 +01:00
|
|
|
function escapePattern(pattern) {
|
|
|
|
return pattern.replace(/[\x00-\x7f]/g,
|
|
|
|
function(s) { return '\\x' + ('00' + s.charCodeAt().toString(16)).substr(-2); });
|
|
|
|
}
|
|
|
|
|
2015-04-05 07:45:42 +02:00
|
|
|
// margeOptions(options1, options2 ... )
|
2015-04-11 05:11:52 +02:00
|
|
|
// margeOptions(true, options1, options2 ... )
|
|
|
|
// arg1=true : Start from defaultOptions and pick elements of that.
|
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) {
|
2016-04-27 15:02:12 +02:00
|
|
|
if (optionsPart == null) { return options; } // eslint-disable-line eqeqeq
|
2015-04-05 07:45:42 +02:00
|
|
|
|
2015-04-08 10:33:53 +02:00
|
|
|
// ======== DEPRECATED ========
|
2015-04-11 05:11:52 +02:00
|
|
|
if (optionsPart.hasOwnProperty('noEchoBack') &&
|
|
|
|
!optionsPart.hasOwnProperty('hideEchoBack')) {
|
2015-04-08 10:33:53 +02:00
|
|
|
optionsPart.hideEchoBack = optionsPart.noEchoBack;
|
|
|
|
delete optionsPart.noEchoBack;
|
|
|
|
}
|
2015-04-11 05:11:52 +02:00
|
|
|
if (optionsPart.hasOwnProperty('noTrim') &&
|
|
|
|
!optionsPart.hasOwnProperty('keepWhitespace')) {
|
2015-04-08 10:33:53 +02:00
|
|
|
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];
|
2016-05-02 12:56:06 +02:00
|
|
|
switch (optionName) {
|
2015-04-14 18:05:24 +02:00
|
|
|
// _readlineSync <- * * -> defaultOptions
|
2015-04-07 11:38:14 +02:00
|
|
|
// ================ string
|
2015-04-05 07:45:42 +02:00
|
|
|
case 'mask': // * *
|
2015-04-05 13:05:23 +02:00
|
|
|
case 'limitMessage': // *
|
2015-04-14 18:05:24 +02:00
|
|
|
case 'defaultInput': // *
|
2015-04-13 08:55:33 +02:00
|
|
|
case 'encoding': // * *
|
2016-04-27 15:02:12 +02:00
|
|
|
value = value != null ? value + '' : ''; // eslint-disable-line eqeqeq
|
|
|
|
if (value && optionName !== 'limitMessage') { value = value.replace(/[\r\n]/g, ''); }
|
2015-04-07 11:38:14 +02:00
|
|
|
options[optionName] = value;
|
2015-04-05 07:45:42 +02:00
|
|
|
break;
|
2015-04-13 08:55:33 +02:00
|
|
|
// ================ number(int)
|
2015-04-05 08:39:16 +02:00
|
|
|
case 'bufferSize': // * *
|
2016-04-27 15:02:12 +02:00
|
|
|
if (!isNaN(value = parseInt(value, 10)) && typeof value === 'number') {
|
|
|
|
options[optionName] = value; // limited updating (number is needed)
|
|
|
|
}
|
2015-04-05 07:45:42 +02:00
|
|
|
break;
|
2015-04-07 11:38:14 +02:00
|
|
|
// ================ boolean
|
2015-04-13 08:55:33 +02:00
|
|
|
case 'displayOnly': // *
|
|
|
|
case 'keyIn': // *
|
2015-04-07 11:38:14 +02:00
|
|
|
case 'hideEchoBack': // * *
|
2015-04-05 07:45:42 +02:00
|
|
|
case 'caseSensitive': // * *
|
2015-04-07 11:38:14 +02:00
|
|
|
case 'keepWhitespace': // * *
|
2015-04-13 08:55:33 +02:00
|
|
|
case 'history': // *
|
2015-04-14 18:05:24 +02:00
|
|
|
case 'cd': // *
|
2015-04-05 07:45:42 +02:00
|
|
|
options[optionName] = !!value;
|
|
|
|
break;
|
2015-04-07 11:38:14 +02:00
|
|
|
// ================ array
|
2015-04-13 08:55:33 +02:00
|
|
|
case 'limit': // * * to string for readlineExt
|
2015-04-07 11:38:14 +02:00
|
|
|
case 'trueValue': // *
|
|
|
|
case 'falseValue': // *
|
2015-04-11 05:11:52 +02:00
|
|
|
options[optionName] = flattenArray(value, function(value) {
|
2016-04-27 15:02:12 +02:00
|
|
|
var type = typeof value;
|
|
|
|
return type === 'string' || type === 'number' ||
|
|
|
|
type === 'function' || value instanceof RegExp;
|
|
|
|
}).map(function(value) {
|
|
|
|
return typeof value === 'string' ? value.replace(/[\r\n]/g, '') : value;
|
|
|
|
});
|
2015-04-05 11:11:30 +02:00
|
|
|
break;
|
2015-04-13 08:55:33 +02:00
|
|
|
// ================ function
|
|
|
|
case 'print': // * *
|
2015-04-14 18:05:24 +02:00
|
|
|
case 'phContent': // *
|
2015-04-17 11:08:22 +02:00
|
|
|
case 'preCheck': // *
|
2015-04-13 08:55:33 +02:00
|
|
|
options[optionName] = typeof value === 'function' ? value : void 0;
|
|
|
|
break;
|
2015-04-07 11:38:14 +02:00
|
|
|
// ================ other
|
2015-04-05 07:45:42 +02:00
|
|
|
case 'prompt': // *
|
2015-04-13 08:55:33 +02:00
|
|
|
case 'display': // *
|
2016-04-27 15:02:12 +02:00
|
|
|
options[optionName] = value != null ? value : ''; // eslint-disable-line eqeqeq
|
2015-04-05 07:45:42 +02:00
|
|
|
break;
|
2016-05-02 12:56:06 +02:00
|
|
|
// no default
|
2015-04-05 07:45:42 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
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) {
|
2015-04-11 05:11:52 +02:00
|
|
|
var type = typeof comp;
|
2015-04-13 08:55:33 +02:00
|
|
|
return type === 'string' ?
|
2015-04-11 05:11:52 +02:00
|
|
|
(caseSensitive ? res === comp : res.toLowerCase() === comp.toLowerCase()) :
|
2016-04-27 15:02:12 +02:00
|
|
|
type === 'number' ? parseFloat(res) === comp :
|
2015-04-11 05:11:52 +02:00
|
|
|
type === 'function' ? comp(res) :
|
2015-04-13 08:55:33 +02:00
|
|
|
comp instanceof RegExp ? comp.test(res) : false;
|
2015-04-05 13:05:23 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-04-20 07:53:22 +02:00
|
|
|
function replaceHomePath(path, expand) {
|
|
|
|
var homePath = pathUtil.normalize(
|
|
|
|
IS_WIN ? (process.env.HOMEDRIVE || '') + (process.env.HOMEPATH || '') :
|
|
|
|
process.env.HOME || '').replace(/[\/\\]+$/, '');
|
|
|
|
path = pathUtil.normalize(path);
|
|
|
|
return expand ? path.replace(/^~(?=\/|\\|$)/, homePath) :
|
2016-02-23 17:20:41 +01:00
|
|
|
path.replace(new RegExp('^' + escapePattern(homePath) +
|
2015-04-20 07:53:22 +02:00
|
|
|
'(?=\\/|\\\\|$)', IS_WIN ? 'i' : ''), '~');
|
|
|
|
}
|
|
|
|
|
2015-04-07 11:38:14 +02:00
|
|
|
function replacePlaceholder(text, generator) {
|
2016-02-23 17:20:41 +01:00
|
|
|
var PTN_INNER = '(?:\\(([\\s\\S]*?)\\))?(\\w+|.-.)(?:\\(([\\s\\S]*?)\\))?',
|
|
|
|
rePlaceholder = new RegExp('(\\$)?(\\$<' + PTN_INNER + '>)', 'g'),
|
|
|
|
rePlaceholderCompat = new RegExp('(\\$)?(\\$\\{' + PTN_INNER + '\\})', 'g');
|
|
|
|
|
|
|
|
function getPlaceholderText(s, escape, placeholder, pre, param, post) {
|
|
|
|
var text;
|
2016-04-27 15:02:12 +02:00
|
|
|
return escape || typeof (text = generator(param)) !== 'string' ? placeholder :
|
2016-02-23 17:20:41 +01:00
|
|
|
text ? (pre || '') + text + (post || '') : '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return text.replace(rePlaceholder, getPlaceholderText)
|
|
|
|
.replace(rePlaceholderCompat, getPlaceholderText);
|
2015-04-07 11:38:14 +02:00
|
|
|
}
|
|
|
|
|
2015-04-11 05:11:52 +02:00
|
|
|
function array2charlist(array, caseSensitive, collectSymbols) {
|
|
|
|
var values, group = [], groupClass = -1, charCode = 0, symbols = '', suppressed;
|
|
|
|
function addGroup(groups, group) {
|
|
|
|
if (group.length > 3) { // ellipsis
|
|
|
|
groups.push(group[0] + '...' + group[group.length - 1]);
|
|
|
|
suppressed = true;
|
|
|
|
} else if (group.length) {
|
|
|
|
groups = groups.concat(group);
|
|
|
|
}
|
|
|
|
return groups;
|
|
|
|
}
|
|
|
|
|
2016-04-27 15:02:12 +02:00
|
|
|
values = array.reduce(
|
|
|
|
function(chars, value) { return chars.concat((value + '').split('')); }, [])
|
2015-04-11 05:11:52 +02:00
|
|
|
.reduce(function(groups, curChar) {
|
|
|
|
var curGroupClass, curCharCode;
|
2015-04-14 18:05:24 +02:00
|
|
|
if (!caseSensitive) { curChar = curChar.toLowerCase(); }
|
2015-04-11 05:11:52 +02:00
|
|
|
curGroupClass = /^\d$/.test(curChar) ? 1 :
|
|
|
|
/^[A-Z]$/.test(curChar) ? 2 : /^[a-z]$/.test(curChar) ? 3 : 0;
|
|
|
|
if (collectSymbols && curGroupClass === 0) {
|
|
|
|
symbols += curChar;
|
|
|
|
} else {
|
|
|
|
curCharCode = curChar.charCodeAt(0);
|
|
|
|
if (curGroupClass && curGroupClass === groupClass &&
|
|
|
|
curCharCode === charCode + 1) {
|
|
|
|
group.push(curChar);
|
|
|
|
} else {
|
|
|
|
groups = addGroup(groups, group);
|
|
|
|
group = [curChar];
|
|
|
|
groupClass = curGroupClass;
|
|
|
|
}
|
|
|
|
charCode = curCharCode;
|
|
|
|
}
|
|
|
|
return groups;
|
|
|
|
}, []);
|
|
|
|
values = addGroup(values, group); // last group
|
|
|
|
if (symbols) { values.push(symbols); suppressed = true; }
|
|
|
|
return {values: values, suppressed: suppressed};
|
|
|
|
}
|
|
|
|
|
2016-04-27 15:02:12 +02:00
|
|
|
function joinChunks(chunks, suppressed) {
|
|
|
|
return chunks.join(chunks.length > 2 ? ', ' : suppressed ? ' / ' : '/');
|
|
|
|
}
|
2015-04-11 05:11:52 +02:00
|
|
|
|
2015-04-13 08:55:33 +02:00
|
|
|
function getPhContent(param, options) {
|
2015-04-20 07:53:22 +02:00
|
|
|
var text, values, resCharlist = {}, arg;
|
2015-04-14 18:05:24 +02:00
|
|
|
if (options.phContent) {
|
|
|
|
text = options.phContent(param, options);
|
|
|
|
}
|
|
|
|
if (typeof text !== 'string') {
|
|
|
|
switch (param) {
|
|
|
|
case 'hideEchoBack':
|
|
|
|
case 'mask':
|
|
|
|
case 'defaultInput':
|
|
|
|
case 'caseSensitive':
|
|
|
|
case 'keepWhitespace':
|
|
|
|
case 'encoding':
|
|
|
|
case 'bufferSize':
|
|
|
|
case 'history':
|
|
|
|
case 'cd':
|
|
|
|
text = !options.hasOwnProperty(param) ? '' :
|
|
|
|
typeof options[param] === 'boolean' ? (options[param] ? 'on' : 'off') :
|
|
|
|
options[param] + '';
|
|
|
|
break;
|
|
|
|
// case 'prompt':
|
|
|
|
// case 'query':
|
|
|
|
// case 'display':
|
|
|
|
// text = options.hasOwnProperty('displaySrc') ? options.displaySrc + '' : '';
|
|
|
|
// break;
|
|
|
|
case 'limit':
|
|
|
|
case 'trueValue':
|
|
|
|
case 'falseValue':
|
|
|
|
values = options[options.hasOwnProperty(param + 'Src') ? param + 'Src' : param];
|
|
|
|
if (options.keyIn) { // suppress
|
|
|
|
resCharlist = array2charlist(values, options.caseSensitive);
|
|
|
|
values = resCharlist.values;
|
|
|
|
} else {
|
|
|
|
values = values.filter(function(value) {
|
|
|
|
var type = typeof value;
|
|
|
|
return type === 'string' || type === 'number';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
text = joinChunks(values, resCharlist.suppressed);
|
|
|
|
break;
|
|
|
|
case 'limitCount':
|
|
|
|
case 'limitCountNotZero':
|
|
|
|
text = options[options.hasOwnProperty('limitSrc') ?
|
|
|
|
'limitSrc' : 'limit'].length;
|
|
|
|
text = text || param !== 'limitCountNotZero' ? text + '' : '';
|
|
|
|
break;
|
|
|
|
case 'lastInput':
|
|
|
|
text = lastInput;
|
|
|
|
break;
|
|
|
|
case 'cwd':
|
|
|
|
case 'CWD':
|
|
|
|
case 'cwdHome':
|
|
|
|
text = process.cwd();
|
2016-04-27 15:02:12 +02:00
|
|
|
if (param === 'CWD') {
|
|
|
|
text = pathUtil.basename(text);
|
|
|
|
} else if (param === 'cwdHome') {
|
|
|
|
text = replaceHomePath(text);
|
|
|
|
}
|
2015-04-14 18:05:24 +02:00
|
|
|
break;
|
|
|
|
case 'date':
|
|
|
|
case 'time':
|
|
|
|
case 'localeDate':
|
|
|
|
case 'localeTime':
|
|
|
|
text = (new Date())['to' +
|
|
|
|
param.replace(/^./, function(str) { return str.toUpperCase(); }) +
|
|
|
|
'String']();
|
|
|
|
break;
|
|
|
|
default: // with arg
|
2016-04-27 15:02:12 +02:00
|
|
|
if (typeof (arg = (param.match(/^history_m(\d+)$/) || [])[1]) === 'string') {
|
2015-04-14 18:05:24 +02:00
|
|
|
text = inputHistory[inputHistory.length - arg] || '';
|
|
|
|
}
|
|
|
|
}
|
2015-04-07 11:38:14 +02:00
|
|
|
}
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2015-04-13 08:55:33 +02:00
|
|
|
function getPhCharlist(param) {
|
2015-04-11 05:11:52 +02:00
|
|
|
var matches = /^(.)-(.)$/.exec(param), text = '', from, to, code, step;
|
2016-04-27 15:02:12 +02:00
|
|
|
if (!matches) { return null; }
|
2015-04-11 05:11:52 +02:00
|
|
|
from = matches[1].charCodeAt(0);
|
|
|
|
to = matches[2].charCodeAt(0);
|
|
|
|
step = from < to ? 1 : -1;
|
2016-04-27 15:26:19 +02:00
|
|
|
for (code = from; code !== to + step; code += step) { text += String.fromCharCode(code); }
|
2015-04-11 05:11:52 +02:00
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2015-04-17 11:08:22 +02:00
|
|
|
// cmd "arg" " a r g " "" 'a"r"g' "a""rg" "arg
|
|
|
|
function parseCl(cl) {
|
2015-04-16 12:08:04 +02:00
|
|
|
var reToken = new RegExp(/(\s*)(?:("|')(.*?)(?:\2|$)|(\S+))/g), matches,
|
|
|
|
taken = '', args = [], part;
|
|
|
|
cl = cl.trim();
|
|
|
|
while ((matches = reToken.exec(cl))) {
|
2015-04-17 11:08:22 +02:00
|
|
|
part = matches[3] || matches[4] || '';
|
|
|
|
if (matches[1]) {
|
2015-04-16 12:08:04 +02:00
|
|
|
args.push(taken);
|
|
|
|
taken = '';
|
|
|
|
}
|
|
|
|
taken += part;
|
|
|
|
}
|
|
|
|
if (taken) { args.push(taken); }
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
2015-04-17 11:08:22 +02:00
|
|
|
function toBool(res, options) {
|
|
|
|
return (
|
|
|
|
(options.trueValue.length &&
|
|
|
|
isMatched(res, options.trueValue, options.caseSensitive)) ? true :
|
|
|
|
(options.falseValue.length &&
|
|
|
|
isMatched(res, options.falseValue, options.caseSensitive)) ? false : res);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getValidLine(options) {
|
|
|
|
var res, forceNext, limitMessage,
|
|
|
|
matches, histInput, args, resCheck;
|
2016-04-27 15:02:12 +02:00
|
|
|
|
2015-04-13 08:55:33 +02:00
|
|
|
function _getPhContent(param) { return getPhContent(param, options); }
|
2016-04-27 15:02:12 +02:00
|
|
|
function addDisplay(text) { options.display += (/[^\r\n]$/.test(options.display) ? '\n' : '') + text; }
|
2015-04-13 08:55:33 +02:00
|
|
|
|
2015-04-07 11:38:14 +02:00
|
|
|
options.limitSrc = options.limit;
|
|
|
|
options.displaySrc = options.display;
|
2015-04-05 13:05:23 +02:00
|
|
|
options.limit = ''; // for readlineExt
|
2015-04-13 08:55:33 +02:00
|
|
|
options.display = replacePlaceholder(options.display + '', _getPhContent);
|
|
|
|
|
2015-04-05 13:05:23 +02:00
|
|
|
while (true) {
|
|
|
|
res = _readlineSync(options);
|
2015-04-13 08:55:33 +02:00
|
|
|
forceNext = false;
|
|
|
|
limitMessage = '';
|
|
|
|
|
2015-04-16 12:08:04 +02:00
|
|
|
if (options.defaultInput && !res) { res = options.defaultInput; }
|
|
|
|
|
2015-04-13 08:55:33 +02:00
|
|
|
if (options.history) {
|
|
|
|
if ((matches = /^\s*\!(?:\!|-1)(:p)?\s*$/.exec(res))) { // `!!` `!-1` +`:p`
|
|
|
|
histInput = inputHistory[0] || '';
|
2016-04-27 15:02:12 +02:00
|
|
|
if (matches[1]) { // only display
|
|
|
|
forceNext = true;
|
|
|
|
} else { // replace input
|
|
|
|
res = histInput;
|
|
|
|
}
|
2015-04-13 08:55:33 +02:00
|
|
|
// Show it even if it is empty (NL only).
|
2015-04-16 12:08:04 +02:00
|
|
|
addDisplay(histInput + '\n');
|
2015-04-13 08:55:33 +02:00
|
|
|
if (!forceNext) { // Loop may break
|
|
|
|
options.displayOnly = true;
|
|
|
|
_readlineSync(options);
|
|
|
|
options.displayOnly = false;
|
|
|
|
}
|
|
|
|
} else if (res && res !== inputHistory[inputHistory.length - 1]) {
|
|
|
|
inputHistory = [res];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-17 11:08:22 +02:00
|
|
|
if (!forceNext && options.cd && res) {
|
|
|
|
args = parseCl(res);
|
2016-05-02 12:56:06 +02:00
|
|
|
switch (args[0].toLowerCase()) {
|
2015-04-16 12:08:04 +02:00
|
|
|
case 'cd':
|
|
|
|
if (args[1]) {
|
|
|
|
try {
|
2015-04-20 07:53:22 +02:00
|
|
|
process.chdir(replaceHomePath(args[1], true));
|
2015-04-16 12:08:04 +02:00
|
|
|
} catch (e) {
|
|
|
|
addDisplay(e + '');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
forceNext = true;
|
|
|
|
break;
|
|
|
|
case 'pwd':
|
|
|
|
addDisplay(process.cwd());
|
|
|
|
forceNext = true;
|
|
|
|
break;
|
2016-05-02 12:56:06 +02:00
|
|
|
// no default
|
2015-04-16 12:08:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-17 11:08:22 +02:00
|
|
|
if (!forceNext && options.preCheck) {
|
|
|
|
resCheck = options.preCheck(res, options);
|
2015-04-13 08:55:33 +02:00
|
|
|
res = resCheck.res;
|
|
|
|
if (resCheck.forceNext) { forceNext = true; } // Don't switch to false.
|
|
|
|
}
|
2015-04-16 12:08:04 +02:00
|
|
|
|
2015-04-13 08:55:33 +02:00
|
|
|
if (!forceNext) {
|
|
|
|
if (!options.limitSrc.length ||
|
|
|
|
isMatched(res, options.limitSrc, options.caseSensitive)) { break; }
|
2016-04-27 15:02:12 +02:00
|
|
|
if (options.limitMessage) {
|
|
|
|
limitMessage = replacePlaceholder(options.limitMessage, _getPhContent);
|
|
|
|
}
|
2015-04-13 08:55:33 +02:00
|
|
|
}
|
|
|
|
|
2015-04-16 12:08:04 +02:00
|
|
|
addDisplay((limitMessage ? limitMessage + '\n' : '') +
|
|
|
|
replacePlaceholder(options.displaySrc + '', _getPhContent));
|
2015-04-05 13:05:23 +02:00
|
|
|
}
|
2015-04-13 08:55:33 +02:00
|
|
|
return toBool(res, options);
|
2015-04-05 13:05:23 +02:00
|
|
|
}
|
|
|
|
|
2014-06-27 03:06:29 +02:00
|
|
|
// for dev
|
2015-04-11 05:11:52 +02:00
|
|
|
exports._DBG_set_useExt = function(val) { _DBG_useExt = val; };
|
|
|
|
exports._DBG_set_checkOptions = function(val) { _DBG_checkOptions = val; };
|
|
|
|
exports._DBG_set_checkMethod = function(val) { _DBG_checkMethod = val; };
|
2015-04-13 08:55:33 +02:00
|
|
|
exports._DBG_clearHistory = function() { lastInput = ''; inputHistory = []; };
|
2013-08-29 19:26:22 +02:00
|
|
|
|
2015-04-20 07:53:22 +02:00
|
|
|
// ------------------------------------
|
|
|
|
|
|
|
|
exports.setDefaultOptions = function(options) {
|
2015-04-07 11:38:14 +02:00
|
|
|
defaultOptions = margeOptions(true, options);
|
|
|
|
return margeOptions(true); // copy
|
|
|
|
};
|
|
|
|
|
2014-07-12 00:37:25 +02:00
|
|
|
exports.question = function(query, options) {
|
2016-04-27 15:02:12 +02:00
|
|
|
/* eslint-disable key-spacing */
|
2015-04-14 18:05:24 +02:00
|
|
|
return getValidLine(margeOptions(margeOptions(true, options), {
|
2016-04-27 15:02:12 +02:00
|
|
|
display: query
|
|
|
|
}));
|
|
|
|
/* eslint-enable key-spacing */
|
2013-08-29 19:26:22 +02:00
|
|
|
};
|
2015-03-01 15:46:52 +01:00
|
|
|
|
2015-04-22 11:51:25 +02:00
|
|
|
exports.prompt = function(options) {
|
|
|
|
var readOptions = margeOptions(true, options);
|
|
|
|
readOptions.display = readOptions.prompt;
|
|
|
|
return getValidLine(readOptions);
|
|
|
|
};
|
|
|
|
|
2015-03-07 16:19:40 +01:00
|
|
|
exports.keyIn = function(query, options) {
|
2016-04-27 15:02:12 +02:00
|
|
|
/* eslint-disable key-spacing */
|
2015-04-05 11:11:30 +02:00
|
|
|
var readOptions = margeOptions(margeOptions(true, options), {
|
2016-04-27 15:02:12 +02:00
|
|
|
display: query,
|
|
|
|
keyIn: true,
|
|
|
|
keepWhitespace: true
|
|
|
|
});
|
|
|
|
/* eslint-enable key-spacing */
|
2015-04-07 11:38:14 +02:00
|
|
|
|
|
|
|
// char list
|
2015-04-11 05:11:52 +02:00
|
|
|
readOptions.limitSrc = readOptions.limit.filter(function(value) {
|
2016-04-27 15:02:12 +02:00
|
|
|
var type = typeof value;
|
|
|
|
return type === 'string' || type === 'number';
|
|
|
|
})
|
|
|
|
.map(function(text) { return replacePlaceholder(text + '', getPhCharlist); });
|
2015-04-07 11:38:14 +02:00
|
|
|
// pattern
|
2016-02-23 17:20:41 +01:00
|
|
|
readOptions.limit = escapePattern(readOptions.limitSrc.join(''));
|
2015-04-05 16:14:48 +02:00
|
|
|
|
2015-04-07 11:38:14 +02:00
|
|
|
['trueValue', 'falseValue'].forEach(function(optionName) {
|
2015-04-20 09:43:12 +02:00
|
|
|
readOptions[optionName] = readOptions[optionName].reduce(function(comps, comp) {
|
2015-04-11 05:11:52 +02:00
|
|
|
var type = typeof comp;
|
|
|
|
if (type === 'string' || type === 'number') {
|
2015-04-05 16:14:48 +02:00
|
|
|
comps = comps.concat((comp + '').split(''));
|
2015-04-20 09:43:12 +02:00
|
|
|
} else { comps.push(comp); }
|
|
|
|
return comps;
|
|
|
|
}, []);
|
2015-04-05 16:14:48 +02:00
|
|
|
});
|
|
|
|
|
2015-04-07 11:38:14 +02:00
|
|
|
readOptions.display = replacePlaceholder(readOptions.display + '',
|
2015-04-13 08:55:33 +02:00
|
|
|
function(param) { return getPhContent(param, readOptions); });
|
2015-04-05 07:45:42 +02:00
|
|
|
|
2015-04-13 08:55:33 +02:00
|
|
|
return toBool(_readlineSync(readOptions), readOptions);
|
2015-03-01 15:46:52 +01:00
|
|
|
};
|
2015-04-05 07:45:42 +02:00
|
|
|
|
2015-04-11 05:11:52 +02:00
|
|
|
// ------------------------------------
|
|
|
|
|
|
|
|
exports.questionEMail = function(query, options) {
|
2016-04-27 15:02:12 +02:00
|
|
|
if (query == null) { query = 'Input e-mail address :'; } // eslint-disable-line eqeqeq
|
|
|
|
/* eslint-disable key-spacing */
|
2015-04-11 05:11:52 +02:00
|
|
|
return exports.question(query, margeOptions({
|
2016-04-27 15:02:12 +02:00
|
|
|
// -------- default
|
|
|
|
hideEchoBack: false,
|
|
|
|
// http://www.w3.org/TR/html5/forms.html#valid-e-mail-address
|
|
|
|
limit: /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/,
|
|
|
|
limitMessage: 'Input valid e-mail address, please.',
|
|
|
|
trueValue: null,
|
|
|
|
falseValue: null
|
|
|
|
}, options, {
|
|
|
|
// -------- forced
|
|
|
|
keepWhitespace: false,
|
|
|
|
cd: false
|
|
|
|
}));
|
|
|
|
/* eslint-enable key-spacing */
|
2015-04-11 05:11:52 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.questionNewPassword = function(query, options) {
|
2016-04-27 15:02:12 +02:00
|
|
|
/* eslint-disable key-spacing */
|
|
|
|
var resCharlist, min, max,
|
|
|
|
readOptions = margeOptions({
|
2015-04-11 05:11:52 +02:00
|
|
|
// -------- default
|
|
|
|
hideEchoBack: true,
|
|
|
|
mask: '*',
|
2016-02-23 17:20:41 +01:00
|
|
|
limitMessage: 'It can include: $<charlist>\n' +
|
|
|
|
'And the length must be: $<length>',
|
2015-04-11 05:11:52 +02:00
|
|
|
trueValue: null,
|
|
|
|
falseValue: null,
|
2015-04-13 08:55:33 +02:00
|
|
|
caseSensitive: true
|
2015-04-14 18:05:24 +02:00
|
|
|
}, options, {
|
|
|
|
// -------- forced
|
|
|
|
history: false,
|
|
|
|
cd: false,
|
|
|
|
// limit (by charlist etc.),
|
|
|
|
phContent: function(param) {
|
2016-04-27 15:02:12 +02:00
|
|
|
return param === 'charlist' ? resCharlist.text :
|
|
|
|
param === 'length' ? min + '...' + max : null;
|
|
|
|
}
|
2015-04-14 18:05:24 +02:00
|
|
|
}),
|
2015-04-16 12:08:04 +02:00
|
|
|
// added: charlist, min, max, confirmMessage, unmatchMessage
|
2016-04-27 15:02:12 +02:00
|
|
|
charlist, confirmMessage, unmatchMessage,
|
|
|
|
limit, limitMessage, res1, res2;
|
|
|
|
/* eslint-enable key-spacing */
|
2015-04-16 12:08:04 +02:00
|
|
|
options = options || {};
|
2015-04-13 08:55:33 +02:00
|
|
|
|
2015-04-14 18:05:24 +02:00
|
|
|
charlist = replacePlaceholder(
|
2016-02-23 17:20:41 +01:00
|
|
|
options.charlist ? options.charlist + '' : '$<!-~>', getPhCharlist);
|
2015-04-16 12:08:04 +02:00
|
|
|
if (isNaN(min = parseInt(options.min, 10)) || typeof min !== 'number') { min = 12; }
|
|
|
|
if (isNaN(max = parseInt(options.max, 10)) || typeof max !== 'number') { max = 24; }
|
2016-02-23 17:20:41 +01:00
|
|
|
limit = new RegExp('^[' + escapePattern(charlist) +
|
2015-04-14 18:05:24 +02:00
|
|
|
']{' + min + ',' + max + '}$');
|
|
|
|
resCharlist = array2charlist([charlist], readOptions.caseSensitive, true);
|
|
|
|
resCharlist.text = joinChunks(resCharlist.values, resCharlist.suppressed);
|
|
|
|
|
2016-04-27 15:02:12 +02:00
|
|
|
confirmMessage = options.confirmMessage != null ? options.confirmMessage : // eslint-disable-line eqeqeq
|
2015-04-23 15:59:41 +02:00
|
|
|
'Reinput a same one to confirm it :';
|
2016-04-27 15:02:12 +02:00
|
|
|
unmatchMessage = options.unmatchMessage != null ? options.unmatchMessage : // eslint-disable-line eqeqeq
|
2015-04-16 12:08:04 +02:00
|
|
|
'It differs from first one.' +
|
2015-08-11 10:13:26 +02:00
|
|
|
' Hit only the Enter key if you want to retry from first one.';
|
2015-04-13 08:55:33 +02:00
|
|
|
|
2016-04-27 15:02:12 +02:00
|
|
|
if (query == null) { query = 'Input new password :'; } // eslint-disable-line eqeqeq
|
2015-04-11 05:11:52 +02:00
|
|
|
|
2015-04-14 18:05:24 +02:00
|
|
|
limitMessage = readOptions.limitMessage;
|
2015-04-11 05:11:52 +02:00
|
|
|
while (!res2) {
|
2015-04-14 18:05:24 +02:00
|
|
|
readOptions.limit = limit;
|
|
|
|
readOptions.limitMessage = limitMessage;
|
2015-04-11 05:11:52 +02:00
|
|
|
res1 = exports.question(query, readOptions);
|
|
|
|
|
|
|
|
readOptions.limit = [res1, ''];
|
2015-04-16 12:08:04 +02:00
|
|
|
readOptions.limitMessage = unmatchMessage;
|
|
|
|
res2 = exports.question(confirmMessage, readOptions);
|
2015-04-11 05:11:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return res1;
|
|
|
|
};
|
|
|
|
|
2015-04-14 18:05:24 +02:00
|
|
|
function _questionNum(query, options, parser) {
|
2015-04-13 08:55:33 +02:00
|
|
|
var validValue;
|
|
|
|
function getValidValue(value) {
|
2015-04-14 18:05:24 +02:00
|
|
|
validValue = parser(value);
|
2015-04-13 08:55:33 +02:00
|
|
|
return !isNaN(validValue) && typeof validValue === 'number';
|
|
|
|
}
|
2016-04-27 15:02:12 +02:00
|
|
|
/* eslint-disable key-spacing */
|
2015-04-14 18:05:24 +02:00
|
|
|
exports.question(query, margeOptions({
|
2016-04-27 15:02:12 +02:00
|
|
|
// -------- default
|
|
|
|
limitMessage: 'Input valid number, please.'
|
|
|
|
}, options, {
|
|
|
|
// -------- forced
|
|
|
|
limit: getValidValue,
|
|
|
|
cd: false
|
|
|
|
// trueValue, falseValue, caseSensitive, keepWhitespace don't work.
|
|
|
|
}));
|
|
|
|
/* eslint-enable key-spacing */
|
2015-04-13 08:55:33 +02:00
|
|
|
return validValue;
|
|
|
|
}
|
|
|
|
exports.questionInt = function(query, options) {
|
2015-04-14 18:05:24 +02:00
|
|
|
return _questionNum(query, options, function(value) { return parseInt(value, 10); });
|
|
|
|
};
|
|
|
|
exports.questionFloat = function(query, options) {
|
|
|
|
return _questionNum(query, options, parseFloat);
|
2015-04-13 08:55:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.questionPath = function(query, options) {
|
2016-04-27 15:02:12 +02:00
|
|
|
/* eslint-disable key-spacing */
|
|
|
|
var validPath, error = '',
|
|
|
|
readOptions = margeOptions({
|
2015-04-14 18:05:24 +02:00
|
|
|
// -------- default
|
2015-04-16 12:08:04 +02:00
|
|
|
hideEchoBack: false,
|
2016-02-23 17:20:41 +01:00
|
|
|
limitMessage: '$<error(\n)>Input valid path, please.' +
|
|
|
|
'$<( Min:)min>$<( Max:)max>',
|
2015-04-14 18:05:24 +02:00
|
|
|
history: true,
|
|
|
|
cd: true
|
|
|
|
}, options, {
|
2015-04-13 08:55:33 +02:00
|
|
|
// -------- forced
|
2015-04-14 18:05:24 +02:00
|
|
|
keepWhitespace: false,
|
|
|
|
limit: function(value) {
|
2016-04-27 15:02:12 +02:00
|
|
|
var exists, stat, res;
|
|
|
|
value = replaceHomePath(value, true);
|
|
|
|
error = ''; // for validate
|
|
|
|
// mkdir -p
|
|
|
|
function mkdirParents(dirPath) {
|
|
|
|
dirPath.split(/\/|\\/).reduce(function(parents, dir) {
|
|
|
|
var path = pathUtil.resolve((parents += dir + pathUtil.sep));
|
|
|
|
if (!fs.existsSync(path)) {
|
|
|
|
fs.mkdirSync(path);
|
|
|
|
} else if (!fs.statSync(path).isDirectory()) {
|
|
|
|
throw new Error('Non directory already exists: ' + path);
|
2015-04-16 12:08:04 +02:00
|
|
|
}
|
2016-04-27 15:02:12 +02:00
|
|
|
return parents;
|
|
|
|
}, '');
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
exists = fs.existsSync(value);
|
|
|
|
validPath = exists ? fs.realpathSync(value) : pathUtil.resolve(value);
|
|
|
|
// options.exists default: true, not-bool: no-check
|
|
|
|
if (!options.hasOwnProperty('exists') && !exists ||
|
|
|
|
typeof options.exists === 'boolean' && options.exists !== exists) {
|
|
|
|
error = (exists ? 'Already exists' : 'No such file or directory') +
|
|
|
|
': ' + validPath;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!exists && options.create) {
|
|
|
|
if (options.isDirectory) {
|
|
|
|
mkdirParents(validPath);
|
|
|
|
} else {
|
|
|
|
mkdirParents(pathUtil.dirname(validPath));
|
|
|
|
fs.closeSync(fs.openSync(validPath, 'w')); // touch
|
2015-04-16 12:08:04 +02:00
|
|
|
}
|
2016-04-27 15:02:12 +02:00
|
|
|
validPath = fs.realpathSync(validPath);
|
|
|
|
}
|
|
|
|
if (exists && (options.min || options.max ||
|
|
|
|
options.isFile || options.isDirectory)) {
|
|
|
|
stat = fs.statSync(validPath);
|
|
|
|
// type check first (directory has zero size)
|
|
|
|
if (options.isFile && !stat.isFile()) {
|
|
|
|
error = 'Not file: ' + validPath;
|
|
|
|
return false;
|
|
|
|
} else if (options.isDirectory && !stat.isDirectory()) {
|
|
|
|
error = 'Not directory: ' + validPath;
|
|
|
|
return false;
|
|
|
|
} else if (options.min && stat.size < +options.min ||
|
|
|
|
options.max && stat.size > +options.max) {
|
|
|
|
error = 'Size ' + stat.size + ' is out of range: ' + validPath;
|
|
|
|
return false;
|
2015-04-14 18:05:24 +02:00
|
|
|
}
|
|
|
|
}
|
2016-04-27 15:02:12 +02:00
|
|
|
if (typeof options.validate === 'function' &&
|
|
|
|
(res = options.validate(validPath)) !== true) {
|
|
|
|
if (typeof res === 'string') { error = res; }
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
error = e + '';
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
2015-05-23 06:56:40 +02:00
|
|
|
// trueValue, falseValue, caseSensitive don't work.
|
2015-04-14 18:05:24 +02:00
|
|
|
phContent: function(param) {
|
2016-04-27 15:02:12 +02:00
|
|
|
return param === 'error' ? error :
|
|
|
|
param !== 'min' && param !== 'max' ? null :
|
|
|
|
options.hasOwnProperty(param) ? options[param] + '' : '';
|
|
|
|
}
|
|
|
|
});
|
2015-04-20 07:53:22 +02:00
|
|
|
// added: exists, create, min, max, isFile, isDirectory, validate
|
2016-04-27 15:02:12 +02:00
|
|
|
/* eslint-enable key-spacing */
|
2015-04-16 12:08:04 +02:00
|
|
|
options = options || {};
|
2015-04-14 18:05:24 +02:00
|
|
|
|
2016-04-27 15:02:12 +02:00
|
|
|
if (query == null) { query = 'Input path (you can "cd" and "pwd") :'; } // eslint-disable-line eqeqeq
|
2015-04-14 18:05:24 +02:00
|
|
|
|
|
|
|
exports.question(query, readOptions);
|
2015-04-13 08:55:33 +02:00
|
|
|
return validPath;
|
|
|
|
};
|
|
|
|
|
2015-04-17 11:08:22 +02:00
|
|
|
// props: preCheck, args, hRes, limit
|
|
|
|
function getClHandler(commandHandler, options) {
|
|
|
|
var clHandler = {}, hIndex = {};
|
|
|
|
if (typeof commandHandler === 'object') {
|
|
|
|
Object.keys(commandHandler).forEach(function(cmd) {
|
|
|
|
if (typeof commandHandler[cmd] === 'function') {
|
|
|
|
hIndex[options.caseSensitive ? cmd : cmd.toLowerCase()] = commandHandler[cmd];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
clHandler.preCheck = function(res) {
|
|
|
|
var cmdKey;
|
|
|
|
clHandler.args = parseCl(res);
|
|
|
|
cmdKey = clHandler.args[0] || '';
|
|
|
|
if (!options.caseSensitive) { cmdKey = cmdKey.toLowerCase(); }
|
|
|
|
clHandler.hRes =
|
2015-06-12 09:08:20 +02:00
|
|
|
cmdKey !== '_' && hIndex.hasOwnProperty(cmdKey) ?
|
2015-04-17 11:08:22 +02:00
|
|
|
hIndex[cmdKey].apply(res, clHandler.args.slice(1)) :
|
|
|
|
hIndex.hasOwnProperty('_') ? hIndex._.apply(res, clHandler.args) : null;
|
|
|
|
return {res: res, forceNext: false};
|
|
|
|
};
|
|
|
|
if (!hIndex.hasOwnProperty('_')) {
|
|
|
|
clHandler.limit = function() { // It's called after preCheck.
|
|
|
|
var cmdKey = clHandler.args[0] || '';
|
|
|
|
if (!options.caseSensitive) { cmdKey = cmdKey.toLowerCase(); }
|
|
|
|
return hIndex.hasOwnProperty(cmdKey);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
clHandler.preCheck = function(res) {
|
|
|
|
clHandler.args = parseCl(res);
|
|
|
|
clHandler.hRes = typeof commandHandler === 'function' ?
|
2015-04-20 07:53:22 +02:00
|
|
|
commandHandler.apply(res, clHandler.args) : true; // true for break loop
|
2015-04-17 11:08:22 +02:00
|
|
|
return {res: res, forceNext: false};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return clHandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.promptCL = function(commandHandler, options) {
|
2016-04-27 15:02:12 +02:00
|
|
|
/* eslint-disable key-spacing */
|
2015-04-17 11:08:22 +02:00
|
|
|
var readOptions = margeOptions({
|
2015-04-13 08:55:33 +02:00
|
|
|
// -------- default
|
2015-04-16 12:08:04 +02:00
|
|
|
hideEchoBack: false,
|
2015-04-17 11:08:22 +02:00
|
|
|
limitMessage: 'Requested command is not available.',
|
|
|
|
caseSensitive: false,
|
2015-04-14 18:05:24 +02:00
|
|
|
history: true
|
2015-04-17 11:08:22 +02:00
|
|
|
}, options),
|
2015-04-14 18:05:24 +02:00
|
|
|
// -------- forced
|
2015-04-22 11:51:25 +02:00
|
|
|
// trueValue, falseValue, keepWhitespace don't work.
|
2015-04-17 11:08:22 +02:00
|
|
|
// preCheck, limit (by clHandler)
|
|
|
|
clHandler = getClHandler(commandHandler, readOptions);
|
2016-04-27 15:02:12 +02:00
|
|
|
/* eslint-enable key-spacing */
|
2015-04-17 11:08:22 +02:00
|
|
|
readOptions.limit = clHandler.limit;
|
|
|
|
readOptions.preCheck = clHandler.preCheck;
|
|
|
|
exports.prompt(readOptions);
|
|
|
|
return clHandler.args;
|
2015-04-13 08:55:33 +02:00
|
|
|
};
|
|
|
|
|
2015-04-17 11:08:22 +02:00
|
|
|
exports.promptLoop = function(inputHandler, options) {
|
2016-04-27 15:02:12 +02:00
|
|
|
/* eslint-disable key-spacing */
|
2015-04-17 11:08:22 +02:00
|
|
|
var readOptions = margeOptions({
|
2016-04-27 15:02:12 +02:00
|
|
|
// -------- default
|
|
|
|
hideEchoBack: false,
|
|
|
|
trueValue: null,
|
|
|
|
falseValue: null,
|
|
|
|
caseSensitive: false,
|
|
|
|
history: true
|
|
|
|
}, options);
|
|
|
|
/* eslint-enable key-spacing */
|
2015-04-17 11:08:22 +02:00
|
|
|
while (true) { if (inputHandler(exports.prompt(readOptions))) { break; } }
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.promptCLLoop = function(commandHandler, options) {
|
2016-04-27 15:02:12 +02:00
|
|
|
/* eslint-disable key-spacing */
|
2015-04-17 11:08:22 +02:00
|
|
|
var readOptions = margeOptions({
|
|
|
|
// -------- default
|
|
|
|
hideEchoBack: false,
|
|
|
|
limitMessage: 'Requested command is not available.',
|
|
|
|
caseSensitive: false,
|
|
|
|
history: true
|
|
|
|
}, options),
|
|
|
|
// -------- forced
|
2015-04-22 11:51:25 +02:00
|
|
|
// trueValue, falseValue, keepWhitespace don't work.
|
2015-04-17 11:08:22 +02:00
|
|
|
// preCheck, limit (by clHandler)
|
|
|
|
clHandler = getClHandler(commandHandler, readOptions);
|
2016-04-27 15:02:12 +02:00
|
|
|
/* eslint-enable key-spacing */
|
2015-04-17 11:08:22 +02:00
|
|
|
readOptions.limit = clHandler.limit;
|
|
|
|
readOptions.preCheck = clHandler.preCheck;
|
|
|
|
while (true) {
|
|
|
|
exports.prompt(readOptions);
|
|
|
|
if (clHandler.hRes) { break; }
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.promptSimShell = function(options) {
|
2016-04-27 15:02:12 +02:00
|
|
|
/* eslint-disable key-spacing */
|
2015-04-16 12:08:04 +02:00
|
|
|
return exports.prompt(margeOptions({
|
2016-04-27 15:02:12 +02:00
|
|
|
// -------- default
|
|
|
|
hideEchoBack: false,
|
|
|
|
history: true
|
|
|
|
}, options, {
|
|
|
|
// -------- forced
|
|
|
|
prompt: (function() {
|
|
|
|
return IS_WIN ?
|
|
|
|
'$<cwd>>' :
|
|
|
|
// 'user@host:cwd$ '
|
|
|
|
(process.env.USER || '') +
|
|
|
|
(process.env.HOSTNAME ?
|
|
|
|
'@' + process.env.HOSTNAME.replace(/\..*$/, '') : '') +
|
|
|
|
':$<cwdHome>$ ';
|
|
|
|
})()
|
|
|
|
}));
|
|
|
|
/* eslint-enable key-spacing */
|
2015-04-16 12:08:04 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
function _keyInYN(query, options, limit) {
|
2015-04-14 18:05:24 +02:00
|
|
|
var res;
|
2016-04-27 15:02:12 +02:00
|
|
|
if (query == null) { query = 'Are you sure? :'; } // eslint-disable-line eqeqeq
|
|
|
|
if ((!options || options.guide !== false) && (query += '')) {
|
|
|
|
query = query.replace(/\s*:?\s*$/, '') + ' [y/n] :';
|
|
|
|
}
|
|
|
|
/* eslint-disable key-spacing */
|
2015-04-14 18:05:24 +02:00
|
|
|
res = exports.keyIn(query, margeOptions(options, {
|
2016-04-27 15:02:12 +02:00
|
|
|
// -------- forced
|
|
|
|
hideEchoBack: false,
|
|
|
|
limit: limit,
|
|
|
|
trueValue: 'y',
|
|
|
|
falseValue: 'n',
|
|
|
|
caseSensitive: false
|
|
|
|
// mask doesn't work.
|
|
|
|
}));
|
|
|
|
// added: guide
|
|
|
|
/* eslint-enable key-spacing */
|
2015-04-13 08:55:33 +02:00
|
|
|
return typeof res === 'boolean' ? res : '';
|
2015-04-16 12:08:04 +02:00
|
|
|
}
|
|
|
|
exports.keyInYN = function(query, options) { return _keyInYN(query, options); };
|
2016-04-27 15:02:12 +02:00
|
|
|
exports.keyInYNStrict = function(query, options) { return _keyInYN(query, options, 'yn'); };
|
2015-04-11 05:11:52 +02:00
|
|
|
|
|
|
|
exports.keyInPause = function(query, options) {
|
2016-04-27 15:02:12 +02:00
|
|
|
if (query == null) { query = 'Continue...'; } // eslint-disable-line eqeqeq
|
|
|
|
if ((!options || options.guide !== false) && (query += '')) {
|
|
|
|
query = query.replace(/\s+$/, '') + ' (Hit any key)';
|
|
|
|
}
|
|
|
|
/* eslint-disable key-spacing */
|
2015-04-16 12:08:04 +02:00
|
|
|
exports.keyIn(query, margeOptions({
|
2016-04-27 15:02:12 +02:00
|
|
|
// -------- default
|
|
|
|
limit: null
|
|
|
|
}, options, {
|
|
|
|
// -------- forced
|
|
|
|
hideEchoBack: true,
|
|
|
|
mask: ''
|
|
|
|
}));
|
|
|
|
// added: guide
|
|
|
|
/* eslint-enable key-spacing */
|
2015-04-11 05:11:52 +02:00
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
2015-04-17 11:08:22 +02:00
|
|
|
exports.keyInSelect = function(items, query, options) {
|
2016-04-27 15:02:12 +02:00
|
|
|
/* eslint-disable key-spacing */
|
2015-04-11 05:11:52 +02:00
|
|
|
var readOptions = margeOptions({
|
|
|
|
// -------- default
|
2015-04-14 18:05:24 +02:00
|
|
|
hideEchoBack: false
|
2015-04-11 05:11:52 +02:00
|
|
|
}, options, {
|
|
|
|
// -------- forced
|
|
|
|
trueValue: null,
|
2015-04-13 08:55:33 +02:00
|
|
|
falseValue: null,
|
2016-01-27 08:42:29 +01:00
|
|
|
caseSensitive: false,
|
|
|
|
// limit (by items),
|
|
|
|
phContent: function(param) {
|
2016-04-27 15:02:12 +02:00
|
|
|
return param === 'itemsCount' ? items.length + '' :
|
|
|
|
param === 'firstItem' ? (items[0] + '').trim() :
|
|
|
|
param === 'lastItem' ? (items[items.length - 1] + '').trim() : null;
|
|
|
|
}
|
2015-04-14 18:05:24 +02:00
|
|
|
}),
|
2015-04-16 12:08:04 +02:00
|
|
|
// added: guide, cancel
|
2015-04-14 18:05:24 +02:00
|
|
|
keylist = '', key2i = {}, charCode = 49 /* '1' */, display = '\n';
|
2016-04-27 15:02:12 +02:00
|
|
|
/* eslint-enable key-spacing */
|
|
|
|
if (!Array.isArray(items) || !items.length || items.length > 35) {
|
|
|
|
throw '`items` must be Array (max length: 35).';
|
|
|
|
}
|
2015-04-11 05:11:52 +02:00
|
|
|
|
|
|
|
items.forEach(function(item, i) {
|
|
|
|
var key = String.fromCharCode(charCode);
|
|
|
|
keylist += key;
|
|
|
|
key2i[key] = i;
|
2016-01-27 08:42:29 +01:00
|
|
|
display += '[' + key + '] ' + (item + '').trim() + '\n';
|
2015-04-14 18:05:24 +02:00
|
|
|
charCode = charCode === 57 /* '9' */ ? 97 /* 'a' */ : charCode + 1;
|
2015-04-11 05:11:52 +02:00
|
|
|
});
|
2015-04-14 18:05:24 +02:00
|
|
|
if (!options || options.cancel !== false) {
|
2015-04-11 05:11:52 +02:00
|
|
|
keylist += '0';
|
|
|
|
key2i['0'] = -1;
|
2016-04-27 15:02:12 +02:00
|
|
|
display += '[0] ' +
|
|
|
|
(options && options.cancel != null && typeof options.cancel !== 'boolean' ? // eslint-disable-line eqeqeq
|
2016-01-27 08:42:29 +01:00
|
|
|
(options.cancel + '').trim() : 'CANCEL') + '\n';
|
2015-04-11 05:11:52 +02:00
|
|
|
}
|
|
|
|
readOptions.limit = keylist;
|
|
|
|
display += '\n';
|
|
|
|
|
2016-04-27 15:02:12 +02:00
|
|
|
if (query == null) { query = 'Choose one from list :'; } // eslint-disable-line eqeqeq
|
2015-04-11 05:11:52 +02:00
|
|
|
if ((query += '')) {
|
2016-04-27 15:02:12 +02:00
|
|
|
if (!options || options.guide !== false) {
|
|
|
|
query = query.replace(/\s*:?\s*$/, '') + ' [$<limit>] :';
|
|
|
|
}
|
2015-04-11 05:11:52 +02:00
|
|
|
display += query;
|
|
|
|
}
|
|
|
|
|
2015-04-14 18:05:24 +02:00
|
|
|
return key2i[exports.keyIn(display, readOptions).toLowerCase()];
|
2015-04-11 05:11:52 +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]; }
|
2015-04-20 07:53:22 +02:00
|
|
|
return exports.setDefaultOptions(options)[optionName];
|
2015-04-05 08:39:16 +02:00
|
|
|
}
|
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); };
|