jsir 2.0.7 → 2.0.9
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/cleanEval.js +3 -0
- package/cmd/oaa.js +233 -163
- package/package.json +1 -1
- package/util.js +15 -12
package/cleanEval.js
ADDED
package/cmd/oaa.js
CHANGED
|
@@ -1,16 +1,4 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
const path = require('path');
|
|
3
|
-
const {spawnSync} = require('child_process');
|
|
4
|
-
const globalDirectories = require('global-dirs');
|
|
5
|
-
let nodePath = process.env.NODE_PATH ? process.env.NODE_PATH.trim():'';
|
|
6
|
-
let avoidFlag = path.delimiter + path.delimiter + path.delimiter;
|
|
7
|
-
if (!nodePath.endsWith(avoidFlag) && nodePath.indexOf(globalDirectories.npm.packages) === -1) {
|
|
8
|
-
process.env.NODE_PATH = [globalDirectories.npm.packages, nodePath].filter(i => i).join(path.delimiter) + avoidFlag
|
|
9
|
-
process.on('SIGINT', function () {});
|
|
10
|
-
spawnSync(process.argv[0], process.argv.slice(1), {stdio:"inherit"});
|
|
11
|
-
return;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
2
|
const $lib = require('../util');
|
|
15
3
|
const {
|
|
16
4
|
getLibDataDir, trim, regEach, getConfig, mkdir, reget,
|
|
@@ -18,14 +6,16 @@ const {
|
|
|
18
6
|
info, warn, error, arrayDataFile, infoStr, warnStr, errorStack,
|
|
19
7
|
getInfo, ei, pad, msgStr, getType,
|
|
20
8
|
errorTag, isArgsMatch, draftQuery, setConfig,
|
|
21
|
-
$log, $draft, getTextComments
|
|
9
|
+
$log, $draft, getTextComments, getOr, importG
|
|
22
10
|
} = $lib;
|
|
23
11
|
const _args = process.argv.slice(2).map(trim);
|
|
24
12
|
const evalCode = require('../evalCode')
|
|
13
|
+
const {evalVal} = require('../cleanEval')
|
|
25
14
|
const _chokidar = require('chokidar');
|
|
26
15
|
const setting = require('../setting')
|
|
27
16
|
const _fs = require('fs')
|
|
28
17
|
const readline = require("readline");
|
|
18
|
+
const {requireG} = require("../util");
|
|
29
19
|
|
|
30
20
|
const _workspaceConfigFile = 'workspace.json';
|
|
31
21
|
const _libDataDir = getLibDataDir()
|
|
@@ -47,22 +37,67 @@ let _rl
|
|
|
47
37
|
let _rlHistory = []
|
|
48
38
|
let _haveWrapperInput = true
|
|
49
39
|
let _exit = false
|
|
40
|
+
let _noAppendNextLine = true
|
|
50
41
|
|
|
42
|
+
const _onGetMap = {}
|
|
51
43
|
const _data = {}
|
|
44
|
+
const _tempTime = 60 * 1000
|
|
45
|
+
const _dealOnGet = (key) => {
|
|
46
|
+
if (_noAppendNextLine || !_onGetMap[key]) {
|
|
47
|
+
return _data[key]
|
|
48
|
+
}
|
|
49
|
+
let item = _onGetMap[key]
|
|
50
|
+
if (Date.now() <= item.startTime) {
|
|
51
|
+
item.startTime = Date.now() + _tempTime
|
|
52
|
+
return _data[key]
|
|
53
|
+
}
|
|
54
|
+
let flag = _onGetMap[key].fn()
|
|
55
|
+
if (getType(flag)=== 'Promise') {
|
|
56
|
+
return new Promise(async (resolve, reject) => {
|
|
57
|
+
try {
|
|
58
|
+
await flag
|
|
59
|
+
item.startTime = Date.now() + _tempTime
|
|
60
|
+
resolve(_data[key])
|
|
61
|
+
} catch (e) {
|
|
62
|
+
reject(e)
|
|
63
|
+
}
|
|
64
|
+
})
|
|
65
|
+
} else {
|
|
66
|
+
item.startTime = Date.now() + _tempTime
|
|
67
|
+
return _data[key]
|
|
68
|
+
}
|
|
69
|
+
}
|
|
52
70
|
const $data = {
|
|
53
71
|
get: (key, defaultVal) => {
|
|
54
|
-
if (!vl(_data[key])
|
|
55
|
-
|
|
72
|
+
if (!vl(_data[key])) {
|
|
73
|
+
if (defaultVal !== undefined) {
|
|
74
|
+
return defaultVal
|
|
75
|
+
} else {
|
|
76
|
+
return _data[key]
|
|
77
|
+
}
|
|
56
78
|
}
|
|
57
|
-
return
|
|
79
|
+
return _dealOnGet(key)
|
|
58
80
|
},
|
|
59
81
|
gos: (key, defaultVal) => {
|
|
60
|
-
if (!vl(_data[key])
|
|
61
|
-
|
|
82
|
+
if (!vl(_data[key])) {
|
|
83
|
+
if (defaultVal !== undefined) {
|
|
84
|
+
_data[key] = defaultVal
|
|
85
|
+
return defaultVal
|
|
86
|
+
} else {
|
|
87
|
+
return _data[key]
|
|
88
|
+
}
|
|
62
89
|
}
|
|
63
|
-
return
|
|
90
|
+
return _dealOnGet(key)
|
|
91
|
+
},
|
|
92
|
+
set: (key, val, onGet) => {
|
|
93
|
+
if (onGet) {
|
|
94
|
+
_onGetMap[key] = {
|
|
95
|
+
fn: onGet,
|
|
96
|
+
startTime: Date.now() + _tempTime
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return _data[key] = val
|
|
64
100
|
},
|
|
65
|
-
set: (key, val) => _data[key] = val,
|
|
66
101
|
del: (key) => {
|
|
67
102
|
let val = _data[key]
|
|
68
103
|
delete _data[key]
|
|
@@ -77,8 +112,6 @@ global.$newInput = false
|
|
|
77
112
|
global.$workspaceMap = {}
|
|
78
113
|
global.$defaultSpace = 'local'
|
|
79
114
|
|
|
80
|
-
let _noAppendNextLine = true
|
|
81
|
-
|
|
82
115
|
function getFileOpenExe(fileName) {
|
|
83
116
|
fileName = trim(fileName);
|
|
84
117
|
let strs = fileName.split('.')
|
|
@@ -265,7 +298,7 @@ function initRl(callback, preStr, hidden) {
|
|
|
265
298
|
if (promptStr !== '') {
|
|
266
299
|
promptStr = (preStr
|
|
267
300
|
|| ((callback && callback !== wrapperInput) ? "-> ":"")
|
|
268
|
-
|| [Object.values(global.$tips).map(
|
|
301
|
+
|| [Object.values(global.$tips).map(trim).filter(i => i).map(i => i.replace(/,/g, '-')).join(','), $defaultSpace].filter(i => i).join(':') + `> `)
|
|
269
302
|
promptStr = infoStr(promptStr)
|
|
270
303
|
}
|
|
271
304
|
if (hidden) {
|
|
@@ -309,7 +342,7 @@ function _nextLine(callback, preStr, hidden, resolve, end) {
|
|
|
309
342
|
} else {
|
|
310
343
|
inputStr = line
|
|
311
344
|
if (/^\s+$/.test(textLine) && (!callback || wrapperInput === callback)) {
|
|
312
|
-
|
|
345
|
+
process.stdout.write('\033[2J\033[H');
|
|
313
346
|
listCmd();
|
|
314
347
|
}
|
|
315
348
|
}
|
|
@@ -543,12 +576,7 @@ async function _wrapperInput(str) {
|
|
|
543
576
|
await evalText(text)
|
|
544
577
|
}
|
|
545
578
|
} else if (str.match(/^\./)) {
|
|
546
|
-
|
|
547
|
-
if (items.length > 0) {
|
|
548
|
-
await dealKeyword(items[0], items.slice(1))
|
|
549
|
-
} else {
|
|
550
|
-
help()
|
|
551
|
-
}
|
|
579
|
+
await dealKeyword(enrichArgs(str))
|
|
552
580
|
} else if (!str.split(/\s+/)[0].match(/^\d+$/)) {
|
|
553
581
|
_cmdMap = arrayToCmdMap(filterCmd(str))
|
|
554
582
|
listCmd()
|
|
@@ -694,6 +722,13 @@ const keywordDef = {
|
|
|
694
722
|
},
|
|
695
723
|
short: 'h'
|
|
696
724
|
},
|
|
725
|
+
version: {
|
|
726
|
+
comment: '查看版本',
|
|
727
|
+
exeFn: (args) => {
|
|
728
|
+
console.log(require("../package.json").version)
|
|
729
|
+
},
|
|
730
|
+
short: 'v'
|
|
731
|
+
},
|
|
697
732
|
list: {
|
|
698
733
|
comment: '查看文件列表',
|
|
699
734
|
exeFn: (args) => {
|
|
@@ -849,23 +884,42 @@ const keywordDef = {
|
|
|
849
884
|
},
|
|
850
885
|
short: 'c'
|
|
851
886
|
},
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
887
|
+
deps: {
|
|
888
|
+
comment: '查看文件依赖',
|
|
889
|
+
exeFn: (args) => {
|
|
890
|
+
let uniqueName = _cmdMap[args[0]]
|
|
891
|
+
if (!uniqueName) {
|
|
892
|
+
warn("no items")
|
|
893
|
+
} else {
|
|
894
|
+
let cmds = getScriptRequires(uniqueName);
|
|
895
|
+
_cmdMap = arrayToCmdMap(cmds)
|
|
896
|
+
listCmd()
|
|
897
|
+
|
|
898
|
+
for (let un of cmds) {
|
|
899
|
+
let path = getFullPath(un);
|
|
900
|
+
let text = removeComment(String(_fs.readFileSync(path)))
|
|
901
|
+
regEach(text, /[^$]require\s*\(([^()]+)\)/g, r=> {
|
|
902
|
+
if (r[1]) {
|
|
903
|
+
try {
|
|
904
|
+
requireG(evalVal(r[1]))
|
|
905
|
+
} catch (e) {
|
|
906
|
+
error(e)
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
});
|
|
910
|
+
regEach(text, /\$import\s*\(([^()]+)\)/g, r=> {
|
|
911
|
+
if (r[1]) {
|
|
912
|
+
importG(evalVal(r[1])).catch(e => error(e))
|
|
913
|
+
}
|
|
914
|
+
});
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
},
|
|
918
|
+
args: {
|
|
919
|
+
fileIndex: '文件下标'
|
|
920
|
+
},
|
|
921
|
+
short: 'd'
|
|
922
|
+
},
|
|
869
923
|
file: {
|
|
870
924
|
comment: '工作文件模式',
|
|
871
925
|
exeFn: async (args) => {
|
|
@@ -975,10 +1029,8 @@ const keywordDef = {
|
|
|
975
1029
|
return
|
|
976
1030
|
}
|
|
977
1031
|
let cmds = filterCmd('^e ' + toJsirFileName(args[0]).replace(/^[eif]\s+/, '') + '$')
|
|
978
|
-
_cmdMap = arrayToCmdMap(cmds)
|
|
979
|
-
listCmd()
|
|
980
1032
|
if (cmds.length === 1) {
|
|
981
|
-
await runCmd(
|
|
1033
|
+
await runCmd(args.slice(1).join(' '), cmds[0])
|
|
982
1034
|
}
|
|
983
1035
|
},
|
|
984
1036
|
args: {
|
|
@@ -1008,7 +1060,32 @@ const keywordDef = {
|
|
|
1008
1060
|
}
|
|
1009
1061
|
}
|
|
1010
1062
|
|
|
1011
|
-
async function dealKeyword(
|
|
1063
|
+
async function dealKeyword(items) {
|
|
1064
|
+
let keyword;
|
|
1065
|
+
let args = []
|
|
1066
|
+
for (let item of items) {
|
|
1067
|
+
if (!item.endsWith(" ") && item.startsWith(".")) {
|
|
1068
|
+
if (keyword) {
|
|
1069
|
+
await _dealKeyword(keyword, args)
|
|
1070
|
+
}
|
|
1071
|
+
keyword = trim(item);
|
|
1072
|
+
args = []
|
|
1073
|
+
continue
|
|
1074
|
+
}
|
|
1075
|
+
if (keyword) {
|
|
1076
|
+
args.push(trim(item))
|
|
1077
|
+
}
|
|
1078
|
+
}
|
|
1079
|
+
if (keyword) {
|
|
1080
|
+
await _dealKeyword(keyword, args)
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1084
|
+
async function _dealKeyword(keyword, args) {
|
|
1085
|
+
if (keyword === '.') {
|
|
1086
|
+
keyword = '.help'
|
|
1087
|
+
}
|
|
1088
|
+
keyword = keyword.substring(1)
|
|
1012
1089
|
let unMatched = true;
|
|
1013
1090
|
for (let key of Object.keys(keywordDef)) {
|
|
1014
1091
|
let item = keywordDef[key]
|
|
@@ -1020,51 +1097,80 @@ async function dealKeyword(keyword, args) {
|
|
|
1020
1097
|
}
|
|
1021
1098
|
}
|
|
1022
1099
|
if (unMatched) {
|
|
1023
|
-
warn(
|
|
1100
|
+
warn(`unknown keyword: ${keyword}`)
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
function removeComment(text) {
|
|
1105
|
+
let result = []
|
|
1106
|
+
let start = false
|
|
1107
|
+
for (let line of text.split('\n')) {
|
|
1108
|
+
if (trim(line).startsWith("/*")) {
|
|
1109
|
+
start = true;
|
|
1110
|
+
continue
|
|
1111
|
+
}
|
|
1112
|
+
if (trim(line).endsWith("*/")) {
|
|
1113
|
+
start = false;
|
|
1114
|
+
continue
|
|
1115
|
+
}
|
|
1116
|
+
if (trim(line).startsWith("//")) {
|
|
1117
|
+
continue
|
|
1118
|
+
}
|
|
1119
|
+
if (start) {
|
|
1120
|
+
continue
|
|
1121
|
+
}
|
|
1122
|
+
result.push(line)
|
|
1024
1123
|
}
|
|
1124
|
+
return result.join("\n")
|
|
1025
1125
|
}
|
|
1026
1126
|
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1127
|
+
function _getScriptRequires(uniqueName, cmds, links) {
|
|
1128
|
+
let path = getFullPath(uniqueName);
|
|
1129
|
+
let text = removeComment(String(_fs.readFileSync(path)))
|
|
1130
|
+
let temp = []
|
|
1131
|
+
regEach(text, /\$require\s*\(([^()]+)\)/g, r=> {
|
|
1132
|
+
if (r[1]) {
|
|
1133
|
+
let matchStr
|
|
1134
|
+
try {
|
|
1135
|
+
matchStr = evalVal(r[1])
|
|
1136
|
+
} catch (e) {}
|
|
1137
|
+
if (matchStr) {
|
|
1138
|
+
temp.push(filterRequire(parseUniqueName(uniqueName)[0], matchStr))
|
|
1139
|
+
}
|
|
1140
|
+
}
|
|
1141
|
+
});
|
|
1142
|
+
regEach(text, /\$requires\s*\(([^()]+)\)/g, r=> {
|
|
1143
|
+
if (r[1]) {
|
|
1144
|
+
for (const item of r[1].split(/,/)) {
|
|
1145
|
+
let matchStr
|
|
1146
|
+
try {
|
|
1147
|
+
matchStr = evalVal(item)
|
|
1148
|
+
} catch (e) {}
|
|
1149
|
+
if (matchStr) {
|
|
1150
|
+
temp.push(filterRequire(parseUniqueName(uniqueName)[0], matchStr))
|
|
1151
|
+
}
|
|
1152
|
+
}
|
|
1153
|
+
}
|
|
1154
|
+
});
|
|
1155
|
+
temp = [...new Set(temp)]
|
|
1156
|
+
temp.forEach(i => {
|
|
1157
|
+
if (links.indexOf(i) !== -1) {
|
|
1158
|
+
let items = [...links, i];
|
|
1159
|
+
let errorStr = items.map(item => item === i ? warnStr(item):item).join("->")
|
|
1160
|
+
throw `circle deps: ${errorStr}`
|
|
1161
|
+
}
|
|
1162
|
+
})
|
|
1163
|
+
cmds.push(...temp)
|
|
1164
|
+
for (let un of temp) {
|
|
1165
|
+
_getScriptRequires(un, cmds, [...links, un])
|
|
1166
|
+
}
|
|
1167
|
+
}
|
|
1168
|
+
|
|
1169
|
+
function getScriptRequires(uniqueName) {
|
|
1170
|
+
let cmds = [];
|
|
1171
|
+
_getScriptRequires(uniqueName, cmds, [uniqueName]);
|
|
1172
|
+
return [...new Set(cmds)]
|
|
1173
|
+
}
|
|
1068
1174
|
|
|
1069
1175
|
function getComments(i, cmdName, text, cols = [], col) {
|
|
1070
1176
|
let docLines = [infoStr(getCmdMd5Key(parseUniqueName(cmdName)[1])) + " - " + infoStr(i)]
|
|
@@ -1371,7 +1477,7 @@ function getArgDef(text) {
|
|
|
1371
1477
|
let exeStr = trim(reget(text, /\$defArgs\(\s*(\{[\s\S]*?\})\s*\)/))
|
|
1372
1478
|
let argDef = {}
|
|
1373
1479
|
try {
|
|
1374
|
-
argDef =
|
|
1480
|
+
argDef = (exeStr ? evalVal(exeStr) : {}) || {}
|
|
1375
1481
|
let array = []
|
|
1376
1482
|
for (let key of Object.keys(argDef)) {
|
|
1377
1483
|
if (array.hasOwnProperty(key)) {
|
|
@@ -1401,9 +1507,11 @@ function getArgDef(text) {
|
|
|
1401
1507
|
}
|
|
1402
1508
|
|
|
1403
1509
|
function setTips(key, value, onRm) {
|
|
1404
|
-
global.$tips[
|
|
1510
|
+
let vals = getOr(global.$tips, key, []);
|
|
1511
|
+
vals.push(value)
|
|
1512
|
+
global.$tips[key] = [...new Set(vals)]
|
|
1405
1513
|
if (onRm) {
|
|
1406
|
-
_tipsOnRm[
|
|
1514
|
+
getOr(_tipsOnRm, key, []).push(onRm)
|
|
1407
1515
|
}
|
|
1408
1516
|
}
|
|
1409
1517
|
|
|
@@ -1425,14 +1533,16 @@ function delTips(...keys) {
|
|
|
1425
1533
|
}
|
|
1426
1534
|
|
|
1427
1535
|
function tipsOnRmCallback(key) {
|
|
1428
|
-
let
|
|
1536
|
+
let callbacks = _tipsOnRm[key]
|
|
1429
1537
|
delete _tipsOnRm[key]
|
|
1430
|
-
if (
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1538
|
+
if (callbacks && callbacks.length > 0) {
|
|
1539
|
+
for (let callback of callbacks) {
|
|
1540
|
+
try {
|
|
1541
|
+
callback()
|
|
1542
|
+
} catch (e) {
|
|
1543
|
+
$log(errorStack(e))
|
|
1544
|
+
error(`[${key}] OnRmCallback: ${String(e)}`)
|
|
1545
|
+
}
|
|
1436
1546
|
}
|
|
1437
1547
|
}
|
|
1438
1548
|
}
|
|
@@ -1441,13 +1551,11 @@ function getCmdMd5Key(str) {
|
|
|
1441
1551
|
return '0x' + md5(str).substr(0, 8);
|
|
1442
1552
|
}
|
|
1443
1553
|
|
|
1444
|
-
|
|
1445
|
-
cmdMatchStr
|
|
1446
|
-
|
|
1447
|
-
if (cmdMatchStr.startsWith("*")) {
|
|
1448
|
-
nullable = true;
|
|
1449
|
-
cmdMatchStr = trim(cmdMatchStr.substr(1));
|
|
1554
|
+
function filterRequire(currSpace, cmdMatchStr) {
|
|
1555
|
+
if (typeof cmdMatchStr === 'number') {
|
|
1556
|
+
cmdMatchStr = '0x' + pad(8, BigNumber(cmdMatchStr).toString(16), '0');
|
|
1450
1557
|
}
|
|
1558
|
+
cmdMatchStr = trim(cmdMatchStr);
|
|
1451
1559
|
|
|
1452
1560
|
let cmds;
|
|
1453
1561
|
let uName = toUniqueName(cmdMatchStr, currSpace)
|
|
@@ -1466,30 +1574,22 @@ async function _requireSource(currSpace, cmdMatchStr, printLog = false) {
|
|
|
1466
1574
|
cmds = cmds.filter(cmd => parseUniqueName(cmd)[0] === (appointSpace || currSpace))
|
|
1467
1575
|
}
|
|
1468
1576
|
}
|
|
1469
|
-
if (cmds.length
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
}
|
|
1473
|
-
throw `none unique match: ${cmdMatchStr}`
|
|
1577
|
+
if (cmds.length > 1) {
|
|
1578
|
+
throw `multiple match: ${cmdMatchStr}`
|
|
1579
|
+
} else if (cmds.length < 1) {
|
|
1580
|
+
throw `none match: ${cmdMatchStr}`
|
|
1474
1581
|
}
|
|
1582
|
+
return cmds[0];
|
|
1583
|
+
}
|
|
1475
1584
|
|
|
1585
|
+
async function _requireSource(currSpace, cmdMatchStr) {
|
|
1476
1586
|
let result
|
|
1477
|
-
let uniqueName =
|
|
1587
|
+
let uniqueName = filterRequire(currSpace, cmdMatchStr);
|
|
1478
1588
|
let path = getFullPath(uniqueName)
|
|
1479
1589
|
let text = String(_fs.readFileSync(path))
|
|
1480
1590
|
let pair = parseUniqueName(uniqueName)
|
|
1481
1591
|
if (pair[1].startsWith('i ')) {
|
|
1482
|
-
|
|
1483
|
-
result = await evalText(text, uniqueName)
|
|
1484
|
-
} catch (e) {
|
|
1485
|
-
if (nullable) {
|
|
1486
|
-
warn(`require [${cmdMatchStr}] failed`)
|
|
1487
|
-
$log(errorStack(e))
|
|
1488
|
-
return null;
|
|
1489
|
-
} else {
|
|
1490
|
-
throw e
|
|
1491
|
-
}
|
|
1492
|
-
}
|
|
1592
|
+
result = await evalText(text, uniqueName)
|
|
1493
1593
|
} else if (pair[1].startsWith('e ')) {
|
|
1494
1594
|
result = async (argsStr) => {
|
|
1495
1595
|
return await runCmd(trim(argsStr), uniqueName, text)
|
|
@@ -1498,37 +1598,24 @@ async function _requireSource(currSpace, cmdMatchStr, printLog = false) {
|
|
|
1498
1598
|
result = text;
|
|
1499
1599
|
}
|
|
1500
1600
|
if (!vl(result)) {
|
|
1501
|
-
if (nullable) {
|
|
1502
|
-
return null;
|
|
1503
|
-
}
|
|
1504
1601
|
throw `invalid result: ${cmdMatchStr}`
|
|
1505
1602
|
}
|
|
1506
1603
|
if (getType(result) === 'Function' || getType(result) === 'AsyncFunction') {
|
|
1507
1604
|
let tmp = result;
|
|
1508
1605
|
result = (...args) => {
|
|
1509
1606
|
let resp
|
|
1510
|
-
let startTime = Date.now();
|
|
1511
|
-
let pl = () => {
|
|
1512
|
-
if (printLog) {
|
|
1513
|
-
info(`${uniqueName} finished in ${Date.now() - startTime}ms`)
|
|
1514
|
-
}
|
|
1515
|
-
}
|
|
1516
1607
|
try {
|
|
1517
1608
|
resp = tmp(...args)
|
|
1518
1609
|
if (getType(resp) === 'Promise') {
|
|
1519
1610
|
return resp.then(result => {
|
|
1520
|
-
pl();
|
|
1521
1611
|
return Promise.resolve(result);
|
|
1522
1612
|
}).catch(e => {
|
|
1523
|
-
pl();
|
|
1524
1613
|
return Promise.reject(errorTag(e, uniqueName))
|
|
1525
1614
|
})
|
|
1526
1615
|
} else {
|
|
1527
|
-
pl();
|
|
1528
1616
|
return resp;
|
|
1529
1617
|
}
|
|
1530
1618
|
} catch (e) {
|
|
1531
|
-
pl();
|
|
1532
1619
|
throw errorTag(e, uniqueName);
|
|
1533
1620
|
}
|
|
1534
1621
|
}
|
|
@@ -1536,27 +1623,6 @@ async function _requireSource(currSpace, cmdMatchStr, printLog = false) {
|
|
|
1536
1623
|
return result
|
|
1537
1624
|
}
|
|
1538
1625
|
|
|
1539
|
-
async function _requireSources(currSpace, ...matchItems) {
|
|
1540
|
-
let result = []
|
|
1541
|
-
for (let i = 0; i < matchItems.length; i++) {
|
|
1542
|
-
let curr = matchItems[i]
|
|
1543
|
-
let next = matchItems[i + 1]
|
|
1544
|
-
if (typeof curr === 'number') {
|
|
1545
|
-
curr = '0x' + pad(8, BigNumber(curr).toString(16), '0');
|
|
1546
|
-
}
|
|
1547
|
-
if (typeof next === 'number') {
|
|
1548
|
-
next = '0x' + pad(8, BigNumber(next).toString(16), '0');
|
|
1549
|
-
}
|
|
1550
|
-
if (typeof curr === 'string' && typeof next !== "string") {
|
|
1551
|
-
result.push(await _requireSource(currSpace, curr, next))
|
|
1552
|
-
i++
|
|
1553
|
-
} else if (typeof curr === 'string') {
|
|
1554
|
-
result.push(await _requireSource(currSpace, curr))
|
|
1555
|
-
}
|
|
1556
|
-
}
|
|
1557
|
-
return result
|
|
1558
|
-
}
|
|
1559
|
-
|
|
1560
1626
|
async function evalText($text = '', $cmdName = '', $args = []) {
|
|
1561
1627
|
let currSpace;
|
|
1562
1628
|
if ($cmdName) {
|
|
@@ -1566,10 +1632,14 @@ async function evalText($text = '', $cmdName = '', $args = []) {
|
|
|
1566
1632
|
currSpace = $defaultSpace
|
|
1567
1633
|
}
|
|
1568
1634
|
let $requires = async (...matchItems) => {
|
|
1569
|
-
|
|
1635
|
+
let result = []
|
|
1636
|
+
for (let i = 0; i < matchItems.length; i++) {
|
|
1637
|
+
result.push(await _requireSource(currSpace, matchItems[i]))
|
|
1638
|
+
}
|
|
1639
|
+
return result
|
|
1570
1640
|
}
|
|
1571
|
-
let $require = async (matchItem
|
|
1572
|
-
return
|
|
1641
|
+
let $require = async (matchItem) => {
|
|
1642
|
+
return await _requireSource(currSpace, matchItem)
|
|
1573
1643
|
}
|
|
1574
1644
|
try {
|
|
1575
1645
|
return await evalCode($text, $cmdName, $args,
|
package/package.json
CHANGED
package/util.js
CHANGED
|
@@ -18,6 +18,9 @@ const emptyFn = ()=>{}
|
|
|
18
18
|
const dayJs = require('dayjs')
|
|
19
19
|
const table = require('console.table')
|
|
20
20
|
const _fs = require("fs");
|
|
21
|
+
if (module.paths.indexOf(globalDirectories.npm.packages) === -1) {
|
|
22
|
+
module.paths.push(globalDirectories.npm.packages)
|
|
23
|
+
}
|
|
21
24
|
|
|
22
25
|
let _globalLog = createLimitLogger2(`${setting.name}.log`, null, false)
|
|
23
26
|
const $log = str => {
|
|
@@ -329,32 +332,32 @@ function dataFile(fileName, fn, fmt, defaultObj = {}, returnStr = false) {
|
|
|
329
332
|
}
|
|
330
333
|
}
|
|
331
334
|
|
|
332
|
-
function requireG(
|
|
335
|
+
function requireG(moduleName){
|
|
333
336
|
try {
|
|
334
|
-
let result = require(
|
|
337
|
+
let result = require(moduleName);
|
|
335
338
|
if (result) {
|
|
336
339
|
return result
|
|
337
340
|
}
|
|
338
341
|
} catch (e) {}
|
|
339
|
-
let path = globalDirectories.npm.packages + '/' +
|
|
342
|
+
let path = globalDirectories.npm.packages + '/' + moduleName;
|
|
340
343
|
if (!fs.existsSync(path)) {
|
|
341
|
-
console.log(warnStr(`npm install -g ${
|
|
342
|
-
throw `${
|
|
344
|
+
console.log(warnStr(`npm install -g ${moduleName}`))
|
|
345
|
+
throw `${moduleName} not found, use above cmd to install it`;
|
|
343
346
|
}
|
|
344
347
|
return require(path);
|
|
345
348
|
}
|
|
346
349
|
|
|
347
|
-
async function importG(
|
|
350
|
+
async function importG(moduleName) {
|
|
348
351
|
try {
|
|
349
|
-
let result = await import(
|
|
352
|
+
let result = await import(moduleName);
|
|
350
353
|
if (result) {
|
|
351
354
|
return result
|
|
352
355
|
}
|
|
353
356
|
} catch (e) {}
|
|
354
|
-
let path = globalDirectories.npm.packages + '/' +
|
|
357
|
+
let path = globalDirectories.npm.packages + '/' + moduleName;
|
|
355
358
|
if (!fs.existsSync(path)) {
|
|
356
|
-
console.log(warnStr(`npm install -g ${
|
|
357
|
-
throw `${
|
|
359
|
+
console.log(warnStr(`npm install -g ${moduleName}`))
|
|
360
|
+
throw `${moduleName} not found, use above cmd to install it`;
|
|
358
361
|
}
|
|
359
362
|
return await import(path);
|
|
360
363
|
}
|
|
@@ -422,7 +425,7 @@ function getConfig(key, defaultVal) {
|
|
|
422
425
|
if (typeof defaultVal === "string") {
|
|
423
426
|
defaultVal = trim(defaultVal)
|
|
424
427
|
}
|
|
425
|
-
if (!vl(config[key]) &&
|
|
428
|
+
if (!vl(config[key]) && defaultVal !== undefined) {
|
|
426
429
|
return defaultVal
|
|
427
430
|
}
|
|
428
431
|
let val = config[key]
|
|
@@ -732,7 +735,7 @@ function _linuxAskAndKill(rl, keys){
|
|
|
732
735
|
}
|
|
733
736
|
|
|
734
737
|
function trim(obj) {
|
|
735
|
-
return obj ? String(obj).trim():""
|
|
738
|
+
return vl(obj) ? String(obj).trim():""
|
|
736
739
|
}
|
|
737
740
|
|
|
738
741
|
function getLibDataDir() {
|