Add: parse home directory
This commit is contained in:
parent
80da6fd081
commit
57758fa427
2 changed files with 1133 additions and 147 deletions
|
@ -18,6 +18,7 @@ var
|
|||
fs = require('fs'),
|
||||
TTY = process.binding('tty_wrap').TTY,
|
||||
childProc = require('child_process'),
|
||||
pathUtil = require('path'),
|
||||
|
||||
defaultOptions = {
|
||||
prompt: '> ',
|
||||
|
@ -274,7 +275,7 @@ function readlineExt(options) {
|
|||
function _execFileSync(options, execOptions) {
|
||||
|
||||
function getTempfile(name) {
|
||||
var pathUtil = require('path'), filepath, suffix = '', fd;
|
||||
var filepath, suffix = '', fd;
|
||||
tempdir = tempdir || require('os').tmpdir();
|
||||
|
||||
while (true) {
|
||||
|
@ -522,6 +523,16 @@ function isMatched(res, comps, caseSensitive) {
|
|||
});
|
||||
}
|
||||
|
||||
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) :
|
||||
path.replace(new RegExp('^' + homePath.replace(/[^A-Za-z0-9_ ]/g, '\\$&') +
|
||||
'(?=\\/|\\\\|$)', IS_WIN ? 'i' : ''), '~');
|
||||
}
|
||||
|
||||
function replacePlaceholder(text, generator) {
|
||||
return text.replace(/(\$)?(\$\{(?:\(([\s\S]*?)\))?(\w+|.-.)(?:\(([\s\S]*?)\))?\})/g,
|
||||
function(str, escape, placeholder, pre, param, post) {
|
||||
|
@ -575,7 +586,7 @@ function joinChunks(chunks, suppressed)
|
|||
{ return chunks.join(chunks.length > 2 ? ', ' : suppressed ? ' / ' : '/'); }
|
||||
|
||||
function getPhContent(param, options) {
|
||||
var text, values, resCharlist = {}, path, arg;
|
||||
var text, values, resCharlist = {}, arg;
|
||||
if (options.phContent) {
|
||||
text = options.phContent(param, options);
|
||||
}
|
||||
|
@ -627,14 +638,8 @@ function getPhContent(param, options) {
|
|||
case 'CWD':
|
||||
case 'cwdHome':
|
||||
text = process.cwd();
|
||||
if (param === 'CWD') { text = require('path').basename(text); }
|
||||
else if (param === 'cwdHome') {
|
||||
path = (IS_WIN ? ((process.env.HOMEDRIVE || '') +
|
||||
(process.env.HOMEPATH || '')).toLowerCase() :
|
||||
process.env.HOME || '').replace(/[\/\\]+$/, '');
|
||||
if (path && (IS_WIN ? text.toLowerCase() : text).indexOf(path) === 0)
|
||||
{ text = '~' + text.substr(path.length); }
|
||||
}
|
||||
if (param === 'CWD') { text = pathUtil.basename(text); }
|
||||
else if (param === 'cwdHome') { text = replaceHomePath(text); }
|
||||
break;
|
||||
case 'date':
|
||||
case 'time':
|
||||
|
@ -731,9 +736,7 @@ function getValidLine(options) {
|
|||
case 'cd':
|
||||
if (args[1]) {
|
||||
try {
|
||||
process.chdir(args[1].trim() === '~' ? (
|
||||
IS_WIN ? (process.env.HOMEDRIVE || '') + (process.env.HOMEPATH || '') :
|
||||
process.env.HOME || '') : args[1]);
|
||||
process.chdir(replaceHomePath(args[1], true));
|
||||
} catch (e) {
|
||||
addDisplay(e + '');
|
||||
}
|
||||
|
@ -772,13 +775,13 @@ exports._DBG_set_checkOptions = function(val) { _DBG_checkOptions = val; };
|
|||
exports._DBG_set_checkMethod = function(val) { _DBG_checkMethod = val; };
|
||||
exports._DBG_clearHistory = function() { lastInput = ''; inputHistory = []; };
|
||||
|
||||
exports.setDefault = function(options) {
|
||||
// ------------------------------------
|
||||
|
||||
exports.setDefaultOptions = function(options) {
|
||||
defaultOptions = margeOptions(true, options);
|
||||
return margeOptions(true); // copy
|
||||
};
|
||||
|
||||
// ------------------------------------
|
||||
|
||||
exports.prompt = function(options) {
|
||||
var readOptions = margeOptions(true, options);
|
||||
readOptions.display = readOptions.prompt;
|
||||
|
@ -936,14 +939,15 @@ exports.questionPath = function(query, options) {
|
|||
// -------- default
|
||||
hideEchoBack: false,
|
||||
limitMessage: '${error(\n)}Input valid path, please.' +
|
||||
'${( Min:)minSize}${( Max:)maxSize}',
|
||||
'${( Min:)min}${( Max:)max}',
|
||||
history: true,
|
||||
cd: true
|
||||
}, options, {
|
||||
// -------- forced
|
||||
keepWhitespace: false,
|
||||
limit: function(value) {
|
||||
var exists, stat, res, pathUtil = require('path');
|
||||
var exists, stat, res;
|
||||
value = replaceHomePath(value);
|
||||
// mkdir -p
|
||||
function mkdirParents(dirPath) {
|
||||
dirPath.split(/\/|\\/).reduce(function(parents, dir) {
|
||||
|
@ -976,7 +980,7 @@ exports.questionPath = function(query, options) {
|
|||
}
|
||||
validPath = fs.realpathSync(validPath);
|
||||
}
|
||||
if (exists && (options.minSize || options.maxSize ||
|
||||
if (exists && (options.min || options.max ||
|
||||
options.isFile || options.isDirectory)) {
|
||||
stat = fs.statSync(validPath);
|
||||
// type check first (directory has zero size)
|
||||
|
@ -986,8 +990,8 @@ exports.questionPath = function(query, options) {
|
|||
} else if (options.isDirectory && !stat.isDirectory()) {
|
||||
error = 'Not directory: ' + validPath;
|
||||
return;
|
||||
} else if (options.minSize && stat.size < +options.minSize ||
|
||||
options.maxSize && stat.size > +options.maxSize) {
|
||||
} else if (options.min && stat.size < +options.min ||
|
||||
options.max && stat.size > +options.max) {
|
||||
error = 'Size ' + stat.size +' is out of range: ' + validPath;
|
||||
return;
|
||||
}
|
||||
|
@ -1006,11 +1010,11 @@ exports.questionPath = function(query, options) {
|
|||
// trueValue, falseValue are don't work.
|
||||
phContent: function(param) {
|
||||
return param === 'error' ? error :
|
||||
param !== 'minSize' && param !== 'maxSize' ? null :
|
||||
param !== 'min' && param !== 'max' ? null :
|
||||
options.hasOwnProperty(param) ? options[param] + '' : '';
|
||||
}
|
||||
}),
|
||||
// added: exists, create, minSize, maxSize, isFile, isDirectory, validate
|
||||
// added: exists, create, min, max, isFile, isDirectory, validate
|
||||
validPath, error = '';
|
||||
options = options || {};
|
||||
|
||||
|
@ -1053,7 +1057,7 @@ function getClHandler(commandHandler, options) {
|
|||
clHandler.preCheck = function(res) {
|
||||
clHandler.args = parseCl(res);
|
||||
clHandler.hRes = typeof commandHandler === 'function' ?
|
||||
commandHandler.apply(res, clHandler.args) : null;
|
||||
commandHandler.apply(res, clHandler.args) : true; // true for break loop
|
||||
return {res: res, forceNext: false};
|
||||
};
|
||||
}
|
||||
|
@ -1069,7 +1073,7 @@ exports.promptCL = function(commandHandler, options) {
|
|||
history: true
|
||||
}, options),
|
||||
// -------- forced
|
||||
// trueValue, falseValue are don't work.
|
||||
// trueValue, falseValue, keepWhitespace are don't work.
|
||||
// preCheck, limit (by clHandler)
|
||||
clHandler = getClHandler(commandHandler, readOptions);
|
||||
readOptions.limit = clHandler.limit;
|
||||
|
@ -1100,7 +1104,7 @@ exports.promptCLLoop = function(commandHandler, options) {
|
|||
history: true
|
||||
}, options),
|
||||
// -------- forced
|
||||
// trueValue, falseValue are don't work.
|
||||
// trueValue, falseValue, keepWhitespace are don't work.
|
||||
// preCheck, limit (by clHandler)
|
||||
clHandler = getClHandler(commandHandler, readOptions);
|
||||
readOptions.limit = clHandler.limit;
|
||||
|
@ -1218,7 +1222,7 @@ exports.keyInSelect = function(items, query, options) {
|
|||
function _setOption(optionName, args) {
|
||||
var options;
|
||||
if (args.length) { options = {}; options[optionName] = args[0]; }
|
||||
return exports.setDefault(options)[optionName];
|
||||
return exports.setDefaultOptions(options)[optionName];
|
||||
}
|
||||
exports.setPrint = function() { return _setOption('print', arguments); };
|
||||
exports.setPrompt = function() { return _setOption('prompt', arguments); };
|
||||
|
|
Loading…
Reference in a new issue