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