format code

This commit is contained in:
anseki 2019-07-27 21:52:40 +09:00
parent 84eae4ae65
commit 12985521cf
2 changed files with 109 additions and 82 deletions

View file

@ -3,6 +3,9 @@
"extends": "../../../_common/eslintrc.json", "extends": "../../../_common/eslintrc.json",
"env": {"node": true}, "env": {"node": true},
"rules": { "rules": {
"no-var": "off",
"prefer-arrow-callback": "off",
"object-shorthand": "off",
"no-underscore-dangle": [2, {"allow": ["_DBG_useExt", "_DBG_checkOptions", "_DBG_checkMethod", "_readlineSync", "_execFileSync", "_handle", "_flattenArray", "_getPhContent", "_DBG_set_useExt", "_DBG_set_checkOptions", "_DBG_set_checkMethod", "_DBG_clearHistory", "_questionNum", "_keyInYN", "_setOption"]}], "no-underscore-dangle": [2, {"allow": ["_DBG_useExt", "_DBG_checkOptions", "_DBG_checkMethod", "_readlineSync", "_execFileSync", "_handle", "_flattenArray", "_getPhContent", "_DBG_set_useExt", "_DBG_set_checkOptions", "_DBG_set_checkMethod", "_DBG_clearHistory", "_questionNum", "_keyInYN", "_setOption"]}],
"camelcase": 0 "camelcase": 0
} }

View file

@ -42,10 +42,15 @@ var
/* eslint-enable key-spacing */ /* eslint-enable key-spacing */
}, },
fdR = 'none', fdW, ttyR, isRawMode = false, fdR = 'none',
extHostPath, extHostArgs, tempdir, salt = 0, isRawMode = false,
lastInput = '', inputHistory = [], rawInput, salt = 0,
_DBG_useExt = false, _DBG_checkOptions = false, _DBG_checkMethod = false; lastInput = '',
inputHistory = [],
_DBG_useExt = false,
_DBG_checkOptions = false,
_DBG_checkMethod = false,
fdW, ttyR, extHostPath, extHostArgs, tempdir, rawInput;
function getHostArgs(options) { function getHostArgs(options) {
// Send any text to crazy Windows shell safely. // Send any text to crazy Windows shell safely.
@ -84,7 +89,8 @@ function getHostArgs(options) {
function _execFileSync(options, execOptions) { function _execFileSync(options, execOptions) {
function getTempfile(name) { function getTempfile(name) {
var filepath, suffix = '', fd; var suffix = '',
filepath, fd;
tempdir = tempdir || require('os').tmpdir(); tempdir = tempdir || require('os').tmpdir();
while (true) { while (true) {
@ -105,12 +111,13 @@ function _execFileSync(options, execOptions) {
return filepath; return filepath;
} }
var hostArgs, shellPath, shellArgs, res = {}, exitCode, extMessage, var res = {},
pathStdout = getTempfile('readline-sync.stdout'), pathStdout = getTempfile('readline-sync.stdout'),
pathStderr = getTempfile('readline-sync.stderr'), pathStderr = getTempfile('readline-sync.stderr'),
pathExit = getTempfile('readline-sync.exit'), pathExit = getTempfile('readline-sync.exit'),
pathDone = getTempfile('readline-sync.done'), pathDone = getTempfile('readline-sync.done'),
crypto = require('crypto'), shasum, decipher, password; crypto = require('crypto'),
hostArgs, shellPath, shellArgs, exitCode, extMessage, shasum, decipher, password;
shasum = crypto.createHash(ALGORITHM_HASH); shasum = crypto.createHash(ALGORITHM_HASH);
shasum.update('' + process.pid + (salt++) + Math.random()); shasum.update('' + process.pid + (salt++) + Math.random());
@ -179,15 +186,17 @@ function _execFileSync(options, execOptions) {
} }
function readlineExt(options) { function readlineExt(options) {
var hostArgs, res = {}, extMessage, var res = {},
execOptions = {env: process.env, encoding: options.encoding}; execOptions = {env: process.env, encoding: options.encoding},
hostArgs, extMessage;
if (!extHostPath) { if (!extHostPath) {
if (IS_WIN) { if (IS_WIN) {
if (process.env.PSModulePath) { // Windows PowerShell if (process.env.PSModulePath) { // Windows PowerShell
extHostPath = 'powershell.exe'; extHostPath = 'powershell.exe';
extHostArgs = ['-ExecutionPolicy', 'Bypass', '-File', __dirname + '\\read.ps1']; // eslint-disable-line no-path-concat extHostArgs = ['-ExecutionPolicy', 'Bypass',
} else { // Windows Script Host '-File', __dirname + '\\read.ps1']; // eslint-disable-line no-path-concat
} else { // Windows Script Host
extHostPath = 'cscript.exe'; extHostPath = 'cscript.exe';
extHostArgs = ['//nologo', __dirname + '\\read.cs.js']; // eslint-disable-line no-path-concat extHostArgs = ['//nologo', __dirname + '\\read.cs.js']; // eslint-disable-line no-path-concat
} }
@ -241,9 +250,9 @@ function readlineExt(options) {
encoding, bufferSize, print encoding, bufferSize, print
*/ */
function _readlineSync(options) { function _readlineSync(options) {
var input = '', displaySave = options.display, var input = '',
silent = !options.display && displaySave = options.display,
options.keyIn && options.hideEchoBack && !options.mask; silent = !options.display && options.keyIn && options.hideEchoBack && !options.mask;
function tryExt() { function tryExt() {
var res = readlineExt(options); var res = readlineExt(options);
@ -334,9 +343,8 @@ function _readlineSync(options) {
})(); })();
(function() { // try read (function() { // try read
var atEol, limit, var isCooked = !options.hideEchoBack && !options.keyIn,
isCooked = !options.hideEchoBack && !options.keyIn, atEol, limit, buffer, reqSize, readSize, chunk, line;
buffer, reqSize, readSize, chunk, line;
rawInput = ''; rawInput = '';
// Node.js v0.10- returns an error if same mode is set. // Node.js v0.10- returns an error if same mode is set.
@ -422,9 +430,12 @@ function _readlineSync(options) {
})(); })();
if (options.print && !silent) { if (options.print && !silent) {
options.print(displaySave + (options.displayOnly ? '' : options.print(
(options.hideEchoBack ? (new Array(input.length + 1)).join(options.mask) displaySave + (
: input) + '\n'), // must at least write '\n' options.displayOnly ? '' : (
options.hideEchoBack ? (new Array(input.length + 1)).join(options.mask) : input
) + '\n' // must at least write '\n'
),
options.encoding); options.encoding);
} }
@ -435,9 +446,8 @@ function _readlineSync(options) {
function flattenArray(array, validator) { function flattenArray(array, validator) {
var flatArray = []; var flatArray = [];
function _flattenArray(array) { function _flattenArray(array) {
if (array == null) { if (array == null) { return; }
return; if (Array.isArray(array)) {
} else if (Array.isArray(array)) {
array.forEach(_flattenArray); array.forEach(_flattenArray);
} else if (!validator || validator(array)) { } else if (!validator || validator(array)) {
flatArray.push(array); flatArray.push(array);
@ -488,8 +498,9 @@ function margeOptions() {
var value; var value;
if (!optionsPart.hasOwnProperty(optionName)) { return; } if (!optionsPart.hasOwnProperty(optionName)) { return; }
value = optionsPart[optionName]; value = optionsPart[optionName];
/* eslint-disable no-multi-spaces */
switch (optionName) { switch (optionName) {
// _readlineSync <- * * -> defaultOptions // _readlineSync <- * * -> defaultOptions
// ================ string // ================ string
case 'mask': // * * case 'mask': // * *
case 'limitMessage': // * case 'limitMessage': // *
@ -540,6 +551,7 @@ function margeOptions() {
break; break;
// no default // no default
} }
/* eslint-enable no-multi-spaces */
}); });
return options; return options;
}, {}); }, {});
@ -548,8 +560,8 @@ function margeOptions() {
function isMatched(res, comps, caseSensitive) { function isMatched(res, comps, caseSensitive) {
return comps.some(function(comp) { return comps.some(function(comp) {
var type = typeof comp; var type = typeof comp;
return type === 'string' ? return type === 'string'
(caseSensitive ? res === comp : res.toLowerCase() === comp.toLowerCase()) : ? (caseSensitive ? res === comp : res.toLowerCase() === comp.toLowerCase()) :
type === 'number' ? parseFloat(res) === comp : type === 'number' ? parseFloat(res) === comp :
type === 'function' ? comp(res) : type === 'function' ? comp(res) :
comp instanceof RegExp ? comp.test(res) : false; comp instanceof RegExp ? comp.test(res) : false;
@ -559,7 +571,7 @@ function isMatched(res, comps, caseSensitive) {
function replaceHomePath(path, expand) { function replaceHomePath(path, expand) {
var homePath = pathUtil.normalize( var homePath = pathUtil.normalize(
IS_WIN ? (process.env.HOMEDRIVE || '') + (process.env.HOMEPATH || '') : IS_WIN ? (process.env.HOMEDRIVE || '') + (process.env.HOMEPATH || '') :
process.env.HOME || '').replace(/[\/\\]+$/, ''); process.env.HOME || '').replace(/[/\\]+$/, '');
path = pathUtil.normalize(path); path = pathUtil.normalize(path);
return expand ? path.replace(/^~(?=\/|\\|$)/, homePath) : return expand ? path.replace(/^~(?=\/|\\|$)/, homePath) :
path.replace(new RegExp('^' + escapePattern(homePath) + path.replace(new RegExp('^' + escapePattern(homePath) +
@ -582,7 +594,11 @@ function replacePlaceholder(text, generator) {
} }
function array2charlist(array, caseSensitive, collectSymbols) { function array2charlist(array, caseSensitive, collectSymbols) {
var values, group = [], groupClass = -1, charCode = 0, symbols = '', suppressed; var group = [],
groupClass = -1,
charCode = 0,
symbols = '',
values, suppressed;
function addGroup(groups, group) { function addGroup(groups, group) {
if (group.length > 3) { // ellipsis if (group.length > 3) { // ellipsis
groups.push(group[0] + '...' + group[group.length - 1]); groups.push(group[0] + '...' + group[group.length - 1]);
@ -593,29 +609,29 @@ function array2charlist(array, caseSensitive, collectSymbols) {
return groups; return groups;
} }
values = array.reduce( values = array.reduce(function(chars, value) {
function(chars, value) { return chars.concat((value + '').split('')); }, []) return chars.concat((value + '').split(''));
.reduce(function(groups, curChar) { }, []).reduce(function(groups, curChar) {
var curGroupClass, curCharCode; var curGroupClass, curCharCode;
if (!caseSensitive) { curChar = curChar.toLowerCase(); } if (!caseSensitive) { curChar = curChar.toLowerCase(); }
curGroupClass = /^\d$/.test(curChar) ? 1 : curGroupClass = /^\d$/.test(curChar) ? 1 :
/^[A-Z]$/.test(curChar) ? 2 : /^[a-z]$/.test(curChar) ? 3 : 0; /^[A-Z]$/.test(curChar) ? 2 : /^[a-z]$/.test(curChar) ? 3 : 0;
if (collectSymbols && curGroupClass === 0) { if (collectSymbols && curGroupClass === 0) {
symbols += curChar; symbols += curChar;
} else {
curCharCode = curChar.charCodeAt(0);
if (curGroupClass && curGroupClass === groupClass &&
curCharCode === charCode + 1) {
group.push(curChar);
} else { } else {
curCharCode = curChar.charCodeAt(0); groups = addGroup(groups, group);
if (curGroupClass && curGroupClass === groupClass && group = [curChar];
curCharCode === charCode + 1) { groupClass = curGroupClass;
group.push(curChar);
} else {
groups = addGroup(groups, group);
group = [curChar];
groupClass = curGroupClass;
}
charCode = curCharCode;
} }
return groups; charCode = curCharCode;
}, []); }
return groups;
}, []);
values = addGroup(values, group); // last group values = addGroup(values, group); // last group
if (symbols) { values.push(symbols); suppressed = true; } if (symbols) { values.push(symbols); suppressed = true; }
return {values: values, suppressed: suppressed}; return {values: values, suppressed: suppressed};
@ -626,7 +642,8 @@ function joinChunks(chunks, suppressed) {
} }
function getPhContent(param, options) { function getPhContent(param, options) {
var text, values, resCharlist = {}, arg; var resCharlist = {},
text, values, arg;
if (options.phContent) { if (options.phContent) {
text = options.phContent(param, options); text = options.phContent(param, options);
} }
@ -667,8 +684,7 @@ function getPhContent(param, options) {
break; break;
case 'limitCount': case 'limitCount':
case 'limitCountNotZero': case 'limitCountNotZero':
text = options[options.hasOwnProperty('limitSrc') ? text = options[options.hasOwnProperty('limitSrc') ? 'limitSrc' : 'limit'].length;
'limitSrc' : 'limit'].length;
text = text || param !== 'limitCountNotZero' ? text + '' : ''; text = text || param !== 'limitCountNotZero' ? text + '' : '';
break; break;
case 'lastInput': case 'lastInput':
@ -702,7 +718,9 @@ function getPhContent(param, options) {
} }
function getPhCharlist(param) { function getPhCharlist(param) {
var matches = /^(.)-(.)$/.exec(param), text = '', from, to, code, step; var matches = /^(.)-(.)$/.exec(param),
text = '',
from, to, code, step;
if (!matches) { return null; } if (!matches) { return null; }
from = matches[1].charCodeAt(0); from = matches[1].charCodeAt(0);
to = matches[2].charCodeAt(0); to = matches[2].charCodeAt(0);
@ -713,8 +731,10 @@ function getPhCharlist(param) {
// cmd "arg" " a r g " "" 'a"r"g' "a""rg" "arg // cmd "arg" " a r g " "" 'a"r"g' "a""rg" "arg
function parseCl(cl) { function parseCl(cl) {
var reToken = new RegExp(/(\s*)(?:("|')(.*?)(?:\2|$)|(\S+))/g), matches, var reToken = new RegExp(/(\s*)(?:("|')(.*?)(?:\2|$)|(\S+))/g),
taken = '', args = [], part; taken = '',
args = [],
matches, part;
cl = cl.trim(); cl = cl.trim();
while ((matches = reToken.exec(cl))) { while ((matches = reToken.exec(cl))) {
part = matches[3] || matches[4] || ''; part = matches[3] || matches[4] || '';
@ -756,7 +776,7 @@ function getValidLine(options) {
if (options.defaultInput && !res) { res = options.defaultInput; } if (options.defaultInput && !res) { res = options.defaultInput; }
if (options.history) { if (options.history) {
if ((matches = /^\s*\!(?:\!|-1)(:p)?\s*$/.exec(res))) { // `!!` `!-1` +`:p` if ((matches = /^\s*!(?:!|-1)(:p)?\s*$/.exec(res))) { // `!!` `!-1` +`:p`
histInput = inputHistory[0] || ''; histInput = inputHistory[0] || '';
if (matches[1]) { // only display if (matches[1]) { // only display
forceNext = true; forceNext = true;
@ -856,8 +876,9 @@ exports.keyIn = function(query, options) {
readOptions.limitSrc = readOptions.limit.filter(function(value) { readOptions.limitSrc = readOptions.limit.filter(function(value) {
var type = typeof value; var type = typeof value;
return type === 'string' || type === 'number'; return type === 'string' || type === 'number';
}) }).map(function(text) {
.map(function(text) { return replacePlaceholder(text + '', getPhCharlist); }); return replacePlaceholder(text + '', getPhCharlist);
});
// pattern // pattern
readOptions.limit = escapePattern(readOptions.limitSrc.join('')); readOptions.limit = escapePattern(readOptions.limitSrc.join(''));
@ -886,7 +907,7 @@ exports.questionEMail = function(query, options) {
// -------- default // -------- default
hideEchoBack: false, hideEchoBack: false,
// http://www.w3.org/TR/html5/forms.html#valid-e-mail-address // 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])?)*$/, 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.', limitMessage: 'Input valid e-mail address, please.',
trueValue: null, trueValue: null,
falseValue: null falseValue: null
@ -985,7 +1006,8 @@ exports.questionFloat = function(query, options) {
exports.questionPath = function(query, options) { exports.questionPath = function(query, options) {
/* eslint-disable key-spacing */ /* eslint-disable key-spacing */
var validPath, error = '', var error = '',
validPath, // before readOptions
readOptions = margeOptions({ readOptions = margeOptions({
// -------- default // -------- default
hideEchoBack: false, hideEchoBack: false,
@ -1078,7 +1100,8 @@ exports.questionPath = function(query, options) {
// props: preCheck, args, hRes, limit // props: preCheck, args, hRes, limit
function getClHandler(commandHandler, options) { function getClHandler(commandHandler, options) {
var clHandler = {}, hIndex = {}; var clHandler = {},
hIndex = {};
if (typeof commandHandler === 'object') { if (typeof commandHandler === 'object') {
Object.keys(commandHandler).forEach(function(cmd) { Object.keys(commandHandler).forEach(function(cmd) {
if (typeof commandHandler[cmd] === 'function') { if (typeof commandHandler[cmd] === 'function') {
@ -1091,9 +1114,9 @@ function getClHandler(commandHandler, options) {
cmdKey = clHandler.args[0] || ''; cmdKey = clHandler.args[0] || '';
if (!options.caseSensitive) { cmdKey = cmdKey.toLowerCase(); } if (!options.caseSensitive) { cmdKey = cmdKey.toLowerCase(); }
clHandler.hRes = clHandler.hRes =
cmdKey !== '_' && hIndex.hasOwnProperty(cmdKey) ? cmdKey !== '_' && hIndex.hasOwnProperty(cmdKey)
hIndex[cmdKey].apply(res, clHandler.args.slice(1)) : ? hIndex[cmdKey].apply(res, clHandler.args.slice(1)) :
hIndex.hasOwnProperty('_') ? hIndex._.apply(res, clHandler.args) : null; hIndex.hasOwnProperty('_') ? hIndex._.apply(res, clHandler.args) : null;
return {res: res, forceNext: false}; return {res: res, forceNext: false};
}; };
if (!hIndex.hasOwnProperty('_')) { if (!hIndex.hasOwnProperty('_')) {
@ -1106,8 +1129,8 @@ function getClHandler(commandHandler, options) {
} else { } else {
clHandler.preCheck = function(res) { clHandler.preCheck = function(res) {
clHandler.args = parseCl(res); clHandler.args = parseCl(res);
clHandler.hRes = typeof commandHandler === 'function' ? clHandler.hRes = typeof commandHandler === 'function'
commandHandler.apply(res, clHandler.args) : true; // true for break loop ? commandHandler.apply(res, clHandler.args) : true; // true for break loop
return {res: res, forceNext: false}; return {res: res, forceNext: false};
}; };
} }
@ -1123,9 +1146,9 @@ exports.promptCL = function(commandHandler, options) {
caseSensitive: false, caseSensitive: false,
history: true history: true
}, options), }, options),
// -------- forced // -------- forced
// trueValue, falseValue, keepWhitespace don't work. // trueValue, falseValue, keepWhitespace don't work.
// preCheck, limit (by clHandler) // preCheck, limit (by clHandler)
clHandler = getClHandler(commandHandler, readOptions); clHandler = getClHandler(commandHandler, readOptions);
/* eslint-enable key-spacing */ /* eslint-enable key-spacing */
readOptions.limit = clHandler.limit; readOptions.limit = clHandler.limit;
@ -1146,7 +1169,7 @@ exports.promptLoop = function(inputHandler, options) {
}, options); }, options);
/* eslint-enable key-spacing */ /* eslint-enable key-spacing */
while (true) { if (inputHandler(exports.prompt(readOptions))) { break; } } while (true) { if (inputHandler(exports.prompt(readOptions))) { break; } }
return; // return; // nothing is returned
}; };
exports.promptCLLoop = function(commandHandler, options) { exports.promptCLLoop = function(commandHandler, options) {
@ -1158,9 +1181,9 @@ exports.promptCLLoop = function(commandHandler, options) {
caseSensitive: false, caseSensitive: false,
history: true history: true
}, options), }, options),
// -------- forced // -------- forced
// trueValue, falseValue, keepWhitespace don't work. // trueValue, falseValue, keepWhitespace don't work.
// preCheck, limit (by clHandler) // preCheck, limit (by clHandler)
clHandler = getClHandler(commandHandler, readOptions); clHandler = getClHandler(commandHandler, readOptions);
/* eslint-enable key-spacing */ /* eslint-enable key-spacing */
readOptions.limit = clHandler.limit; readOptions.limit = clHandler.limit;
@ -1169,7 +1192,7 @@ exports.promptCLLoop = function(commandHandler, options) {
exports.prompt(readOptions); exports.prompt(readOptions);
if (clHandler.hRes) { break; } if (clHandler.hRes) { break; }
} }
return; // return; // nothing is returned
}; };
exports.promptSimShell = function(options) { exports.promptSimShell = function(options) {
@ -1181,12 +1204,10 @@ exports.promptSimShell = function(options) {
}, options, { }, options, {
// -------- forced // -------- forced
prompt: (function() { prompt: (function() {
return IS_WIN ? return IS_WIN ? '$<cwd>>' :
'$<cwd>>' :
// 'user@host:cwd$ ' // 'user@host:cwd$ '
(process.env.USER || '') + (process.env.USER || '') +
(process.env.HOSTNAME ? (process.env.HOSTNAME ? '@' + process.env.HOSTNAME.replace(/\..*$/, '') : '') +
'@' + process.env.HOSTNAME.replace(/\..*$/, '') : '') +
':$<cwdHome>$ '; ':$<cwdHome>$ ';
})() })()
})); }));
@ -1232,7 +1253,7 @@ exports.keyInPause = function(query, options) {
})); }));
// added: guide // added: guide
/* eslint-enable key-spacing */ /* eslint-enable key-spacing */
return; // return; // nothing is returned
}; };
exports.keyInSelect = function(items, query, options) { exports.keyInSelect = function(items, query, options) {
@ -1253,7 +1274,10 @@ exports.keyInSelect = function(items, query, options) {
} }
}), }),
// added: guide, cancel // added: guide, cancel
keylist = '', key2i = {}, charCode = 49 /* '1' */, display = '\n'; keylist = '',
key2i = {},
charCode = 49 /* '1' */,
display = '\n';
/* eslint-enable key-spacing */ /* eslint-enable key-spacing */
if (!Array.isArray(items) || !items.length || items.length > 35) { if (!Array.isArray(items) || !items.length || items.length > 35) {
throw '`items` must be Array (max length: 35).'; throw '`items` must be Array (max length: 35).';
@ -1270,8 +1294,8 @@ exports.keyInSelect = function(items, query, options) {
keylist += '0'; keylist += '0';
key2i['0'] = -1; key2i['0'] = -1;
display += '[0] ' + display += '[0] ' +
(options && options.cancel != null && typeof options.cancel !== 'boolean' ? (options && options.cancel != null && typeof options.cancel !== 'boolean'
(options.cancel + '').trim() : 'CANCEL') + '\n'; ? (options.cancel + '').trim() : 'CANCEL') + '\n';
} }
readOptions.limit = keylist; readOptions.limit = keylist;
display += '\n'; display += '\n';