jsir 2.1.7 → 2.2.0
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/README.md +21 -21
- package/cmd/oaa.js +518 -292
- package/deps/evalCode.js +5 -5
- package/deps/example.js +6 -14
- package/deps/util.js +228 -144
- package/package.json +1 -2
package/cmd/oaa.js
CHANGED
|
@@ -5,16 +5,16 @@ const {
|
|
|
5
5
|
getCbText, e, sleep, objDataFile, vl, md5, BigNumber,
|
|
6
6
|
arrayDataFile, infoStr, warnStr, errorStack,
|
|
7
7
|
getInfo, ei, pad, msgStr, getType,
|
|
8
|
-
|
|
8
|
+
isArgsMatch, draftQuery, setConfig,
|
|
9
9
|
$log, $draft, getTextComments, getOr, importG, requireG,
|
|
10
|
-
clearConsole
|
|
10
|
+
clearConsole, trimText
|
|
11
11
|
} = $lib;
|
|
12
12
|
const _args = process.argv.slice(2).map(trim);
|
|
13
13
|
const evalCode = require('../deps/evalCode')
|
|
14
14
|
const {evalVal} = require('../deps/cleanEval')
|
|
15
15
|
const _chokidar = require('chokidar');
|
|
16
16
|
const setting = require('../deps/setting')
|
|
17
|
-
const
|
|
17
|
+
const fs = require('fs')
|
|
18
18
|
const readline = require("readline");
|
|
19
19
|
const packageJson = require("../package.json");
|
|
20
20
|
const example = require("../deps/example");
|
|
@@ -31,57 +31,70 @@ const $config = {
|
|
|
31
31
|
get: getConfig,
|
|
32
32
|
set: setConfig
|
|
33
33
|
}
|
|
34
|
-
const
|
|
35
|
-
const info = (msg) => $lib.info(msg, CONSOLE);
|
|
36
|
-
const warn = (msg) => $lib.warn(msg, CONSOLE);
|
|
37
|
-
const error = (msg, tag) => $lib.error(msg, tag, CONSOLE);
|
|
34
|
+
const console = Object.assign({}, global.console);
|
|
38
35
|
|
|
36
|
+
let lastFilterArg = '';
|
|
39
37
|
let _cmdMapFile = setting.name + 'CmdMap.json'
|
|
40
38
|
let _cmdMap = {}
|
|
41
39
|
let _rl
|
|
42
40
|
let _rlHistory = []
|
|
43
41
|
let _haveWrapperInput = true
|
|
44
42
|
let _noAppendNextLine = true
|
|
43
|
+
let _onLazyTempCode = null
|
|
45
44
|
|
|
46
45
|
const _onLazyGetMap = {}
|
|
47
46
|
const _data = {}
|
|
48
47
|
const _lazyTime = 5 * 60 * 1000
|
|
49
|
-
const _dealOnLazyGet = (key) => {
|
|
48
|
+
const _dealOnLazyGet = (key, onLazyTempCode) => {
|
|
50
49
|
if (_noAppendNextLine || !_onLazyGetMap[key]) {
|
|
51
50
|
return _data[key]
|
|
52
51
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
item.startTime = Date.now() + _lazyTime
|
|
56
|
-
return _data[key]
|
|
57
|
-
}
|
|
58
|
-
let flag = _onLazyGetMap[key].fn()
|
|
59
|
-
if (getType(flag)=== 'Promise') {
|
|
60
|
-
return new Promise(async (resolve, reject) => {
|
|
61
|
-
try {
|
|
62
|
-
await flag
|
|
63
|
-
item.startTime = Date.now() + _lazyTime
|
|
64
|
-
resolve(_data[key])
|
|
65
|
-
} catch (e) {
|
|
66
|
-
reject(e)
|
|
67
|
-
}
|
|
68
|
-
})
|
|
69
|
-
} else {
|
|
70
|
-
item.startTime = Date.now() + _lazyTime
|
|
52
|
+
if (Date.now() <= _onLazyGetMap[key]) {
|
|
53
|
+
_onLazyGetMap[key] = Date.now() + _lazyTime
|
|
71
54
|
return _data[key]
|
|
72
55
|
}
|
|
56
|
+
return new Promise(async (resolve, reject) => {
|
|
57
|
+
let inputCode = onLazyTempCode || await nextLine(i => i, "TEMP_CODE: ", true)
|
|
58
|
+
if (inputCode !== _onLazyTempCode) {
|
|
59
|
+
reject("tempCode check failed")
|
|
60
|
+
} else {
|
|
61
|
+
_onLazyGetMap[key] = Date.now() + _lazyTime
|
|
62
|
+
resolve(_data[key])
|
|
63
|
+
}
|
|
64
|
+
})
|
|
73
65
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
66
|
+
|
|
67
|
+
const _dataSet = (key, val, onLazyTempCode) => {
|
|
68
|
+
let useOnLazy = false;
|
|
69
|
+
if (typeof onLazyTempCode === 'string') {
|
|
70
|
+
useOnLazy = true
|
|
71
|
+
if (!vl(_onLazyTempCode)) {
|
|
72
|
+
_onLazyTempCode = trim(onLazyTempCode);
|
|
73
|
+
}
|
|
74
|
+
} else if (true === onLazyTempCode) {
|
|
75
|
+
useOnLazy = true
|
|
76
|
+
} else if (onLazyTempCode !== undefined) {
|
|
77
|
+
throw "invalid tempCode defined"
|
|
78
|
+
}
|
|
79
|
+
if (useOnLazy) {
|
|
80
|
+
_onLazyGetMap[key] = Date.now() + _lazyTime
|
|
81
|
+
if (!vl(_onLazyTempCode)) {
|
|
82
|
+
return new Promise(async (resolve, reject) => {
|
|
83
|
+
let tempCode = await nextLine(i => i, "SET TEMP_CODE: ", true)
|
|
84
|
+
if (vl(tempCode)) {
|
|
85
|
+
_onLazyTempCode = trim(tempCode);
|
|
86
|
+
resolve(_data[key] = val)
|
|
87
|
+
} else {
|
|
88
|
+
reject("invalid tempCode")
|
|
89
|
+
}
|
|
90
|
+
});
|
|
79
91
|
}
|
|
80
92
|
}
|
|
93
|
+
|
|
81
94
|
return _data[key] = val
|
|
82
95
|
}
|
|
83
96
|
const $data = {
|
|
84
|
-
get: (key, defaultVal) => {
|
|
97
|
+
get: (key, defaultVal, onLazyTempCode) => {
|
|
85
98
|
if (!vl(_data[key])) {
|
|
86
99
|
if (defaultVal !== undefined) {
|
|
87
100
|
return defaultVal
|
|
@@ -89,17 +102,17 @@ const $data = {
|
|
|
89
102
|
return _data[key]
|
|
90
103
|
}
|
|
91
104
|
}
|
|
92
|
-
return _dealOnLazyGet(key)
|
|
105
|
+
return _dealOnLazyGet(key, onLazyTempCode)
|
|
93
106
|
},
|
|
94
|
-
gos: (key, defaultVal,
|
|
107
|
+
gos: (key, defaultVal, onLazyTempCode) => {
|
|
95
108
|
if (!vl(_data[key])) {
|
|
96
109
|
if (defaultVal !== undefined) {
|
|
97
|
-
return _dataSet(key, defaultVal,
|
|
110
|
+
return _dataSet(key, defaultVal, onLazyTempCode)
|
|
98
111
|
} else {
|
|
99
112
|
return _data[key]
|
|
100
113
|
}
|
|
101
114
|
}
|
|
102
|
-
return _dealOnLazyGet(key)
|
|
115
|
+
return _dealOnLazyGet(key, onLazyTempCode)
|
|
103
116
|
},
|
|
104
117
|
set: _dataSet,
|
|
105
118
|
del: (key) => {
|
|
@@ -124,7 +137,7 @@ function getFileOpenExe(fileName) {
|
|
|
124
137
|
if (strs.length > 1) {
|
|
125
138
|
suffix = strs[strs.length - 1]
|
|
126
139
|
}
|
|
127
|
-
let defaultExe = getConfig("defaultExe", "
|
|
140
|
+
let defaultExe = getConfig("defaultExe", "open");
|
|
128
141
|
if (!suffix) {
|
|
129
142
|
return defaultExe
|
|
130
143
|
}
|
|
@@ -143,7 +156,7 @@ function checkWorkspaces() {
|
|
|
143
156
|
if (space === 'local') {
|
|
144
157
|
continue
|
|
145
158
|
}
|
|
146
|
-
if (
|
|
159
|
+
if (fs.existsSync(workspace)) {
|
|
147
160
|
$workspaceMap[space] = workspace;
|
|
148
161
|
}
|
|
149
162
|
}
|
|
@@ -154,7 +167,8 @@ function checkWorkspaces() {
|
|
|
154
167
|
async function start() {
|
|
155
168
|
checkWorkspaces()
|
|
156
169
|
|
|
157
|
-
|
|
170
|
+
lastFilterArg = getConfig("lastFilterArg");
|
|
171
|
+
_cmdMap = objDataFile(_cmdMapFile);
|
|
158
172
|
if (_args[0]) {
|
|
159
173
|
let line = _args.map(i => {
|
|
160
174
|
if (/\s+/.test(i) || !i) {
|
|
@@ -169,11 +183,11 @@ async function start() {
|
|
|
169
183
|
}
|
|
170
184
|
|
|
171
185
|
async function getFileWatcher(workFilePath, text) {
|
|
172
|
-
info("workFile open " + workFilePath)
|
|
186
|
+
console.info("workFile open " + workFilePath)
|
|
173
187
|
if (!_fileWatcherMap[workFilePath]) {
|
|
174
|
-
|
|
188
|
+
fs.unlinkSync(workFilePath)
|
|
175
189
|
await sleep(1000)
|
|
176
|
-
|
|
190
|
+
fs.writeFileSync(workFilePath, text)
|
|
177
191
|
let watcher = _chokidar.watch([workFilePath]);
|
|
178
192
|
_fileWatcherMap[workFilePath] = watcher;
|
|
179
193
|
setTips("FILE", null, () => {
|
|
@@ -185,7 +199,7 @@ async function getFileWatcher(workFilePath, text) {
|
|
|
185
199
|
|
|
186
200
|
function closeFileWatcher(workFilePath) {
|
|
187
201
|
let watcher = _fileWatcherMap[workFilePath]
|
|
188
|
-
info("workFile close " + workFilePath)
|
|
202
|
+
console.info("workFile close " + workFilePath)
|
|
189
203
|
watcher.unwatch([workFilePath])
|
|
190
204
|
watcher.close()
|
|
191
205
|
delete _fileWatcherMap[workFilePath]
|
|
@@ -201,23 +215,23 @@ async function workFile(uniqueName) {
|
|
|
201
215
|
|
|
202
216
|
async function watchFile(uniqueName) {
|
|
203
217
|
let workFilePath = getFullPath(uniqueName);
|
|
204
|
-
if (!
|
|
205
|
-
|
|
218
|
+
if (!fs.existsSync(workFilePath)) {
|
|
219
|
+
fs.writeFileSync(workFilePath, '');
|
|
206
220
|
}
|
|
207
|
-
let text = String(
|
|
221
|
+
let text = String(fs.readFileSync(workFilePath))
|
|
208
222
|
let watcher = await getFileWatcher(workFilePath, text)
|
|
209
223
|
|
|
210
224
|
if (watcher) {
|
|
211
225
|
watcher.on('change', async () => {
|
|
212
|
-
let newText = String(
|
|
226
|
+
let newText = String(fs.readFileSync(workFilePath));
|
|
213
227
|
let exeStr = getExeStr(text, newText);
|
|
214
228
|
text = newText;
|
|
215
229
|
if (trim(exeStr)) {
|
|
216
230
|
try {
|
|
217
|
-
|
|
231
|
+
console.log("\n" + infoStr("------ workFile run ------"))
|
|
218
232
|
await wrapperInput("# " + exeStr)
|
|
219
233
|
} catch (e) {
|
|
220
|
-
error(e)
|
|
234
|
+
console.error(e)
|
|
221
235
|
}
|
|
222
236
|
}
|
|
223
237
|
}).on("unlink", () => {
|
|
@@ -383,7 +397,7 @@ function _nextLine(callback, promptStr, hidden, resolve, end, isText) {
|
|
|
383
397
|
closeRl()
|
|
384
398
|
inputStr = inputStr.replace(/\s+$/, '');
|
|
385
399
|
if (!isText && /^\d+$/.test(inputStr) && /^\s+/.test(textLine)) {
|
|
386
|
-
inputStr =
|
|
400
|
+
inputStr = '.e ' + inputStr;
|
|
387
401
|
}
|
|
388
402
|
let pro = (callback || wrapperInput)(inputStr);
|
|
389
403
|
resolve && resolve(inputStr);
|
|
@@ -393,12 +407,8 @@ function _nextLine(callback, promptStr, hidden, resolve, end, isText) {
|
|
|
393
407
|
if ((callback || wrapperInput) === wrapperInput) {
|
|
394
408
|
if (!_noAppendNextLine) {
|
|
395
409
|
if (/^\s+$/.test(textLine)) {
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
} else {
|
|
399
|
-
clearConsole();
|
|
400
|
-
}
|
|
401
|
-
listCmd();
|
|
410
|
+
clearConsole(textLine.length - 1)
|
|
411
|
+
filterCmdAndList();
|
|
402
412
|
nextLine(null, defaultPromptStr(true))
|
|
403
413
|
} else {
|
|
404
414
|
nextLine()
|
|
@@ -423,11 +433,11 @@ async function nextText(callback, end, hidden) {
|
|
|
423
433
|
async function save(args) {
|
|
424
434
|
let uniqueName = toUniqueName(toJsirFileName(args.join(' ')))
|
|
425
435
|
let path = getFullPath(uniqueName)
|
|
426
|
-
if (
|
|
427
|
-
warn(`${path} already exist`)
|
|
428
|
-
|
|
436
|
+
if (fs.existsSync(path)) {
|
|
437
|
+
console.warn(`${path} already exist`)
|
|
438
|
+
resetCmdMap({
|
|
429
439
|
1: uniqueName
|
|
430
|
-
}
|
|
440
|
+
});
|
|
431
441
|
listCmd()
|
|
432
442
|
return
|
|
433
443
|
}
|
|
@@ -439,26 +449,31 @@ async function save(args) {
|
|
|
439
449
|
} else {
|
|
440
450
|
resp = await getCbText();
|
|
441
451
|
}
|
|
442
|
-
|
|
443
|
-
info(`${path} created`)
|
|
444
|
-
|
|
452
|
+
fs.writeFileSync(path, resp)
|
|
453
|
+
console.info(`${path} created`)
|
|
454
|
+
resetCmdMap({
|
|
445
455
|
1: uniqueName
|
|
446
|
-
}
|
|
456
|
+
});
|
|
447
457
|
listCmd()
|
|
448
458
|
}
|
|
449
459
|
|
|
460
|
+
function resetCmdMap(cmdMap = {}) {
|
|
461
|
+
lastFilterArg = '';
|
|
462
|
+
_cmdMap = cmdMap;
|
|
463
|
+
}
|
|
464
|
+
|
|
450
465
|
function rename(dir, oldName, newName) {
|
|
451
466
|
let old = dir + '/' + oldName;
|
|
452
467
|
let _new = dir + '/' + newName;
|
|
453
|
-
if (
|
|
454
|
-
warn(`${_new} already exist`)
|
|
468
|
+
if (fs.existsSync(_new)) {
|
|
469
|
+
console.warn(`${_new} already exist`)
|
|
455
470
|
} else {
|
|
456
|
-
|
|
457
|
-
info(`${_new} renamed`)
|
|
471
|
+
fs.renameSync(old, _new)
|
|
472
|
+
console.info(`${_new} renamed`)
|
|
458
473
|
}
|
|
459
|
-
|
|
474
|
+
resetCmdMap({
|
|
460
475
|
1: toUniqueName(_new)
|
|
461
|
-
}
|
|
476
|
+
});
|
|
462
477
|
listCmd()
|
|
463
478
|
}
|
|
464
479
|
|
|
@@ -471,7 +486,7 @@ function listCmd(tmpMap) {
|
|
|
471
486
|
}
|
|
472
487
|
|
|
473
488
|
tmpMap = arrayToCmdMap(Object.values(tmpMap)
|
|
474
|
-
.filter(item =>
|
|
489
|
+
.filter(item => fs.existsSync(getFullPath(item)))
|
|
475
490
|
.sort((a,b) => {
|
|
476
491
|
a = parseUniqueName(a)[1];
|
|
477
492
|
b = parseUniqueName(b)[1];
|
|
@@ -492,7 +507,7 @@ function listCmd(tmpMap) {
|
|
|
492
507
|
|
|
493
508
|
let items = []
|
|
494
509
|
for (let i of Object.keys(tmpMap)) {
|
|
495
|
-
let text = String(
|
|
510
|
+
let text = String(fs.readFileSync(getFullPath(tmpMap[i])))
|
|
496
511
|
let pair = parseUniqueName(tmpMap[i])
|
|
497
512
|
let name = trimJsirFileName(pair[1])
|
|
498
513
|
let suffix = getJsirFileSuffix(pair[1])
|
|
@@ -504,12 +519,12 @@ function listCmd(tmpMap) {
|
|
|
504
519
|
items.push(item)
|
|
505
520
|
|
|
506
521
|
if (!text) {
|
|
507
|
-
text = String(
|
|
522
|
+
text = String(fs.readFileSync(getFullPath(tmpMap[i])))
|
|
508
523
|
}
|
|
509
524
|
getComments(i, tmpMap[i], text, items, item)
|
|
510
525
|
}
|
|
511
526
|
if (Object.keys(items).length === 0) {
|
|
512
|
-
warn("no items")
|
|
527
|
+
console.warn("no items")
|
|
513
528
|
} else {
|
|
514
529
|
items.forEach(item => {
|
|
515
530
|
if (tmpList && item.key) {
|
|
@@ -527,7 +542,7 @@ function listCmd(tmpMap) {
|
|
|
527
542
|
item.name = pair[0] + '/' + item.name
|
|
528
543
|
}
|
|
529
544
|
})
|
|
530
|
-
|
|
545
|
+
console.table(items)
|
|
531
546
|
}
|
|
532
547
|
}
|
|
533
548
|
|
|
@@ -535,10 +550,14 @@ async function wrapperInput(str) {
|
|
|
535
550
|
try {
|
|
536
551
|
await _wrapperInput(str);
|
|
537
552
|
} catch (e) {
|
|
538
|
-
error(e)
|
|
553
|
+
console.error(e)
|
|
539
554
|
}
|
|
540
555
|
}
|
|
541
556
|
|
|
557
|
+
function getEditor() {
|
|
558
|
+
return getConfig("defaultEditor", "vi")
|
|
559
|
+
}
|
|
560
|
+
|
|
542
561
|
async function _wrapperInput(str) {
|
|
543
562
|
_haveWrapperInput = true;
|
|
544
563
|
global.$newInput = true;
|
|
@@ -547,21 +566,7 @@ async function _wrapperInput(str) {
|
|
|
547
566
|
if (!str) {
|
|
548
567
|
return;
|
|
549
568
|
}
|
|
550
|
-
if (
|
|
551
|
-
let cmdStr = trim(str.substr(1));
|
|
552
|
-
if (/^\d+$/.test(cmdStr)) {
|
|
553
|
-
if (_cmdMap[cmdStr]) {
|
|
554
|
-
ei('vi', [getFullPath(_cmdMap[cmdStr])])
|
|
555
|
-
} else {
|
|
556
|
-
ei('vi', ['-n'])
|
|
557
|
-
}
|
|
558
|
-
} else if (cmdStr) {
|
|
559
|
-
ei(cmdStr.split(/\s+/)[0],
|
|
560
|
-
enrichArgs(trim(cmdStr.replace(/^\S+/, ''))).map(trim));
|
|
561
|
-
} else {
|
|
562
|
-
ei('vi', ['-n'])
|
|
563
|
-
}
|
|
564
|
-
} else if (/^[`'"]/.test(str)) {
|
|
569
|
+
if (/^[`'"]/.test(str)) {
|
|
565
570
|
let fstr = str.substr(0, 1);
|
|
566
571
|
if (fstr === '`') {
|
|
567
572
|
let text = str.substr(1) + "\n" + await nextText(line => line, fstr)
|
|
@@ -569,7 +574,7 @@ async function _wrapperInput(str) {
|
|
|
569
574
|
} else {
|
|
570
575
|
let fLine = trim(str.substr(1))
|
|
571
576
|
if (fLine) {
|
|
572
|
-
|
|
577
|
+
console.log(draftQuery(fLine).join("\n"))
|
|
573
578
|
} else {
|
|
574
579
|
let text = str.substr(1) + "\n" + await nextText(line => line, fstr)
|
|
575
580
|
$draft(text)
|
|
@@ -580,33 +585,16 @@ async function _wrapperInput(str) {
|
|
|
580
585
|
let isStar = str.startsWith('*')
|
|
581
586
|
let text = trim(str.replace(/^[$#*]/, ''))
|
|
582
587
|
if (is$) {
|
|
583
|
-
|
|
588
|
+
console.log(await evalText('return ' + text))
|
|
584
589
|
} else if (isStar) {
|
|
585
|
-
|
|
586
|
-
text = '$context';
|
|
587
|
-
}
|
|
588
|
-
let result = await evalText('return ' + text)
|
|
589
|
-
if (!result || typeof result === 'string' || Object.keys(result).length === 0) {
|
|
590
|
-
CONSOLE.nable([getInfo(result)])
|
|
591
|
-
return
|
|
592
|
-
}
|
|
593
|
-
let rows = []
|
|
594
|
-
for (let key of Object.keys(result)) {
|
|
595
|
-
rows.push(getInfo(result[key], key))
|
|
596
|
-
}
|
|
597
|
-
if (rows.length === 0) {
|
|
598
|
-
warn("no items")
|
|
599
|
-
} else {
|
|
600
|
-
CONSOLE.nable(rows)
|
|
601
|
-
}
|
|
590
|
+
await dealStar(text)
|
|
602
591
|
} else {
|
|
603
592
|
await evalText(text)
|
|
604
593
|
}
|
|
605
594
|
} else if (str.match(/^\./)) {
|
|
606
595
|
await dealKeyword(enrichArgs(str))
|
|
607
596
|
} else if (!str.split(/\s+/)[0].match(/^\d+$/)) {
|
|
608
|
-
|
|
609
|
-
listCmd()
|
|
597
|
+
filterCmdAndList(str);
|
|
610
598
|
} else {
|
|
611
599
|
let strs = str.split(/\s+/)
|
|
612
600
|
if (_cmdMap[strs[0]]) {
|
|
@@ -617,7 +605,7 @@ async function _wrapperInput(str) {
|
|
|
617
605
|
if (firstName === 'f') {
|
|
618
606
|
await workFile(uniqueName)
|
|
619
607
|
} else if (firstName !== 'e') {
|
|
620
|
-
|
|
608
|
+
console.log(String(fs.readFileSync(path)));
|
|
621
609
|
} else {
|
|
622
610
|
await runCmd(str)
|
|
623
611
|
}
|
|
@@ -627,6 +615,143 @@ async function _wrapperInput(str) {
|
|
|
627
615
|
}
|
|
628
616
|
}
|
|
629
617
|
|
|
618
|
+
async function dealStartGlobalMode(rows, items, text) {
|
|
619
|
+
console.info('global search mode');
|
|
620
|
+
rows.push(...await dealStarCase(await evalText('return $context'), items, '$context'))
|
|
621
|
+
rows.push(...await dealStarCase(global, items, 'global'))
|
|
622
|
+
rows.push(...await dealStarCase($lib, items, '$lib'))
|
|
623
|
+
rows.push(...dealStarGlobal(items, text))
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
async function dealStar(text) {
|
|
627
|
+
if (!text) {
|
|
628
|
+
text = '$context';
|
|
629
|
+
}
|
|
630
|
+
let items = text.split(/\s+/);
|
|
631
|
+
let rows = []
|
|
632
|
+
if (_cmdMap[items[0]]) {
|
|
633
|
+
rows.push(...await dealStarCmd([], _cmdMap[items[0]], '.'))
|
|
634
|
+
} else if (items[0].indexOf("/") !== -1) {
|
|
635
|
+
await dealStartGlobalMode(rows, items, text);
|
|
636
|
+
} else {
|
|
637
|
+
try {
|
|
638
|
+
let result = await evalText(`return ${items[0]}`)
|
|
639
|
+
rows.push(...await dealStarCase(result, items.slice(1)))
|
|
640
|
+
} catch (e) {
|
|
641
|
+
await dealStartGlobalMode(rows, items, text);
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
if (rows.length === 0) {
|
|
645
|
+
console.warn("no items")
|
|
646
|
+
} else {
|
|
647
|
+
console.nable(rows)
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
function dealStarGlobal(items, text) {
|
|
652
|
+
let rows = []
|
|
653
|
+
let prefix = ''
|
|
654
|
+
let index = items[0].indexOf("/");
|
|
655
|
+
if (index !== -1) {
|
|
656
|
+
prefix = trim(items[0].substring(0, index + 1))
|
|
657
|
+
text = trim(text.substring(index + 1))
|
|
658
|
+
}
|
|
659
|
+
for (let cmd of filterCmd(prefix + '^i ,')) {
|
|
660
|
+
dealStarCmd(rows, cmd, text)
|
|
661
|
+
}
|
|
662
|
+
return rows;
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
function dealStarCmd(rows, cmd, filterStr) {
|
|
666
|
+
let content = String(fs.readFileSync(getFullPath(cmd)));
|
|
667
|
+
let capturingFunction = false;
|
|
668
|
+
let inMultiLineComment = false;
|
|
669
|
+
let potentialComments = [];
|
|
670
|
+
let functionLines = [];
|
|
671
|
+
let comments = [];
|
|
672
|
+
let fnName = '';
|
|
673
|
+
let fnType = '';
|
|
674
|
+
|
|
675
|
+
// Helper function to reset state
|
|
676
|
+
function resetState() {
|
|
677
|
+
capturingFunction = false;
|
|
678
|
+
fnName = '';
|
|
679
|
+
fnType = '';
|
|
680
|
+
functionLines = [];
|
|
681
|
+
potentialComments = [];
|
|
682
|
+
comments = [];
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
for (let line of content.split('\n')) {
|
|
686
|
+
// Handle multi-line comment start or continue
|
|
687
|
+
if (line.trim().startsWith('/*') || inMultiLineComment) {
|
|
688
|
+
inMultiLineComment = true;
|
|
689
|
+
potentialComments.push(line);
|
|
690
|
+
|
|
691
|
+
// Check for multi-line comment end
|
|
692
|
+
if (line.trim().endsWith('*/')) {
|
|
693
|
+
inMultiLineComment = false;
|
|
694
|
+
}
|
|
695
|
+
continue; // Continue to the next line
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
// Capture single-line comments
|
|
699
|
+
if (line.trim().startsWith('//')) {
|
|
700
|
+
potentialComments.push(line);
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
// Check for function start
|
|
704
|
+
if ((line.startsWith('function') || line.startsWith('async function')) && isArgsMatch(line, [filterStr])) {
|
|
705
|
+
fnName = reget(line, /function\s+(\w+)\s*\(/);
|
|
706
|
+
fnType = line.startsWith('async') ? 'AsyncFunction' : 'Function';
|
|
707
|
+
capturingFunction = true;
|
|
708
|
+
comments = [...potentialComments]; // Confirm the comments are related to the function
|
|
709
|
+
potentialComments = []; // Clear the potential comments as they are now confirmed
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
// Capture function lines
|
|
713
|
+
if (capturingFunction) {
|
|
714
|
+
functionLines.push(line);
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
// Check for function end and process
|
|
718
|
+
if (fnName && line.startsWith("}") && capturingFunction) {
|
|
719
|
+
let functionContent = functionLines.join('\n');
|
|
720
|
+
let commentContent = trimText(comments.join('\n'));
|
|
721
|
+
let row = getInfo(functionContent, fnName, fnType);
|
|
722
|
+
row.value = infoStr(cmd) + ' ' + getCmdMd5Key(parseUniqueName(cmd)[1]) + '\n' + [commentContent, row.value].filter(i => trim(i)).join("\n");
|
|
723
|
+
rows.push(row);
|
|
724
|
+
|
|
725
|
+
resetState();
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
// If not capturing function, reset potential comments
|
|
729
|
+
if (!capturingFunction && !inMultiLineComment && line.trim() !== '' && !line.trim().startsWith('//')) {
|
|
730
|
+
potentialComments = [];
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
return rows;
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
async function dealStarCase(result, matchStrs, cmd) {
|
|
737
|
+
let rows = []
|
|
738
|
+
if (!result || typeof result === 'string' || Object.keys(result).length === 0) {
|
|
739
|
+
rows.push(getInfo(result))
|
|
740
|
+
} else {
|
|
741
|
+
for (let key of Object.keys(result)) {
|
|
742
|
+
if (matchStrs.length === 0 || isArgsMatch(key, [matchStrs.join(' ')])){
|
|
743
|
+
rows.push(getInfo(result[key], key))
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
if (cmd) {
|
|
748
|
+
for (let row of rows) {
|
|
749
|
+
row.value = infoStr(cmd) + '\n' + row.value;
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
return rows;
|
|
753
|
+
}
|
|
754
|
+
|
|
630
755
|
function arrayToCmdMap(cmds) {
|
|
631
756
|
let cmdMap = {}
|
|
632
757
|
for (let i = 0; i < cmds.length; i++) {
|
|
@@ -635,8 +760,8 @@ function arrayToCmdMap(cmds) {
|
|
|
635
760
|
return cmdMap
|
|
636
761
|
}
|
|
637
762
|
|
|
638
|
-
function help() {
|
|
639
|
-
|
|
763
|
+
function help(filterFn) {
|
|
764
|
+
console.nable(Object.keys(keywordDef).filter(i => filterFn ? filterFn(i):true).map(key => {
|
|
640
765
|
let item = keywordDef[key];
|
|
641
766
|
return {
|
|
642
767
|
keyword: ' ' + warnStr(`.${key}`),
|
|
@@ -662,19 +787,23 @@ function delTipsByIndex(idxs) {
|
|
|
662
787
|
params.push(indexKeyMap[keys.length + 1 + Number(index)])
|
|
663
788
|
}
|
|
664
789
|
}
|
|
790
|
+
if (idxs.length > 0 && params.length <= 0) {
|
|
791
|
+
console.warn("no items")
|
|
792
|
+
return;
|
|
793
|
+
}
|
|
665
794
|
delTips(...params)
|
|
666
795
|
}
|
|
667
796
|
|
|
668
797
|
function copyToSpace(uniqueName) {
|
|
669
798
|
let path = getFullPath(uniqueName)
|
|
670
|
-
let text = String(
|
|
799
|
+
let text = String(fs.readFileSync(path))
|
|
671
800
|
let fileName = parseUniqueName(uniqueName)[1];
|
|
672
801
|
let targetPath = $workspaceMap[$defaultSpace] + '/' + fileName;
|
|
673
|
-
if (
|
|
674
|
-
warn(`${fileName} already exist in ${$defaultSpace}`)
|
|
802
|
+
if (fs.existsSync(targetPath)) {
|
|
803
|
+
console.warn(`${fileName} already exist in ${$defaultSpace}`)
|
|
675
804
|
} else {
|
|
676
|
-
|
|
677
|
-
info(`get ${uniqueName} to ${$defaultSpace}`)
|
|
805
|
+
fs.writeFileSync(targetPath, text)
|
|
806
|
+
console.info(`get ${uniqueName} to ${$defaultSpace}`)
|
|
678
807
|
}
|
|
679
808
|
}
|
|
680
809
|
|
|
@@ -697,7 +826,7 @@ let compareMode = {
|
|
|
697
826
|
}
|
|
698
827
|
return result
|
|
699
828
|
},
|
|
700
|
-
'ab=': (aSpace, bSpace,
|
|
829
|
+
'ab=': (aSpace, bSpace, as, bs) => {
|
|
701
830
|
let result = []
|
|
702
831
|
for (let a of as) {
|
|
703
832
|
if (bs.indexOf(a) !== -1) {
|
|
@@ -749,20 +878,10 @@ const keywordDef = {
|
|
|
749
878
|
},
|
|
750
879
|
short: 'h'
|
|
751
880
|
},
|
|
752
|
-
version: {
|
|
753
|
-
comment: 'View version',
|
|
754
|
-
exeFn: (args) => {
|
|
755
|
-
CONSOLE.log(packageJson.version)
|
|
756
|
-
},
|
|
757
|
-
short: 'v'
|
|
758
|
-
},
|
|
759
881
|
list: {
|
|
760
882
|
comment: 'View file list',
|
|
761
883
|
exeFn: (args) => {
|
|
762
|
-
|
|
763
|
-
_cmdMap = arrayToCmdMap(filterCmd(args.length > 0 ? args.join(' '):[',']))
|
|
764
|
-
}
|
|
765
|
-
listCmd()
|
|
884
|
+
filterCmdAndList(args.length > 0 ? args.join(' '):'');
|
|
766
885
|
},
|
|
767
886
|
args: {
|
|
768
887
|
queryParams: 'string matching, [,] represents all, keywords can be omitted'
|
|
@@ -779,22 +898,22 @@ const keywordDef = {
|
|
|
779
898
|
},
|
|
780
899
|
short: 'C'
|
|
781
900
|
},
|
|
782
|
-
|
|
901
|
+
view: {
|
|
783
902
|
comment: 'View file',
|
|
784
903
|
exeFn: (args) => {
|
|
785
904
|
let uniqueName = _cmdMap[args[0]]
|
|
786
905
|
if (!uniqueName) {
|
|
787
|
-
warn("no items")
|
|
906
|
+
console.warn("no items")
|
|
788
907
|
} else {
|
|
789
908
|
let path = getFullPath(uniqueName)
|
|
790
|
-
let sourceStr = String(
|
|
791
|
-
|
|
909
|
+
let sourceStr = String(fs.readFileSync(path))
|
|
910
|
+
console.log(sourceStr)
|
|
792
911
|
}
|
|
793
912
|
},
|
|
794
913
|
args: {
|
|
795
914
|
fileIndex: 'File index'
|
|
796
915
|
},
|
|
797
|
-
short: '
|
|
916
|
+
short: 'v'
|
|
798
917
|
},
|
|
799
918
|
rm: {
|
|
800
919
|
comment: 'Remove file',
|
|
@@ -803,15 +922,15 @@ const keywordDef = {
|
|
|
803
922
|
if (args[0] === 'ALL') {
|
|
804
923
|
for (let value of Object.values(_cmdMap)) {
|
|
805
924
|
let path = getFullPath(value)
|
|
806
|
-
|
|
807
|
-
info(`${path} removed`)
|
|
925
|
+
fs.unlinkSync(path)
|
|
926
|
+
console.info(`${path} removed`)
|
|
808
927
|
}
|
|
809
928
|
} else if (!uniqueName) {
|
|
810
|
-
warn("no items")
|
|
929
|
+
console.warn("no items")
|
|
811
930
|
} else {
|
|
812
931
|
let path = getFullPath(uniqueName)
|
|
813
|
-
|
|
814
|
-
info(`${path} removed`)
|
|
932
|
+
fs.unlinkSync(path)
|
|
933
|
+
console.info(`${path} removed`)
|
|
815
934
|
}
|
|
816
935
|
},
|
|
817
936
|
args: {
|
|
@@ -828,7 +947,7 @@ const keywordDef = {
|
|
|
828
947
|
copyToSpace(value)
|
|
829
948
|
}
|
|
830
949
|
} else if (!uniqueName) {
|
|
831
|
-
warn("no items")
|
|
950
|
+
console.warn("no items")
|
|
832
951
|
} else {
|
|
833
952
|
copyToSpace(uniqueName)
|
|
834
953
|
}
|
|
@@ -843,7 +962,7 @@ const keywordDef = {
|
|
|
843
962
|
exeFn: (args) => {
|
|
844
963
|
let uniqueName = _cmdMap[args[0]]
|
|
845
964
|
if (!uniqueName) {
|
|
846
|
-
warn("no items")
|
|
965
|
+
console.warn("no items")
|
|
847
966
|
} else {
|
|
848
967
|
let pair = parseUniqueName(uniqueName)
|
|
849
968
|
let newName =trim(args.slice(1).join(" "))
|
|
@@ -861,10 +980,10 @@ const keywordDef = {
|
|
|
861
980
|
exeFn: async (args) => {
|
|
862
981
|
let uniqueName = _cmdMap[args[0]]
|
|
863
982
|
if (!uniqueName) {
|
|
864
|
-
warn("no items")
|
|
983
|
+
console.warn("no items")
|
|
865
984
|
} else {
|
|
866
985
|
let path = getFullPath(uniqueName)
|
|
867
|
-
|
|
986
|
+
ei(getEditor(), [path])
|
|
868
987
|
}
|
|
869
988
|
},
|
|
870
989
|
args: {
|
|
@@ -872,6 +991,29 @@ const keywordDef = {
|
|
|
872
991
|
},
|
|
873
992
|
short: 'e'
|
|
874
993
|
},
|
|
994
|
+
setting: {
|
|
995
|
+
comment: 'Global settings',
|
|
996
|
+
exeFn: async (args) => {
|
|
997
|
+
ei(getEditor(), [`${getLibDataDir()}/config.json`])
|
|
998
|
+
},
|
|
999
|
+
short: 's'
|
|
1000
|
+
},
|
|
1001
|
+
open: {
|
|
1002
|
+
comment: 'open file',
|
|
1003
|
+
exeFn: async (args) => {
|
|
1004
|
+
let uniqueName = _cmdMap[args[0]]
|
|
1005
|
+
if (!uniqueName) {
|
|
1006
|
+
console.warn("no items")
|
|
1007
|
+
} else {
|
|
1008
|
+
let path = getFullPath(uniqueName)
|
|
1009
|
+
await e(`"${getFileOpenExe(parseUniqueName(uniqueName)[1])}" "${path}"`)
|
|
1010
|
+
}
|
|
1011
|
+
},
|
|
1012
|
+
args: {
|
|
1013
|
+
fileIndex: 'File index'
|
|
1014
|
+
},
|
|
1015
|
+
short: 'o'
|
|
1016
|
+
},
|
|
875
1017
|
compare: {
|
|
876
1018
|
comment: 'Compare files',
|
|
877
1019
|
exeFn: async (args) => {
|
|
@@ -881,27 +1023,30 @@ const keywordDef = {
|
|
|
881
1023
|
let bSpace = args[1]
|
|
882
1024
|
|
|
883
1025
|
if (aUniqueName && bUniqueName) {
|
|
884
|
-
await e(`${getConfig("compareExe", "
|
|
1026
|
+
let result = await e(`${getConfig("compareExe", "diff")} "${getFullPath(aUniqueName)}" "${getFullPath(bUniqueName)}"`)
|
|
1027
|
+
if (result) {
|
|
1028
|
+
console.log(result)
|
|
1029
|
+
}
|
|
885
1030
|
} else if (aSpace && bSpace) {
|
|
886
1031
|
if (!args[2]) {
|
|
887
|
-
warn('require mode')
|
|
1032
|
+
console.warn('require mode')
|
|
888
1033
|
return
|
|
889
1034
|
}
|
|
890
1035
|
|
|
891
1036
|
let mode = args[2]
|
|
892
1037
|
if (Object.keys(compareMode).indexOf(mode) === -1) {
|
|
893
|
-
warn('invalid mode')
|
|
1038
|
+
console.warn('invalid mode')
|
|
894
1039
|
return;
|
|
895
1040
|
}
|
|
896
1041
|
|
|
897
|
-
let aFiles =
|
|
898
|
-
let bFiles =
|
|
1042
|
+
let aFiles = fs.readdirSync($workspaceMap[aSpace]).filter(isJsirFileName)
|
|
1043
|
+
let bFiles = fs.readdirSync($workspaceMap[bSpace]).filter(isJsirFileName)
|
|
899
1044
|
|
|
900
1045
|
let result = compareMode[mode](aSpace, bSpace, aFiles, bFiles);
|
|
901
|
-
|
|
1046
|
+
resetCmdMap(arrayToCmdMap(result))
|
|
902
1047
|
listCmd()
|
|
903
1048
|
} else {
|
|
904
|
-
warn('invalid args')
|
|
1049
|
+
console.warn('invalid args')
|
|
905
1050
|
}
|
|
906
1051
|
},
|
|
907
1052
|
args: {
|
|
@@ -916,23 +1061,38 @@ const keywordDef = {
|
|
|
916
1061
|
exeFn: (args) => {
|
|
917
1062
|
let uniqueName = _cmdMap[args[0]]
|
|
918
1063
|
if (!uniqueName) {
|
|
919
|
-
warn("no items")
|
|
1064
|
+
console.warn("no items")
|
|
920
1065
|
} else {
|
|
921
1066
|
let cmds = getScriptRequires(uniqueName);
|
|
922
|
-
|
|
1067
|
+
resetCmdMap(arrayToCmdMap([uniqueName, ...cmds]))
|
|
923
1068
|
listCmd()
|
|
924
1069
|
|
|
1070
|
+
let packages = []
|
|
925
1071
|
for (let un of [uniqueName, ...cmds]) {
|
|
926
1072
|
let path = getFullPath(un);
|
|
927
|
-
let text = removeComment(String(
|
|
1073
|
+
let text = removeComment(String(fs.readFileSync(path)))
|
|
928
1074
|
|
|
929
|
-
preLoad(text, "[\\s=;]require", requireG)
|
|
930
|
-
preLoad(text, "^require", requireG)
|
|
931
|
-
|
|
932
|
-
preLoad(text, "
|
|
933
|
-
preLoad(text, "
|
|
934
|
-
preLoad(text, "^import", importG);
|
|
1075
|
+
preLoad(text, "[\\s=;]require", requireG, packages)
|
|
1076
|
+
preLoad(text, "^require", requireG, packages)
|
|
1077
|
+
preLoad(text, "\\$import", importG, packages)
|
|
1078
|
+
preLoad(text, "[\\s=;]import", importG, packages)
|
|
1079
|
+
preLoad(text, "^import", importG, packages)
|
|
935
1080
|
}
|
|
1081
|
+
let moduleDir = $workspaceMap[global.$defaultSpace] + '/node_modules'
|
|
1082
|
+
let deps = {}
|
|
1083
|
+
packages.forEach(name => {
|
|
1084
|
+
if (deps[name]) {
|
|
1085
|
+
return;
|
|
1086
|
+
}
|
|
1087
|
+
let dir = moduleDir + "/" + name;
|
|
1088
|
+
if (fs.existsSync(dir) && fs.existsSync(dir + '/package.json')) {
|
|
1089
|
+
deps[name] = require(dir + '/package.json').version;
|
|
1090
|
+
} else {
|
|
1091
|
+
deps[name] = 'Unknown';
|
|
1092
|
+
}
|
|
1093
|
+
})
|
|
1094
|
+
console.info("package version")
|
|
1095
|
+
console.log(JSON.stringify(deps, null, 2))
|
|
936
1096
|
}
|
|
937
1097
|
},
|
|
938
1098
|
args: {
|
|
@@ -948,7 +1108,7 @@ const keywordDef = {
|
|
|
948
1108
|
} else {
|
|
949
1109
|
let name = _cmdMap[args[0]]
|
|
950
1110
|
if (!name) {
|
|
951
|
-
warn("no items")
|
|
1111
|
+
console.warn("no items")
|
|
952
1112
|
} else {
|
|
953
1113
|
await watchFile(name)
|
|
954
1114
|
}
|
|
@@ -964,73 +1124,34 @@ const keywordDef = {
|
|
|
964
1124
|
exeFn: async (args) => {
|
|
965
1125
|
let newWorkspace = args.join(' ')
|
|
966
1126
|
if (newWorkspace) {
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
1127
|
+
if ($workspaceMap[newWorkspace]) {
|
|
1128
|
+
initWorkspace(newWorkspace)
|
|
1129
|
+
} else if (newWorkspace.startsWith('-')) {
|
|
1130
|
+
let name = newWorkspace.replace(/^-\s*/, '');
|
|
1131
|
+
arrayDataFile(_workspaceConfigFile, arr => arr.filter(path => name !== getSpaceFromDir(path)))
|
|
1132
|
+
} else if (newWorkspace.startsWith("/")) {
|
|
1133
|
+
let workspaces = Object.values($workspaceMap)
|
|
1134
|
+
workspaces.push(newWorkspace)
|
|
1135
|
+
arrayDataFile(_workspaceConfigFile, () => workspaces)
|
|
1136
|
+
} else {
|
|
1137
|
+
console.warn('invalid args')
|
|
1138
|
+
}
|
|
970
1139
|
}
|
|
971
1140
|
checkWorkspaces()
|
|
972
1141
|
let workspaces = Object.values($workspaceMap)
|
|
973
1142
|
let items = workspaces.map((path, index) => {
|
|
974
1143
|
return {
|
|
975
|
-
|
|
976
|
-
name: getSpaceFromDir(path),
|
|
1144
|
+
name: (getSpaceFromDir(path) === $defaultSpace ? '*':' ') + getSpaceFromDir(path),
|
|
977
1145
|
path
|
|
978
1146
|
}
|
|
979
1147
|
})
|
|
980
|
-
|
|
1148
|
+
console.table(items)
|
|
981
1149
|
},
|
|
982
1150
|
args: {
|
|
983
1151
|
workspacePath: 'New workspace path'
|
|
984
1152
|
},
|
|
985
1153
|
short: 'w'
|
|
986
1154
|
},
|
|
987
|
-
uninstall: {
|
|
988
|
-
comment: 'Uninstall workspace',
|
|
989
|
-
exeFn: async (args) => {
|
|
990
|
-
let workspaces = Object.values($workspaceMap)
|
|
991
|
-
let items = workspaces.map((path, index) => {
|
|
992
|
-
return {
|
|
993
|
-
index: ' ' + (index + 1),
|
|
994
|
-
name: getSpaceFromDir(path),
|
|
995
|
-
path
|
|
996
|
-
}
|
|
997
|
-
})
|
|
998
|
-
CONSOLE.table(items)
|
|
999
|
-
let index = await nextLine(line => line, "index: ")
|
|
1000
|
-
let workspace = workspaces[index - 1]
|
|
1001
|
-
if (workspace) {
|
|
1002
|
-
workspaces = workspaces.filter(i => i !== workspace);
|
|
1003
|
-
arrayDataFile(_workspaceConfigFile, () => workspaces);
|
|
1004
|
-
checkWorkspaces()
|
|
1005
|
-
} else {
|
|
1006
|
-
warn('invalid args')
|
|
1007
|
-
}
|
|
1008
|
-
},
|
|
1009
|
-
short: 'u'
|
|
1010
|
-
},
|
|
1011
|
-
switch: {
|
|
1012
|
-
comment: 'Switch workspace',
|
|
1013
|
-
exeFn: async (args) => {
|
|
1014
|
-
checkWorkspaces()
|
|
1015
|
-
let workspaces = Object.values($workspaceMap)
|
|
1016
|
-
let items = workspaces.map((path, index) => {
|
|
1017
|
-
return {
|
|
1018
|
-
index: ' ' + (index + 1),
|
|
1019
|
-
name: getSpaceFromDir(path),
|
|
1020
|
-
path
|
|
1021
|
-
}
|
|
1022
|
-
})
|
|
1023
|
-
CONSOLE.table(items)
|
|
1024
|
-
let index = trim(await nextLine(line => line, "switch workspace: "));
|
|
1025
|
-
if (index && workspaces[index - 1]) {
|
|
1026
|
-
let workspace = workspaces[index - 1];
|
|
1027
|
-
initWorkspace(getSpaceFromDir(workspace))
|
|
1028
|
-
} else {
|
|
1029
|
-
warn('no items')
|
|
1030
|
-
}
|
|
1031
|
-
},
|
|
1032
|
-
short: 'S'
|
|
1033
|
-
},
|
|
1034
1155
|
add: {
|
|
1035
1156
|
comment: 'Add file',
|
|
1036
1157
|
exeFn: async (args) => {
|
|
@@ -1045,7 +1166,7 @@ const keywordDef = {
|
|
|
1045
1166
|
comment: 'Quick execution',
|
|
1046
1167
|
exeFn: async (args) => {
|
|
1047
1168
|
if (!args[0]) {
|
|
1048
|
-
warn('invalid args')
|
|
1169
|
+
console.warn('invalid args')
|
|
1049
1170
|
return
|
|
1050
1171
|
}
|
|
1051
1172
|
let keyword = args[0]
|
|
@@ -1053,13 +1174,20 @@ const keywordDef = {
|
|
|
1053
1174
|
if (keyword.endsWith('.')) {
|
|
1054
1175
|
let _tmp = trim(keyword.substring(0, keyword.length - 1));
|
|
1055
1176
|
if (_tmp) {
|
|
1177
|
+
if (keywordDef[_tmp] || Object.values(keywordDef).map(item => item.short).indexOf(_tmp) !== -1) {
|
|
1178
|
+
help(i => i === _tmp || keywordDef[i].short === _tmp);
|
|
1179
|
+
return;
|
|
1180
|
+
}
|
|
1056
1181
|
justList = true;
|
|
1057
1182
|
keyword = _tmp;
|
|
1183
|
+
} else {
|
|
1184
|
+
console.warn('invalid keyword')
|
|
1185
|
+
return
|
|
1058
1186
|
}
|
|
1059
1187
|
}
|
|
1060
1188
|
let pair = parseUniqueName(keyword)
|
|
1061
1189
|
let cmds;
|
|
1062
|
-
if (
|
|
1190
|
+
if (isMd5Key(pair[1]) || pair[1].indexOf(",") !== -1) {
|
|
1063
1191
|
cmds = getQuickRunCmds(keyword, keyword)
|
|
1064
1192
|
} else {
|
|
1065
1193
|
cmds = getQuickRunCmds(keyword, '^e ' + toJsirFileName(pair[1]) + '$')
|
|
@@ -1071,10 +1199,10 @@ const keywordDef = {
|
|
|
1071
1199
|
if (justList) {
|
|
1072
1200
|
listCmd(arrayToCmdMap(cmds))
|
|
1073
1201
|
} else {
|
|
1074
|
-
await
|
|
1202
|
+
await runScript(cmds[0], args.slice(1))
|
|
1075
1203
|
}
|
|
1076
1204
|
} else {
|
|
1077
|
-
warn(cmds.length > 1 ? "multiple match" : "no match")
|
|
1205
|
+
console.warn(cmds.length > 1 ? "multiple match" : "no match")
|
|
1078
1206
|
}
|
|
1079
1207
|
},
|
|
1080
1208
|
args: {
|
|
@@ -1086,8 +1214,8 @@ const keywordDef = {
|
|
|
1086
1214
|
comment: 'Exit',
|
|
1087
1215
|
exeFn: (args) => {
|
|
1088
1216
|
delTips();
|
|
1089
|
-
|
|
1090
|
-
|
|
1217
|
+
console.log(infoStr("Bye!"));
|
|
1218
|
+
_noAppendNextLine = true;
|
|
1091
1219
|
},
|
|
1092
1220
|
short: 'q'
|
|
1093
1221
|
},
|
|
@@ -1095,10 +1223,24 @@ const keywordDef = {
|
|
|
1095
1223
|
comment: 'Interactive interface',
|
|
1096
1224
|
exeFn: (args) => {
|
|
1097
1225
|
_noAppendNextLine = false
|
|
1098
|
-
|
|
1099
|
-
|
|
1226
|
+
resetCmdMap()
|
|
1227
|
+
console.log(warnStr(`(${setting.name} ${packageJson.version}) You can start with .help, use * to expand context.`))
|
|
1100
1228
|
},
|
|
1101
1229
|
short: 'p'
|
|
1230
|
+
},
|
|
1231
|
+
npm: {
|
|
1232
|
+
comment: 'Node npm',
|
|
1233
|
+
exeFn: (args) => {
|
|
1234
|
+
let packageFile = $workspaceMap[global.$defaultSpace] + '/package.json';
|
|
1235
|
+
if(!fs.existsSync(packageFile)) {
|
|
1236
|
+
let jsonObj = {
|
|
1237
|
+
"dependencies": {}
|
|
1238
|
+
}
|
|
1239
|
+
fs.writeFileSync(packageFile, JSON.stringify(jsonObj, null, 2));
|
|
1240
|
+
}
|
|
1241
|
+
ei(`cd "${$workspaceMap[global.$defaultSpace]}";npm ${args.join(' ')}`, [], true)
|
|
1242
|
+
},
|
|
1243
|
+
short: 'N'
|
|
1102
1244
|
}
|
|
1103
1245
|
}
|
|
1104
1246
|
|
|
@@ -1112,18 +1254,28 @@ function getQuickRunCmds(input, matchStr) {
|
|
|
1112
1254
|
return filterCmd(matchStr).filter(i => parseUniqueName(i)[1].startsWith("e "))
|
|
1113
1255
|
}
|
|
1114
1256
|
|
|
1115
|
-
function preLoad(text, fnNameMatch, fn) {
|
|
1257
|
+
function preLoad(text, fnNameMatch, fn, packages = []) {
|
|
1116
1258
|
regEach(text, new RegExp(`${fnNameMatch}\\s*\\(([^()]+)\\)`, 'g'), r=> {
|
|
1117
1259
|
let item = trim(r[1])
|
|
1118
1260
|
if (item) {
|
|
1119
1261
|
try {
|
|
1120
|
-
let
|
|
1262
|
+
let packageName = evalVal(item);
|
|
1263
|
+
if (packages.indexOf(packageName) !== -1) {
|
|
1264
|
+
return;
|
|
1265
|
+
}
|
|
1266
|
+
packages.push(packageName)
|
|
1267
|
+
let result = fn(packageName)
|
|
1121
1268
|
if (getType(result) === 'Promise') {
|
|
1122
|
-
result.catch(e => {
|
|
1269
|
+
result.catch(e => {
|
|
1270
|
+
$log(e)
|
|
1271
|
+
})
|
|
1123
1272
|
}
|
|
1124
|
-
} catch (e) {
|
|
1273
|
+
} catch (e) {
|
|
1274
|
+
$log(e)
|
|
1275
|
+
}
|
|
1125
1276
|
}
|
|
1126
1277
|
});
|
|
1278
|
+
return packages;
|
|
1127
1279
|
}
|
|
1128
1280
|
|
|
1129
1281
|
async function dealKeyword(items) {
|
|
@@ -1139,7 +1291,7 @@ async function dealKeyword(items) {
|
|
|
1139
1291
|
continue
|
|
1140
1292
|
}
|
|
1141
1293
|
if (keyword) {
|
|
1142
|
-
args.push(
|
|
1294
|
+
args.push(item)
|
|
1143
1295
|
}
|
|
1144
1296
|
}
|
|
1145
1297
|
if (keyword) {
|
|
@@ -1158,7 +1310,11 @@ async function _dealKeyword(keyword, args) {
|
|
|
1158
1310
|
let shortKey = keyword === key ? item.short : keyword;
|
|
1159
1311
|
if (item.short === shortKey) {
|
|
1160
1312
|
unMatched = false;
|
|
1161
|
-
|
|
1313
|
+
if (item === keywordDef.run) {
|
|
1314
|
+
await item.exeFn(args);
|
|
1315
|
+
} else {
|
|
1316
|
+
await item.exeFn(args.map(trim));
|
|
1317
|
+
}
|
|
1162
1318
|
break;
|
|
1163
1319
|
}
|
|
1164
1320
|
}
|
|
@@ -1192,7 +1348,7 @@ function removeComment(text) {
|
|
|
1192
1348
|
|
|
1193
1349
|
function _getScriptRequires(uniqueName, cmds, links) {
|
|
1194
1350
|
let path = getFullPath(uniqueName);
|
|
1195
|
-
let text = removeComment(String(
|
|
1351
|
+
let text = removeComment(String(fs.readFileSync(path)))
|
|
1196
1352
|
let temp = []
|
|
1197
1353
|
regEach(text, /\$require\s*\(([^()]+)\)/g, r=> {
|
|
1198
1354
|
if (r[1]) {
|
|
@@ -1242,8 +1398,8 @@ function getComments(i, cmdName, text, cols = [], col) {
|
|
|
1242
1398
|
let docLines = [getCmdMd5Key(parseUniqueName(cmdName)[1]) + " - " + i]
|
|
1243
1399
|
text = trim(text)
|
|
1244
1400
|
docLines.push(...getTextComments(text))
|
|
1245
|
-
let argDef = getArgDef(text)
|
|
1246
|
-
let comments = getArgComments(argDef)
|
|
1401
|
+
let argDef = getArgDef(text, cmdName)
|
|
1402
|
+
let comments = getArgComments(argDef, true)
|
|
1247
1403
|
if (comments) {
|
|
1248
1404
|
docLines.push(...comments)
|
|
1249
1405
|
}
|
|
@@ -1281,7 +1437,7 @@ function getFullPath(name) {
|
|
|
1281
1437
|
function toUniqueName(name, space) {
|
|
1282
1438
|
name = trim(name)
|
|
1283
1439
|
if (!name) {
|
|
1284
|
-
throw `invalid name`
|
|
1440
|
+
throw `invalid name '${name}'`
|
|
1285
1441
|
}
|
|
1286
1442
|
let uniqueName
|
|
1287
1443
|
if (name.startsWith('/')) {
|
|
@@ -1299,14 +1455,14 @@ function toUniqueName(name, space) {
|
|
|
1299
1455
|
function toJsirFileName(name) {
|
|
1300
1456
|
name = trim(name)
|
|
1301
1457
|
if (!name) {
|
|
1302
|
-
throw `invalid name`
|
|
1458
|
+
throw `invalid name '${name}'`
|
|
1303
1459
|
}
|
|
1304
1460
|
if (isJsirFileName(name)) {
|
|
1305
1461
|
return name;
|
|
1306
1462
|
}
|
|
1307
1463
|
name = name + '.js'
|
|
1308
1464
|
if (!isJsirFileName(name)) {
|
|
1309
|
-
throw `invalid name`
|
|
1465
|
+
throw `invalid name '${name}'`
|
|
1310
1466
|
}
|
|
1311
1467
|
return name
|
|
1312
1468
|
}
|
|
@@ -1343,15 +1499,32 @@ function initWorkspace(space) {
|
|
|
1343
1499
|
global.$defaultSpace = workspace;
|
|
1344
1500
|
}
|
|
1345
1501
|
|
|
1502
|
+
function filterCmdAndList(arg) {
|
|
1503
|
+
arg = trim(arg) || lastFilterArg;
|
|
1504
|
+
if (arg) {
|
|
1505
|
+
if (lastFilterArg !== arg) {
|
|
1506
|
+
lastFilterArg = arg;
|
|
1507
|
+
if (_noAppendNextLine) {
|
|
1508
|
+
setConfig("lastFilterArg", lastFilterArg)
|
|
1509
|
+
}
|
|
1510
|
+
}
|
|
1511
|
+
_cmdMap = arrayToCmdMap(filterCmd(arg));
|
|
1512
|
+
}
|
|
1513
|
+
listCmd();
|
|
1514
|
+
}
|
|
1515
|
+
|
|
1346
1516
|
function filterCmd(arg){
|
|
1517
|
+
let cmds = []
|
|
1347
1518
|
arg = trim(arg)
|
|
1519
|
+
if (!arg) {
|
|
1520
|
+
return cmds;
|
|
1521
|
+
}
|
|
1348
1522
|
let spaceName
|
|
1349
1523
|
if (arg.indexOf('/') !== -1) {
|
|
1350
1524
|
let index = arg.indexOf('/');
|
|
1351
1525
|
spaceName = trim(arg.substring(0, index)) || $defaultSpace
|
|
1352
1526
|
arg = trim(arg.substring(index + 1))
|
|
1353
1527
|
}
|
|
1354
|
-
let cmds = []
|
|
1355
1528
|
for (let workspace of Object.values($workspaceMap)) {
|
|
1356
1529
|
let spaceTmp = getSpaceFromDir(workspace);
|
|
1357
1530
|
if (!spaceName || spaceName === spaceTmp) {
|
|
@@ -1363,7 +1536,7 @@ function filterCmd(arg){
|
|
|
1363
1536
|
|
|
1364
1537
|
function _filterCmd(dirPath, arg) {
|
|
1365
1538
|
let cmdMap = {}
|
|
1366
|
-
let files =
|
|
1539
|
+
let files = fs.readdirSync(dirPath).filter(isJsirFileName)
|
|
1367
1540
|
for (let file of files) {
|
|
1368
1541
|
file = trim(file)
|
|
1369
1542
|
let uniqueName = toUniqueName(dirPath + '/' + file)
|
|
@@ -1384,13 +1557,15 @@ function parseUniqueName(uniqueName) {
|
|
|
1384
1557
|
return [pair[len - 2], pair[len - 1]]
|
|
1385
1558
|
}
|
|
1386
1559
|
|
|
1387
|
-
function getArgComments(argDef) {
|
|
1560
|
+
function getArgComments(argDef, showShort = false) {
|
|
1388
1561
|
let comments = []
|
|
1389
1562
|
let keys = Object.keys(argDef)
|
|
1390
1563
|
let shortMap = {}
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1564
|
+
if (showShort) {
|
|
1565
|
+
let tempShortMap = getShortDefMap(keys)
|
|
1566
|
+
for (let key of Object.keys(tempShortMap)) {
|
|
1567
|
+
shortMap[tempShortMap[key].replace(/^:/, '')] = key;
|
|
1568
|
+
}
|
|
1394
1569
|
}
|
|
1395
1570
|
if (keys.length > 0) {
|
|
1396
1571
|
for (let k of keys) {
|
|
@@ -1425,6 +1600,9 @@ function getShortDefMap(argDefKeys) {
|
|
|
1425
1600
|
for (let str of argDefKeys) {
|
|
1426
1601
|
let originStr = ":" + str;
|
|
1427
1602
|
str = str.replace(/^:/, '')
|
|
1603
|
+
if (!str.startsWith("_")) {
|
|
1604
|
+
continue;
|
|
1605
|
+
}
|
|
1428
1606
|
str = str.replace(/^_/, '')
|
|
1429
1607
|
let shortStr = getShortStr(str);
|
|
1430
1608
|
let key = '-' + shortStr;
|
|
@@ -1449,7 +1627,7 @@ async function runCmd(str = '', uniqueName = '', text = '') {
|
|
|
1449
1627
|
let strs = str.split(/\s+/)
|
|
1450
1628
|
if (!uniqueName) {
|
|
1451
1629
|
if (!_cmdMap[strs[0]] && strs[0] !== '0') {
|
|
1452
|
-
warn('no items')
|
|
1630
|
+
console.warn('no items')
|
|
1453
1631
|
return
|
|
1454
1632
|
}
|
|
1455
1633
|
if (_cmdMap[strs[0]]) {
|
|
@@ -1459,7 +1637,7 @@ async function runCmd(str = '', uniqueName = '', text = '') {
|
|
|
1459
1637
|
|
|
1460
1638
|
if (uniqueName) {
|
|
1461
1639
|
path = getFullPath(uniqueName);
|
|
1462
|
-
text = String(
|
|
1640
|
+
text = String(fs.readFileSync(path))
|
|
1463
1641
|
} else {
|
|
1464
1642
|
text = await getCbText()
|
|
1465
1643
|
}
|
|
@@ -1469,11 +1647,27 @@ async function runCmd(str = '', uniqueName = '', text = '') {
|
|
|
1469
1647
|
}
|
|
1470
1648
|
|
|
1471
1649
|
let argDef = getArgDef(text)
|
|
1472
|
-
let shortDefMap = getShortDefMap(Object.keys(argDef))
|
|
1473
|
-
let oriArgs = enrichArgs(str).map(i => shortDefMap[i] ? shortDefMap[i]:i)
|
|
1474
1650
|
if (argDef['[ParseError]']) {
|
|
1475
1651
|
throw `argDef [ParseError] ${argDef['[ParseError]']}`
|
|
1476
1652
|
}
|
|
1653
|
+
|
|
1654
|
+
let scriptArgs = await getScriptArgs(argDef, enrichArgs(str.replace(/^\d+\s*/, '')));
|
|
1655
|
+
process.argv = [process.argv[0], path, ...(scriptArgs.map(String))]
|
|
1656
|
+
return await evalText(text, uniqueName, scriptArgs)
|
|
1657
|
+
}
|
|
1658
|
+
|
|
1659
|
+
async function runScript(uniqueName, oriArgs) {
|
|
1660
|
+
let path = getFullPath(uniqueName);
|
|
1661
|
+
let text = String(fs.readFileSync(path))
|
|
1662
|
+
let argDef = getArgDef(text)
|
|
1663
|
+
let scriptArgs = await getScriptArgs(argDef, oriArgs);
|
|
1664
|
+
process.argv = [process.argv[0], path, ...(scriptArgs.map(String))]
|
|
1665
|
+
return await evalText(text, uniqueName, scriptArgs)
|
|
1666
|
+
}
|
|
1667
|
+
|
|
1668
|
+
async function getScriptArgs(argDef, oriArgs) {
|
|
1669
|
+
let shortDefMap = getShortDefMap(Object.keys(argDef))
|
|
1670
|
+
oriArgs = oriArgs.map(i => shortDefMap[i] ? shortDefMap[i]:i)
|
|
1477
1671
|
let argNames = Object.keys(argDef)
|
|
1478
1672
|
let exactArgs = {}
|
|
1479
1673
|
let scriptArgs = []
|
|
@@ -1481,9 +1675,12 @@ async function runCmd(str = '', uniqueName = '', text = '') {
|
|
|
1481
1675
|
let arg = oriArgs[i]
|
|
1482
1676
|
let needTrans
|
|
1483
1677
|
let pair
|
|
1678
|
+
if (arg.startsWith("-") && !arg.endsWith(' ')) {
|
|
1679
|
+
throw 'invalid argName ' + arg
|
|
1680
|
+
}
|
|
1484
1681
|
if (arg.startsWith(":") || arg.startsWith("_") && !arg.endsWith(' ')) {
|
|
1485
1682
|
arg = arg.replace(/^:/, '')
|
|
1486
|
-
if (oriArgs[i+1] && !(oriArgs[i+1].startsWith(":") || oriArgs[i+1].startsWith("_"))) {
|
|
1683
|
+
if (oriArgs[i+1] && (oriArgs[i+1].endsWith(' ') || !(oriArgs[i+1].startsWith(":") || oriArgs[i+1].startsWith("_") || oriArgs[i+1].startsWith("-")))) {
|
|
1487
1684
|
pair = [arg, oriArgs[i+1]]
|
|
1488
1685
|
i++
|
|
1489
1686
|
} else {
|
|
@@ -1533,22 +1730,19 @@ async function runCmd(str = '', uniqueName = '', text = '') {
|
|
|
1533
1730
|
}
|
|
1534
1731
|
if (scriptArgKeys.indexOf(name) === -1 || (reg && !reg.test(scriptArgs[name]))) {
|
|
1535
1732
|
argAbsent = true
|
|
1536
|
-
warn(`require ${warnStr(name)}` + (defStr ? ` <${defStr}>`:''))
|
|
1733
|
+
console.warn(`require ${warnStr(name)}` + (defStr ? ` <${defStr}>`:''))
|
|
1537
1734
|
}
|
|
1538
1735
|
}
|
|
1539
1736
|
if (argAbsent) {
|
|
1540
1737
|
throw 'invalid args';
|
|
1541
1738
|
}
|
|
1542
|
-
|
|
1543
|
-
process.argv = [process.argv[0], path, ...(scriptArgs.map(String))]
|
|
1544
|
-
return await evalText(text, uniqueName, scriptArgs)
|
|
1739
|
+
return scriptArgs;
|
|
1545
1740
|
}
|
|
1546
1741
|
|
|
1547
1742
|
// 使用空格作为可执行的标志
|
|
1548
1743
|
function enrichArgs(str) {
|
|
1549
1744
|
let args = []
|
|
1550
1745
|
let temp = []
|
|
1551
|
-
str = str.replace(/^\d+\s*/, '')
|
|
1552
1746
|
regEach(str, /'([^']*)'|"([^"]*)"|`([^`]*)`/g, item => {
|
|
1553
1747
|
if (item[1] !== null && item[1] !== undefined) {
|
|
1554
1748
|
temp.push(trim(item[1]) + ' ')
|
|
@@ -1576,11 +1770,39 @@ function enrichArgs(str) {
|
|
|
1576
1770
|
return args
|
|
1577
1771
|
}
|
|
1578
1772
|
|
|
1579
|
-
function
|
|
1580
|
-
let
|
|
1773
|
+
function parseArgDef(inputString) {
|
|
1774
|
+
let result = {}
|
|
1775
|
+
let preStr = trim(reget(inputString, /\{([\s\S]*?)}\s*=\s*\$defArgs\s*\(/))
|
|
1776
|
+
if (!preStr) {
|
|
1777
|
+
return result;
|
|
1778
|
+
}
|
|
1779
|
+
let ss = preStr.split(/[,\n]|(?<!\/)\/(?=\/)/g).map(trim).filter(i => i);
|
|
1780
|
+
for (let i = 0; i < ss.length; i++) {
|
|
1781
|
+
let s1 = trim(ss[i]);
|
|
1782
|
+
if (s1.startsWith("/")) {
|
|
1783
|
+
continue;
|
|
1784
|
+
}
|
|
1785
|
+
if (ss[i+1] && ss[i+1].startsWith("/")) {
|
|
1786
|
+
result[s1] = trim(ss[i+1].replace(/^\//, ""));
|
|
1787
|
+
i++;
|
|
1788
|
+
} else {
|
|
1789
|
+
result[s1] = '';
|
|
1790
|
+
}
|
|
1791
|
+
}
|
|
1792
|
+
return result;
|
|
1793
|
+
}
|
|
1794
|
+
|
|
1795
|
+
function getArgDef(text, uniqueName) {
|
|
1796
|
+
if (uniqueName && !parseUniqueName(uniqueName)[1].startsWith("e ")) {
|
|
1797
|
+
return {}
|
|
1798
|
+
}
|
|
1799
|
+
if (text.indexOf("$defArgs") === -1) {
|
|
1800
|
+
return {}
|
|
1801
|
+
}
|
|
1802
|
+
let exeStr = trim(reget(text, /\$defArgs\s*\(\s*(\{[\s\S]*?})\s*\)/))
|
|
1581
1803
|
let argDef = {}
|
|
1582
1804
|
try {
|
|
1583
|
-
argDef = (exeStr ? evalVal(exeStr) :
|
|
1805
|
+
argDef = (exeStr ? evalVal(exeStr) : parseArgDef(text)) || {}
|
|
1584
1806
|
let array = []
|
|
1585
1807
|
for (let key of Object.keys(argDef)) {
|
|
1586
1808
|
if (/\s/.test(key) || array.hasOwnProperty(key)) {
|
|
@@ -1642,7 +1864,7 @@ function tipsOnRmCallback(key) {
|
|
|
1642
1864
|
callback()
|
|
1643
1865
|
} catch (e) {
|
|
1644
1866
|
$log(errorStack(e))
|
|
1645
|
-
error(`[${key}] OnRmCallback: ${String(e)}`)
|
|
1867
|
+
console.error(`[${key}] OnRmCallback: ${String(e)}`)
|
|
1646
1868
|
}
|
|
1647
1869
|
}
|
|
1648
1870
|
}
|
|
@@ -1652,6 +1874,10 @@ function getCmdMd5Key(str) {
|
|
|
1652
1874
|
return '0x' + md5(str).substr(0, 8);
|
|
1653
1875
|
}
|
|
1654
1876
|
|
|
1877
|
+
function isMd5Key(str) {
|
|
1878
|
+
return /^0x[a-z\d]{8}$/.test(str);
|
|
1879
|
+
}
|
|
1880
|
+
|
|
1655
1881
|
function filterRequire(currSpace, cmdMatchStr) {
|
|
1656
1882
|
if (typeof cmdMatchStr === 'number') {
|
|
1657
1883
|
cmdMatchStr = '0x' + pad(8, BigNumber(cmdMatchStr).toString(16), '0');
|
|
@@ -1666,7 +1892,7 @@ function filterRequire(currSpace, cmdMatchStr) {
|
|
|
1666
1892
|
uName = pr[0] + "/i " + pr[1]
|
|
1667
1893
|
fullPath = getFullPath(uName)
|
|
1668
1894
|
}
|
|
1669
|
-
if (fullPath &&
|
|
1895
|
+
if (fullPath && fs.existsSync(fullPath)) {
|
|
1670
1896
|
cmds = [uName]
|
|
1671
1897
|
} else {
|
|
1672
1898
|
let appointSpace = parseUniqueName(cmdMatchStr)[0]
|
|
@@ -1687,7 +1913,7 @@ async function _requireSource(currSpace, cmdMatchStr) {
|
|
|
1687
1913
|
let result
|
|
1688
1914
|
let uniqueName = filterRequire(currSpace, cmdMatchStr);
|
|
1689
1915
|
let path = getFullPath(uniqueName)
|
|
1690
|
-
let text = String(
|
|
1916
|
+
let text = String(fs.readFileSync(path))
|
|
1691
1917
|
let pair = parseUniqueName(uniqueName)
|
|
1692
1918
|
if (pair[1].startsWith('i ')) {
|
|
1693
1919
|
result = await evalText(text, uniqueName)
|
|
@@ -1701,27 +1927,31 @@ async function _requireSource(currSpace, cmdMatchStr) {
|
|
|
1701
1927
|
if (!vl(result)) {
|
|
1702
1928
|
throw `invalid result: ${cmdMatchStr}`
|
|
1703
1929
|
}
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1930
|
+
return result
|
|
1931
|
+
}
|
|
1932
|
+
|
|
1933
|
+
function addErrorTag(text) {
|
|
1934
|
+
let result = [];
|
|
1935
|
+
let lines = text.split(/\n/);
|
|
1936
|
+
let functionEnd = true;
|
|
1937
|
+
let fnName = '';
|
|
1938
|
+
for (let i = 0; i < lines.length; i++) {
|
|
1939
|
+
let line = lines[i];
|
|
1940
|
+
result.push(line);
|
|
1941
|
+
if (line.startsWith('async function') || line.startsWith('function')) {
|
|
1942
|
+
fnName = reget(line, /function\s+([\s\S]+)\s*\(/)
|
|
1943
|
+
}
|
|
1944
|
+
if (functionEnd && fnName && (/\)\s*\{$/.test(trim(line)) || (line.startsWith("{") && trim(line) === '{'))) {
|
|
1945
|
+
result[i] += 'try{';
|
|
1946
|
+
functionEnd = false;
|
|
1947
|
+
}
|
|
1948
|
+
if (line.startsWith("}") && !functionEnd) {
|
|
1949
|
+
result[i] = `}catch(e){throw($errorTag(e,$cmdName+'[${fnName}]'))}` + line;
|
|
1950
|
+
functionEnd = true;
|
|
1951
|
+
fnName = '';
|
|
1722
1952
|
}
|
|
1723
1953
|
}
|
|
1724
|
-
return result
|
|
1954
|
+
return result.join('\n');
|
|
1725
1955
|
}
|
|
1726
1956
|
|
|
1727
1957
|
async function evalText($text = '', $cmdName = '', $args = []) {
|
|
@@ -1742,30 +1972,26 @@ async function evalText($text = '', $cmdName = '', $args = []) {
|
|
|
1742
1972
|
let $require = async (matchItem) => {
|
|
1743
1973
|
return await _requireSource(currSpace, matchItem)
|
|
1744
1974
|
}
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
$homeDir, $lib)
|
|
1754
|
-
} catch(e) {
|
|
1755
|
-
throw errorTag(e, $cmdName);
|
|
1756
|
-
}
|
|
1975
|
+
return await evalCode(addErrorTag($text), $cmdName, $args,
|
|
1976
|
+
$data, $config,
|
|
1977
|
+
$require, $requires,
|
|
1978
|
+
nextLine, nextText,
|
|
1979
|
+
setTips, delTips,
|
|
1980
|
+
wrapperInput, filterCmd,
|
|
1981
|
+
currSpace,
|
|
1982
|
+
$homeDir, $lib, _cmdMap);
|
|
1757
1983
|
}
|
|
1758
1984
|
|
|
1759
1985
|
process.on('uncaughtException',function(err){
|
|
1760
|
-
error(
|
|
1986
|
+
console.error('uncaughtException', err)
|
|
1761
1987
|
_noAppendNextLine || nextLine()
|
|
1762
1988
|
})
|
|
1763
1989
|
process.on('unhandledRejection',function(err){
|
|
1764
|
-
error(
|
|
1990
|
+
console.error('unhandledRejection', err)
|
|
1765
1991
|
_noAppendNextLine || nextLine()
|
|
1766
1992
|
})
|
|
1767
1993
|
process.on('rejectionHandled',function(err){
|
|
1768
|
-
error(
|
|
1994
|
+
console.error('rejectionHandled', err)
|
|
1769
1995
|
_noAppendNextLine || nextLine()
|
|
1770
1996
|
})
|
|
1771
1997
|
process.on('SIGINT', function () {
|