jsir 2.1.6 → 2.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cmd/oaa.js +111 -36
- package/deps/util.js +11 -4
- package/package.json +1 -1
package/cmd/oaa.js
CHANGED
|
@@ -6,7 +6,8 @@ const {
|
|
|
6
6
|
arrayDataFile, infoStr, warnStr, errorStack,
|
|
7
7
|
getInfo, ei, pad, msgStr, getType,
|
|
8
8
|
errorTag, isArgsMatch, draftQuery, setConfig,
|
|
9
|
-
$log, $draft, getTextComments, getOr, importG, requireG
|
|
9
|
+
$log, $draft, getTextComments, getOr, importG, requireG,
|
|
10
|
+
clearConsole
|
|
10
11
|
} = $lib;
|
|
11
12
|
const _args = process.argv.slice(2).map(trim);
|
|
12
13
|
const evalCode = require('../deps/evalCode')
|
|
@@ -381,7 +382,9 @@ function _nextLine(callback, promptStr, hidden, resolve, end, isText) {
|
|
|
381
382
|
_haveWrapperInput = true;
|
|
382
383
|
closeRl()
|
|
383
384
|
inputStr = inputStr.replace(/\s+$/, '');
|
|
384
|
-
|
|
385
|
+
if (!isText && /^\d+$/.test(inputStr) && /^\s+/.test(textLine)) {
|
|
386
|
+
inputStr = "& " + inputStr;
|
|
387
|
+
}
|
|
385
388
|
let pro = (callback || wrapperInput)(inputStr);
|
|
386
389
|
resolve && resolve(inputStr);
|
|
387
390
|
try {
|
|
@@ -390,7 +393,11 @@ function _nextLine(callback, promptStr, hidden, resolve, end, isText) {
|
|
|
390
393
|
if ((callback || wrapperInput) === wrapperInput) {
|
|
391
394
|
if (!_noAppendNextLine) {
|
|
392
395
|
if (/^\s+$/.test(textLine)) {
|
|
393
|
-
|
|
396
|
+
if (textLine.length > 1) {
|
|
397
|
+
clearConsole(false)
|
|
398
|
+
} else {
|
|
399
|
+
clearConsole();
|
|
400
|
+
}
|
|
394
401
|
listCmd();
|
|
395
402
|
nextLine(null, defaultPromptStr(true))
|
|
396
403
|
} else {
|
|
@@ -505,6 +512,9 @@ function listCmd(tmpMap) {
|
|
|
505
512
|
warn("no items")
|
|
506
513
|
} else {
|
|
507
514
|
items.forEach(item => {
|
|
515
|
+
if (tmpList && item.key) {
|
|
516
|
+
item.key = ' - '
|
|
517
|
+
}
|
|
508
518
|
if (item.type) {
|
|
509
519
|
let pair = parseUniqueName(item.name);
|
|
510
520
|
if (item.type.startsWith('exe.')) {
|
|
@@ -539,15 +549,14 @@ async function _wrapperInput(str) {
|
|
|
539
549
|
}
|
|
540
550
|
if (/^&/.test(str)) {
|
|
541
551
|
let cmdStr = trim(str.substr(1));
|
|
542
|
-
if (/^\d+$/.test(cmdStr)
|
|
552
|
+
if (/^\d+$/.test(cmdStr)) {
|
|
543
553
|
if (_cmdMap[cmdStr]) {
|
|
544
554
|
ei('vi', [getFullPath(_cmdMap[cmdStr])])
|
|
545
555
|
} else {
|
|
546
|
-
|
|
556
|
+
ei('vi', ['-n'])
|
|
547
557
|
}
|
|
548
558
|
} else if (cmdStr) {
|
|
549
|
-
ei(cmdStr.split(/\s+/)[0],
|
|
550
|
-
enrichArgs(trim(cmdStr.replace(/^\S+/, ''))).map(trim));
|
|
559
|
+
ei(cmdStr.split(/\s+/)[0], enrichArgs(trim(cmdStr.replace(/^\S+/, ''))).map(trim));
|
|
551
560
|
} else {
|
|
552
561
|
ei('vi', ['-n'])
|
|
553
562
|
}
|
|
@@ -1038,21 +1047,31 @@ const keywordDef = {
|
|
|
1038
1047
|
warn('invalid args')
|
|
1039
1048
|
return
|
|
1040
1049
|
}
|
|
1041
|
-
let
|
|
1050
|
+
let keyword = args[0]
|
|
1051
|
+
let justList = false;
|
|
1052
|
+
if (keyword.endsWith('.')) {
|
|
1053
|
+
let _tmp = trim(keyword.substring(0, keyword.length - 1));
|
|
1054
|
+
if (_tmp) {
|
|
1055
|
+
justList = true;
|
|
1056
|
+
keyword = _tmp;
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
let pair = parseUniqueName(keyword)
|
|
1042
1060
|
let cmds;
|
|
1043
1061
|
if (/^0x[a-z\d]{8}$/.test(pair[1]) || pair[1].indexOf(",") !== -1) {
|
|
1044
|
-
cmds = getQuickRunCmds(
|
|
1062
|
+
cmds = getQuickRunCmds(keyword, keyword)
|
|
1045
1063
|
} else {
|
|
1046
|
-
cmds = getQuickRunCmds(
|
|
1064
|
+
cmds = getQuickRunCmds(keyword, '^e ' + toJsirFileName(pair[1]) + '$')
|
|
1047
1065
|
if (cmds.length === 0) {
|
|
1048
|
-
cmds = getQuickRunCmds(
|
|
1066
|
+
cmds = getQuickRunCmds(keyword, keyword)
|
|
1049
1067
|
}
|
|
1050
1068
|
}
|
|
1051
1069
|
if (cmds.length === 1) {
|
|
1052
|
-
if (
|
|
1070
|
+
if (justList) {
|
|
1053
1071
|
listCmd(arrayToCmdMap(cmds))
|
|
1072
|
+
} else {
|
|
1073
|
+
await runScript(cmds[0], args.slice(1))
|
|
1054
1074
|
}
|
|
1055
|
-
await runCmd(args.slice(1).join(' '), cmds[0])
|
|
1056
1075
|
} else {
|
|
1057
1076
|
warn(cmds.length > 1 ? "multiple match" : "no match")
|
|
1058
1077
|
}
|
|
@@ -1119,7 +1138,7 @@ async function dealKeyword(items) {
|
|
|
1119
1138
|
continue
|
|
1120
1139
|
}
|
|
1121
1140
|
if (keyword) {
|
|
1122
|
-
args.push(
|
|
1141
|
+
args.push(item)
|
|
1123
1142
|
}
|
|
1124
1143
|
}
|
|
1125
1144
|
if (keyword) {
|
|
@@ -1138,7 +1157,11 @@ async function _dealKeyword(keyword, args) {
|
|
|
1138
1157
|
let shortKey = keyword === key ? item.short : keyword;
|
|
1139
1158
|
if (item.short === shortKey) {
|
|
1140
1159
|
unMatched = false;
|
|
1141
|
-
|
|
1160
|
+
if (item === keywordDef.run) {
|
|
1161
|
+
await item.exeFn(args);
|
|
1162
|
+
} else {
|
|
1163
|
+
await item.exeFn(args.map(trim));
|
|
1164
|
+
}
|
|
1142
1165
|
break;
|
|
1143
1166
|
}
|
|
1144
1167
|
}
|
|
@@ -1223,7 +1246,7 @@ function getComments(i, cmdName, text, cols = [], col) {
|
|
|
1223
1246
|
text = trim(text)
|
|
1224
1247
|
docLines.push(...getTextComments(text))
|
|
1225
1248
|
let argDef = getArgDef(text)
|
|
1226
|
-
let comments = getArgComments(argDef)
|
|
1249
|
+
let comments = getArgComments(argDef, true)
|
|
1227
1250
|
if (comments) {
|
|
1228
1251
|
docLines.push(...comments)
|
|
1229
1252
|
}
|
|
@@ -1364,21 +1387,20 @@ function parseUniqueName(uniqueName) {
|
|
|
1364
1387
|
return [pair[len - 2], pair[len - 1]]
|
|
1365
1388
|
}
|
|
1366
1389
|
|
|
1367
|
-
function getArgComments(argDef) {
|
|
1390
|
+
function getArgComments(argDef, showShort = false) {
|
|
1368
1391
|
let comments = []
|
|
1369
1392
|
let keys = Object.keys(argDef)
|
|
1393
|
+
let shortMap = {}
|
|
1394
|
+
if (showShort) {
|
|
1395
|
+
let tempShortMap = getShortDefMap(keys)
|
|
1396
|
+
for (let key of Object.keys(tempShortMap)) {
|
|
1397
|
+
shortMap[tempShortMap[key].replace(/^:/, '')] = key;
|
|
1398
|
+
}
|
|
1399
|
+
}
|
|
1370
1400
|
if (keys.length > 0) {
|
|
1371
|
-
let aLine = []
|
|
1372
1401
|
for (let k of keys) {
|
|
1373
|
-
let item = warnStr(k) + (argDef[k] ? ` <${argDef[k]}>`:'')
|
|
1374
|
-
|
|
1375
|
-
comments.push(aLine.join(", ") + ", ")
|
|
1376
|
-
aLine = []
|
|
1377
|
-
}
|
|
1378
|
-
aLine.push(item)
|
|
1379
|
-
}
|
|
1380
|
-
if (aLine.length > 0) {
|
|
1381
|
-
comments.push(aLine.join(", "))
|
|
1402
|
+
let item = warnStr(k) + (shortMap[k] ? ` ${warnStr(shortMap[k])}`:'') + (argDef[k] ? ` <${argDef[k]}>`:'')
|
|
1403
|
+
comments.push(item)
|
|
1382
1404
|
}
|
|
1383
1405
|
comments[0] = '$args: ' + comments[0]
|
|
1384
1406
|
if (comments.length > 1) {
|
|
@@ -1390,6 +1412,42 @@ function getArgComments(argDef) {
|
|
|
1390
1412
|
return comments
|
|
1391
1413
|
}
|
|
1392
1414
|
|
|
1415
|
+
function getShortStr(str) {
|
|
1416
|
+
let result = []
|
|
1417
|
+
for (let i = 0; i < str.length; i++) {
|
|
1418
|
+
if (i === 0) {
|
|
1419
|
+
result.push(str[i].toLowerCase())
|
|
1420
|
+
}
|
|
1421
|
+
if (/[A-Z]/.test(str[i])) {
|
|
1422
|
+
result.push(str[i].toLowerCase())
|
|
1423
|
+
}
|
|
1424
|
+
}
|
|
1425
|
+
return result.join('');
|
|
1426
|
+
}
|
|
1427
|
+
|
|
1428
|
+
function getShortDefMap(argDefKeys) {
|
|
1429
|
+
const result = {};
|
|
1430
|
+
for (let str of argDefKeys) {
|
|
1431
|
+
let originStr = ":" + str;
|
|
1432
|
+
str = str.replace(/^:/, '')
|
|
1433
|
+
if (!str.startsWith("_")) {
|
|
1434
|
+
continue;
|
|
1435
|
+
}
|
|
1436
|
+
str = str.replace(/^_/, '')
|
|
1437
|
+
let shortStr = getShortStr(str);
|
|
1438
|
+
let key = '-' + shortStr;
|
|
1439
|
+
let val = result[key]
|
|
1440
|
+
let i = 1;
|
|
1441
|
+
while (val) {
|
|
1442
|
+
key = '-' + shortStr + i;
|
|
1443
|
+
val = result[key];
|
|
1444
|
+
i ++;
|
|
1445
|
+
}
|
|
1446
|
+
result[key] = originStr;
|
|
1447
|
+
}
|
|
1448
|
+
return result;
|
|
1449
|
+
}
|
|
1450
|
+
|
|
1393
1451
|
async function runCmd(str = '', uniqueName = '', text = '') {
|
|
1394
1452
|
let path = getFullPath(uniqueName);
|
|
1395
1453
|
if (uniqueName || text) {
|
|
@@ -1418,11 +1476,28 @@ async function runCmd(str = '', uniqueName = '', text = '') {
|
|
|
1418
1476
|
return;
|
|
1419
1477
|
}
|
|
1420
1478
|
|
|
1421
|
-
let oriArgs = enrichArgs(str, text)
|
|
1422
1479
|
let argDef = getArgDef(text)
|
|
1423
1480
|
if (argDef['[ParseError]']) {
|
|
1424
1481
|
throw `argDef [ParseError] ${argDef['[ParseError]']}`
|
|
1425
1482
|
}
|
|
1483
|
+
|
|
1484
|
+
let scriptArgs = await getScriptArgs(argDef, enrichArgs(str.replace(/^\d+\s*/, '')));
|
|
1485
|
+
process.argv = [process.argv[0], path, ...(scriptArgs.map(String))]
|
|
1486
|
+
return await evalText(text, uniqueName, scriptArgs)
|
|
1487
|
+
}
|
|
1488
|
+
|
|
1489
|
+
async function runScript(uniqueName, oriArgs) {
|
|
1490
|
+
let path = getFullPath(uniqueName);
|
|
1491
|
+
let text = String(_fs.readFileSync(path))
|
|
1492
|
+
let argDef = getArgDef(text)
|
|
1493
|
+
let scriptArgs = await getScriptArgs(argDef, oriArgs);
|
|
1494
|
+
process.argv = [process.argv[0], path, ...(scriptArgs.map(String))]
|
|
1495
|
+
return await evalText(text, uniqueName, scriptArgs)
|
|
1496
|
+
}
|
|
1497
|
+
|
|
1498
|
+
async function getScriptArgs(argDef, oriArgs) {
|
|
1499
|
+
let shortDefMap = getShortDefMap(Object.keys(argDef))
|
|
1500
|
+
oriArgs = oriArgs.map(i => shortDefMap[i] ? shortDefMap[i]:i)
|
|
1426
1501
|
let argNames = Object.keys(argDef)
|
|
1427
1502
|
let exactArgs = {}
|
|
1428
1503
|
let scriptArgs = []
|
|
@@ -1430,9 +1505,12 @@ async function runCmd(str = '', uniqueName = '', text = '') {
|
|
|
1430
1505
|
let arg = oriArgs[i]
|
|
1431
1506
|
let needTrans
|
|
1432
1507
|
let pair
|
|
1433
|
-
if (arg.startsWith("
|
|
1508
|
+
if (arg.startsWith("-") && !arg.endsWith(' ')) {
|
|
1509
|
+
throw 'invalid argName ' + arg
|
|
1510
|
+
}
|
|
1511
|
+
if (arg.startsWith(":") || arg.startsWith("_") && !arg.endsWith(' ')) {
|
|
1434
1512
|
arg = arg.replace(/^:/, '')
|
|
1435
|
-
if (oriArgs[i+1] && !(oriArgs[i+1].startsWith(":") || oriArgs[i+1].startsWith("_"))) {
|
|
1513
|
+
if (oriArgs[i+1] && (oriArgs[i+1].endsWith(' ') || !(oriArgs[i+1].startsWith(":") || oriArgs[i+1].startsWith("_")))) {
|
|
1436
1514
|
pair = [arg, oriArgs[i+1]]
|
|
1437
1515
|
i++
|
|
1438
1516
|
} else {
|
|
@@ -1488,16 +1566,13 @@ async function runCmd(str = '', uniqueName = '', text = '') {
|
|
|
1488
1566
|
if (argAbsent) {
|
|
1489
1567
|
throw 'invalid args';
|
|
1490
1568
|
}
|
|
1491
|
-
|
|
1492
|
-
process.argv = [process.argv[0], path, ...(scriptArgs.map(String))]
|
|
1493
|
-
return await evalText(text, uniqueName, scriptArgs)
|
|
1569
|
+
return scriptArgs;
|
|
1494
1570
|
}
|
|
1495
1571
|
|
|
1496
1572
|
// 使用空格作为可执行的标志
|
|
1497
1573
|
function enrichArgs(str) {
|
|
1498
1574
|
let args = []
|
|
1499
1575
|
let temp = []
|
|
1500
|
-
str = str.replace(/^\d+\s*/, '')
|
|
1501
1576
|
regEach(str, /'([^']*)'|"([^"]*)"|`([^`]*)`/g, item => {
|
|
1502
1577
|
if (item[1] !== null && item[1] !== undefined) {
|
|
1503
1578
|
temp.push(trim(item[1]) + ' ')
|
|
@@ -1532,8 +1607,8 @@ function getArgDef(text) {
|
|
|
1532
1607
|
argDef = (exeStr ? evalVal(exeStr) : {}) || {}
|
|
1533
1608
|
let array = []
|
|
1534
1609
|
for (let key of Object.keys(argDef)) {
|
|
1535
|
-
if (array.hasOwnProperty(key)) {
|
|
1536
|
-
throw `invalid argName ${key}`
|
|
1610
|
+
if (/\s/.test(key) || array.hasOwnProperty(key)) {
|
|
1611
|
+
throw `invalid argName ${warnStr(key)}`
|
|
1537
1612
|
}
|
|
1538
1613
|
}
|
|
1539
1614
|
} catch (e) {
|
package/deps/util.js
CHANGED
|
@@ -13,7 +13,7 @@ const globalDirectories = require('global-dirs');
|
|
|
13
13
|
const emptyFn = ()=>{}
|
|
14
14
|
const dayJs = require('dayjs')
|
|
15
15
|
const table = require('console.table')
|
|
16
|
-
const {log: _log} = console;
|
|
16
|
+
const {log: _log, clear: _clear} = console;
|
|
17
17
|
const _fs = require("fs");
|
|
18
18
|
if (module.paths.indexOf(globalDirectories.npm.packages) === -1) {
|
|
19
19
|
module.paths.push(globalDirectories.npm.packages)
|
|
@@ -213,9 +213,16 @@ async function timeLimit(proms, mills) {
|
|
|
213
213
|
return result
|
|
214
214
|
}
|
|
215
215
|
|
|
216
|
-
function clearConsole() {
|
|
217
|
-
|
|
218
|
-
|
|
216
|
+
function clearConsole(clean = true) {
|
|
217
|
+
if (clean) {
|
|
218
|
+
// \033[0f 清除从光标位置到屏幕末尾的内容,并把光标移动到屏幕的左上角。
|
|
219
|
+
// process.stdout.write('\033[0f');
|
|
220
|
+
_clear();
|
|
221
|
+
} else {
|
|
222
|
+
// \033[2J:清空屏幕。
|
|
223
|
+
// \033[H:将光标定位到屏幕左上角。
|
|
224
|
+
process.stdout.write('\033[2J\033[H');
|
|
225
|
+
}
|
|
219
226
|
}
|
|
220
227
|
|
|
221
228
|
function appendLog(fileName, text) {
|