jsir 1.0.7 → 1.1.2
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/ooa.js +254 -191
- package/ethWeb.js +114 -54
- package/index.js +23 -11
- package/package.json +1 -1
- package/sol.js +2 -1
- package/util.js +159 -65
package/cmd/ooa.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
const {
|
|
3
3
|
run, getLibDataDir, trim, regEach, getConfig, mkdir, requireG, reget,
|
|
4
|
-
getCbText, setCbText, e, getOr, sleep, objDataFile, setConfig, vl
|
|
4
|
+
getCbText, setCbText, e, getOr, sleep, objDataFile, setConfig, vl, wrapRows,
|
|
5
|
+
info, warn, error, arrayDataFile
|
|
5
6
|
} = require('../index')
|
|
6
7
|
|
|
7
8
|
const _types = {
|
|
@@ -11,8 +12,9 @@ const _types = {
|
|
|
11
12
|
}
|
|
12
13
|
const _Chokidar = require('chokidar');
|
|
13
14
|
const _fs = require('fs')
|
|
14
|
-
const
|
|
15
|
-
const
|
|
15
|
+
const _libDataDir = getLibDataDir()
|
|
16
|
+
const _home = _libDataDir + '/ooa'
|
|
17
|
+
const _args = process.argv.slice(2).map(trim)
|
|
16
18
|
const _history9 = []
|
|
17
19
|
const _tipsOnRm = {}
|
|
18
20
|
const _setting = require('../setting')
|
|
@@ -20,6 +22,7 @@ let _cmdMap = {}
|
|
|
20
22
|
let _rl
|
|
21
23
|
let _haveWrapperInput = false
|
|
22
24
|
let _haveStartRead = false;
|
|
25
|
+
let _repos = arrayDataFile('repos.json')
|
|
23
26
|
|
|
24
27
|
const lib = require('../index')
|
|
25
28
|
|
|
@@ -37,6 +40,9 @@ const _table = require('console.table')
|
|
|
37
40
|
console.table = (...args) => {
|
|
38
41
|
console.log(trim(_table.getTable(...args)))
|
|
39
42
|
}
|
|
43
|
+
console.nable = (rows) => {
|
|
44
|
+
console.table(wrapRows(rows))
|
|
45
|
+
}
|
|
40
46
|
|
|
41
47
|
let _noAppendNextLine = true
|
|
42
48
|
|
|
@@ -93,11 +99,12 @@ const _mainCmdMap = {
|
|
|
93
99
|
cmd: ['@*']
|
|
94
100
|
},
|
|
95
101
|
repo: {
|
|
96
|
-
comment: "设置本地仓库目录"
|
|
102
|
+
comment: "设置本地仓库目录",
|
|
103
|
+
cmd: ['% ']
|
|
97
104
|
},
|
|
98
105
|
lib: {
|
|
99
106
|
comment: "查看内置资源",
|
|
100
|
-
cmd: ['
|
|
107
|
+
cmd: ['*lib ']
|
|
101
108
|
}
|
|
102
109
|
}
|
|
103
110
|
|
|
@@ -106,14 +113,6 @@ run(async () => {
|
|
|
106
113
|
_fs.mkdirSync(_home)
|
|
107
114
|
} catch (e) {}
|
|
108
115
|
|
|
109
|
-
if (_args[0] === 'repo') {
|
|
110
|
-
if (_args[1]) {
|
|
111
|
-
setConfig('jsLibSource', _args[1])
|
|
112
|
-
|
|
113
|
-
}
|
|
114
|
-
console.log(`jsLibSource: ${getConfig('jsLibSource')}`)
|
|
115
|
-
return
|
|
116
|
-
}
|
|
117
116
|
if (_args[0] === 'config') {
|
|
118
117
|
let configFile = getLibDataDir() + '/config.json';
|
|
119
118
|
e(`idea "${configFile}"`)
|
|
@@ -121,7 +120,7 @@ run(async () => {
|
|
|
121
120
|
}
|
|
122
121
|
|
|
123
122
|
dealSourceCmds()
|
|
124
|
-
dealInitData()
|
|
123
|
+
await dealInitData()
|
|
125
124
|
|
|
126
125
|
if (_args[0] === 'repl') {
|
|
127
126
|
_noAppendNextLine = false
|
|
@@ -165,7 +164,7 @@ run(async () => {
|
|
|
165
164
|
}
|
|
166
165
|
|
|
167
166
|
let argStr = _args.slice(1).map(i => {
|
|
168
|
-
if (/\s+/.test(i)) {
|
|
167
|
+
if (/\s+/.test(i) || !i) {
|
|
169
168
|
i = `"${i}"`
|
|
170
169
|
}
|
|
171
170
|
return i
|
|
@@ -176,14 +175,6 @@ run(async () => {
|
|
|
176
175
|
if (['ls'].indexOf(_args[0]) !== -1 && !trim(argStr)) {
|
|
177
176
|
argStr = ','
|
|
178
177
|
}
|
|
179
|
-
if (['lib'].indexOf(_args[0]) !== -1 && trim(argStr)) {
|
|
180
|
-
argStr = trim(argStr)
|
|
181
|
-
if (typeof lib[argStr] === "function") {
|
|
182
|
-
argStr = '.' + argStr + '.toString()'
|
|
183
|
-
} else {
|
|
184
|
-
argStr = '.' + argStr
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
178
|
|
|
188
179
|
let info = _mainCmdMap[_args[0]]
|
|
189
180
|
if (info) {
|
|
@@ -238,14 +229,13 @@ async function fileLine(name) {
|
|
|
238
229
|
&& newLine !== line) {
|
|
239
230
|
let exeStrs
|
|
240
231
|
if (newLine.startsWith("//")) {
|
|
241
|
-
exeStrs =
|
|
242
|
-
.filter(i => i)
|
|
232
|
+
exeStrs = []
|
|
243
233
|
} else if (newLine.startsWith('/*')) {
|
|
244
234
|
exeStrs = newLine.replace(/^\/\*|\*\/$/g, '').split('\n')
|
|
245
235
|
.map(i => trim(trim(i).replace(/^\*/, '')))
|
|
246
236
|
.filter(i => i)
|
|
247
237
|
} else {
|
|
248
|
-
exeStrs = ["
|
|
238
|
+
exeStrs = ["#" + newLine]
|
|
249
239
|
}
|
|
250
240
|
for(let exeStr of exeStrs) {
|
|
251
241
|
await wrapperInput(trim(exeStr))
|
|
@@ -305,10 +295,10 @@ function getLines(text) {
|
|
|
305
295
|
lines.push(cLine.join('\n'))
|
|
306
296
|
}
|
|
307
297
|
|
|
308
|
-
return lines.reverse()
|
|
298
|
+
return lines.slice(1).reverse()
|
|
309
299
|
}
|
|
310
300
|
|
|
311
|
-
function dealInitData() {
|
|
301
|
+
async function dealInitData() {
|
|
312
302
|
if (!global.$i) {
|
|
313
303
|
global.$i = {}
|
|
314
304
|
}
|
|
@@ -321,22 +311,25 @@ function dealInitData() {
|
|
|
321
311
|
.replace(/\.js$/, '')
|
|
322
312
|
.split(/\s+/).filter(item => item).join("_");
|
|
323
313
|
let text = String(_fs.readFileSync(filePath))
|
|
314
|
+
$i[initDataName] = null
|
|
324
315
|
try {
|
|
325
|
-
$i[initDataName] = evalText(text)
|
|
316
|
+
$i[initDataName] = await evalText(text)
|
|
326
317
|
} catch (e) {
|
|
327
|
-
|
|
318
|
+
$log(`$i.${initDataName} loading failed\n${e.stack}`);
|
|
328
319
|
}
|
|
329
320
|
if (!vl($i[initDataName])) {
|
|
330
321
|
warn(`$i.${initDataName} loading failed`)
|
|
331
322
|
}
|
|
332
|
-
|
|
333
|
-
if (!text.match(/\s+\$nextLine\s*\(\s*\)/)) {
|
|
334
|
-
_noAppendNextLine || nextLine()
|
|
335
|
-
}
|
|
336
323
|
}
|
|
337
324
|
}
|
|
338
325
|
|
|
339
|
-
function nextLine(callback, preStr, hidden) {
|
|
326
|
+
async function nextLine(callback, preStr, hidden) {
|
|
327
|
+
return new Promise(resolve => {
|
|
328
|
+
_nextLine(callback, preStr, hidden, resolve)
|
|
329
|
+
})
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
function _nextLine(callback, preStr, hidden, resolve) {
|
|
340
333
|
if (_haveStartRead && !_haveWrapperInput) {
|
|
341
334
|
return
|
|
342
335
|
}
|
|
@@ -358,9 +351,10 @@ function nextLine(callback, preStr, hidden) {
|
|
|
358
351
|
}
|
|
359
352
|
}
|
|
360
353
|
_rl.removeAllListeners('line')
|
|
361
|
-
_rl.on('line', line => {
|
|
354
|
+
_rl.on('line', async line => {
|
|
355
|
+
line = trim(line)
|
|
362
356
|
_haveWrapperInput = true;
|
|
363
|
-
if (hidden) {
|
|
357
|
+
if (line && (hidden || (callback && callback !== wrapperInput))) {
|
|
364
358
|
_rl.history = _rl.history.slice(1)
|
|
365
359
|
}
|
|
366
360
|
if (_noAppendNextLine && _rl) {
|
|
@@ -368,31 +362,44 @@ function nextLine(callback, preStr, hidden) {
|
|
|
368
362
|
_rl = null
|
|
369
363
|
}
|
|
370
364
|
try {
|
|
371
|
-
(callback || wrapperInput)(
|
|
365
|
+
await (callback || wrapperInput)(line)
|
|
372
366
|
} finally {
|
|
367
|
+
resolve && resolve(line)
|
|
373
368
|
callback = null
|
|
369
|
+
hidden = false
|
|
374
370
|
}
|
|
375
371
|
})
|
|
372
|
+
let repoTip = trim(trim(getConfig("jsLibSource")).split('/').map(trim).reverse().filter(i => i)[0])
|
|
376
373
|
let promitStr = (preStr
|
|
377
374
|
|| ((callback && callback !== wrapperInput) ? "-> ":"")
|
|
378
|
-
|| Object.values(global.$tips).filter(i => String(i)).join(',') +
|
|
375
|
+
|| [Object.values(global.$tips).filter(i => String(i)).join(','), repoTip].filter(i => i).join('-') + `> `)
|
|
379
376
|
_rl.setPrompt(promitStr)
|
|
380
377
|
process.stdout.write('\x1B[32m')
|
|
381
378
|
_rl.prompt()
|
|
382
379
|
process.stdout.write('\x1B[39m')
|
|
383
380
|
}
|
|
384
381
|
|
|
385
|
-
function nextText(callback, preStr, hidden) {
|
|
382
|
+
async function nextText(callback, preStr, hidden) {
|
|
383
|
+
return new Promise(resolve => {
|
|
384
|
+
_nextText(callback, preStr, hidden, resolve)
|
|
385
|
+
})
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
async function _nextText(callback, preStr, hidden, resolve) {
|
|
386
389
|
preStr = preStr || "#"
|
|
387
390
|
let text = ''
|
|
388
|
-
let
|
|
389
|
-
|
|
390
|
-
callback(trim(text))
|
|
391
|
-
}
|
|
391
|
+
let line = await nextLine(line => line, preStr, hidden)
|
|
392
|
+
while (line !== trim(preStr)) {
|
|
392
393
|
text += line + "\n"
|
|
393
|
-
nextLine(
|
|
394
|
+
line = await nextLine(line => line, preStr, hidden)
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
text = trim(text)
|
|
398
|
+
try {
|
|
399
|
+
await callback(text)
|
|
400
|
+
} finally {
|
|
401
|
+
resolve && resolve(text)
|
|
394
402
|
}
|
|
395
|
-
nextLine(fn, preStr, hidden)
|
|
396
403
|
}
|
|
397
404
|
|
|
398
405
|
function dealSourceCmds() {
|
|
@@ -407,7 +414,7 @@ function dealSourceCmds() {
|
|
|
407
414
|
}
|
|
408
415
|
})
|
|
409
416
|
} else {
|
|
410
|
-
warn(`config.jsLibSource
|
|
417
|
+
warn(`config.jsLibSource undefined, run [${_setting.name} repo]`)
|
|
411
418
|
}
|
|
412
419
|
}
|
|
413
420
|
|
|
@@ -418,7 +425,7 @@ async function save(args) {
|
|
|
418
425
|
return
|
|
419
426
|
}
|
|
420
427
|
let resp = `/*
|
|
421
|
-
test
|
|
428
|
+
A test script
|
|
422
429
|
*/
|
|
423
430
|
|
|
424
431
|
$defArgs({
|
|
@@ -429,9 +436,7 @@ $defArgs({
|
|
|
429
436
|
let {} = $lib // 引用内置资源
|
|
430
437
|
let {} = $i // 引用初始资源
|
|
431
438
|
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
console.log($args.arg)`;
|
|
439
|
+
console.log($args)`;
|
|
435
440
|
|
|
436
441
|
if (args[0] !== 'e') {
|
|
437
442
|
resp = await getCbText();
|
|
@@ -447,28 +452,7 @@ function rename(old, _new) {
|
|
|
447
452
|
return
|
|
448
453
|
}
|
|
449
454
|
_fs.rename(old, _new, ()=>{})
|
|
450
|
-
|
|
451
|
-
}
|
|
452
|
-
|
|
453
|
-
function info(msg) {
|
|
454
|
-
process.stdout.write('\x1B[32m')
|
|
455
|
-
process.stdout.write('[info] ')
|
|
456
|
-
process.stdout.write('\x1B[39m')
|
|
457
|
-
console.log(msg)
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
function warn(msg) {
|
|
461
|
-
process.stdout.write('\x1B[33m')
|
|
462
|
-
process.stdout.write('[warn] ')
|
|
463
|
-
process.stdout.write('\x1B[39m')
|
|
464
|
-
console.log(msg)
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
function error(e, msg) {
|
|
468
|
-
process.stdout.write('\x1B[35m')
|
|
469
|
-
process.stdout.write(`[${msg || 'error'}] `)
|
|
470
|
-
process.stdout.write('\x1B[39m')
|
|
471
|
-
console.log(e)
|
|
455
|
+
return true
|
|
472
456
|
}
|
|
473
457
|
|
|
474
458
|
function putHis(x) {
|
|
@@ -527,8 +511,8 @@ function listCmd() {
|
|
|
527
511
|
let name = _cmdMap[i].replace(/\.js$/, '')
|
|
528
512
|
let item = {
|
|
529
513
|
key: prefix.join('') + i,
|
|
530
|
-
|
|
531
|
-
|
|
514
|
+
name: name.replace(/^[eif]\s+/, ''),
|
|
515
|
+
type: _types[name.split(/\s+/)[0]] || 'note'
|
|
532
516
|
}
|
|
533
517
|
items.push(item)
|
|
534
518
|
|
|
@@ -544,6 +528,35 @@ function listCmd() {
|
|
|
544
528
|
}
|
|
545
529
|
}
|
|
546
530
|
|
|
531
|
+
function printObjProfile(result) {
|
|
532
|
+
result = Object.keys(result).map(key => {
|
|
533
|
+
return {
|
|
534
|
+
key,
|
|
535
|
+
type: typeof result[key]
|
|
536
|
+
}})
|
|
537
|
+
.sort((a,b) => {
|
|
538
|
+
return a.type >= b.type ? 1:-1
|
|
539
|
+
})
|
|
540
|
+
|
|
541
|
+
let temp = []
|
|
542
|
+
for(let i = 0; i< Object.keys(result).length; i+=3) {
|
|
543
|
+
let item = {}
|
|
544
|
+
temp.push(item)
|
|
545
|
+
|
|
546
|
+
item['type'] = result[i].type
|
|
547
|
+
item['name']= result[i].key
|
|
548
|
+
if (result[i+1]) {
|
|
549
|
+
item['type '] = result[i+1].type
|
|
550
|
+
item['name ']= result[i+1].key
|
|
551
|
+
}
|
|
552
|
+
if (result[i+2]) {
|
|
553
|
+
item['type '] = result[i+2].type
|
|
554
|
+
item['name ']= result[i+2].key
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
console.table(temp)
|
|
558
|
+
}
|
|
559
|
+
|
|
547
560
|
async function wrapperInput(str) {
|
|
548
561
|
global.$newInput = true;
|
|
549
562
|
|
|
@@ -552,46 +565,51 @@ async function wrapperInput(str) {
|
|
|
552
565
|
_noAppendNextLine || nextLine()
|
|
553
566
|
return;
|
|
554
567
|
}
|
|
555
|
-
if (
|
|
556
|
-
let
|
|
568
|
+
if (/^[$#*]/.test(str)) {
|
|
569
|
+
let is$ = str.startsWith('$')
|
|
570
|
+
let isStar = str.startsWith('*')
|
|
571
|
+
let text = trim(str.replace(/^[$#*]/, ''))
|
|
557
572
|
try {
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
result
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
let
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
573
|
+
if (is$) {
|
|
574
|
+
let result = await evalText('return ' + text)
|
|
575
|
+
if (result === $i || result === $lib) {
|
|
576
|
+
printObjProfile(result)
|
|
577
|
+
} else {
|
|
578
|
+
console.log(result)
|
|
579
|
+
}
|
|
580
|
+
return
|
|
581
|
+
}
|
|
582
|
+
if (isStar) {
|
|
583
|
+
let items = text.split(/\s+/).map(trim).filter(i => i)
|
|
584
|
+
let result = await evalText('return ' + items[0])
|
|
585
|
+
if (!result || Object.keys(result).length === 0) {
|
|
586
|
+
console.log(result)
|
|
587
|
+
return
|
|
588
|
+
}
|
|
589
|
+
let rows = []
|
|
590
|
+
let matchs = items.slice(1)
|
|
591
|
+
for (let key of Object.keys(result)) {
|
|
592
|
+
if (matchs.length === 0 || isArgsMatch(key, matchs)){
|
|
593
|
+
let type = typeof result[key]
|
|
594
|
+
let str = String(result[key])
|
|
595
|
+
rows.push({
|
|
596
|
+
key: key,
|
|
597
|
+
value: type === 'function' ? str.split('\n')[0].replace(/\s*{\s*$/, '')
|
|
598
|
+
: (str.length <= 64 ? str:type)
|
|
599
|
+
})
|
|
583
600
|
}
|
|
584
601
|
}
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
602
|
+
if (rows.length === 0) {
|
|
603
|
+
warn("no items")
|
|
604
|
+
} else {
|
|
605
|
+
console.table(rows)
|
|
606
|
+
}
|
|
607
|
+
return
|
|
588
608
|
}
|
|
609
|
+
await evalText(text)
|
|
589
610
|
} catch (e) {
|
|
590
611
|
error(e)
|
|
591
612
|
}
|
|
592
|
-
if (!text.match(/\s+\$nextLine\s*\(\s*\)/)) {
|
|
593
|
-
_noAppendNextLine || nextLine()
|
|
594
|
-
}
|
|
595
613
|
return;
|
|
596
614
|
}
|
|
597
615
|
|
|
@@ -604,6 +622,31 @@ async function wrapperInput(str) {
|
|
|
604
622
|
} else {
|
|
605
623
|
listCmd()
|
|
606
624
|
}
|
|
625
|
+
_noAppendNextLine || nextLine()
|
|
626
|
+
} else if (str.match(/^%/)) {
|
|
627
|
+
let newRepo = trim(str.substring(1))
|
|
628
|
+
if (!newRepo) {
|
|
629
|
+
console.log(`current: ${getConfig('jsLibSource')}`)
|
|
630
|
+
_repos = _repos.filter(i => _fs.existsSync(i))
|
|
631
|
+
for(let i = 0; i<_repos.length; i++) {
|
|
632
|
+
console.log(`${i+1}: ${_repos[i]}`)
|
|
633
|
+
}
|
|
634
|
+
let repoStr = trim(await nextLine(line => line, 'reset(or blank): '))
|
|
635
|
+
if (/^\d+$/.test(repoStr)) {
|
|
636
|
+
repoStr = _repos[Number(repoStr) - 1]
|
|
637
|
+
}
|
|
638
|
+
newRepo = repoStr
|
|
639
|
+
}
|
|
640
|
+
if (newRepo) {
|
|
641
|
+
setConfig('jsLibSource', newRepo)
|
|
642
|
+
if (_repos.indexOf(newRepo) === -1) {
|
|
643
|
+
_repos.push(newRepo)
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
arrayDataFile('repos.json', () => _repos)
|
|
647
|
+
dealSourceCmds()
|
|
648
|
+
await dealInitData()
|
|
649
|
+
|
|
607
650
|
_noAppendNextLine || nextLine()
|
|
608
651
|
} else if (!str.split(/\s+/)[0].match(/^\d+$/)) {
|
|
609
652
|
_cmdMap = filterCmd(str.split(/\s+/))
|
|
@@ -693,7 +736,9 @@ async function dealKeyword(str, strs, fstr, ostr) {
|
|
|
693
736
|
let newName =trim(ostr.join(" "))
|
|
694
737
|
if (newName) {
|
|
695
738
|
newName = newName + ".js"
|
|
696
|
-
rename(_home + '/' + name, _home + '/' + newName)
|
|
739
|
+
if (rename(_home + '/' + name, _home + '/' + newName)) {
|
|
740
|
+
info(`${_home + '/' + newName} renamed`)
|
|
741
|
+
}
|
|
697
742
|
let jsLibDir = trim(getConfig("jsLibSource"))
|
|
698
743
|
if (jsLibDir && _fs.existsSync(jsLibDir)) {
|
|
699
744
|
let cmdsDir = jsLibDir
|
|
@@ -796,6 +841,7 @@ async function dealKeyword(str, strs, fstr, ostr) {
|
|
|
796
841
|
|
|
797
842
|
function getComments(text, cols = [], col) {
|
|
798
843
|
let docLines = []
|
|
844
|
+
text = trim(text)
|
|
799
845
|
if (text.startsWith("/*")) {
|
|
800
846
|
for (let line of text.split("\n")) {
|
|
801
847
|
let aLine = line
|
|
@@ -816,11 +862,11 @@ function getComments(text, cols = [], col) {
|
|
|
816
862
|
docLines.push(...comments)
|
|
817
863
|
}
|
|
818
864
|
if (docLines.length > 0) {
|
|
819
|
-
col.doc = docLines[0]
|
|
865
|
+
col.doc = '-\x1B[32m' + docLines[0] + '\x1B[39m'
|
|
820
866
|
if (docLines.length > 1) {
|
|
821
867
|
for (let line of docLines.slice(1)) {
|
|
822
868
|
cols.push({
|
|
823
|
-
doc: line
|
|
869
|
+
doc: ' ' + line
|
|
824
870
|
})
|
|
825
871
|
}
|
|
826
872
|
}
|
|
@@ -837,24 +883,38 @@ function filterCmd(args){
|
|
|
837
883
|
if (!file.endsWith('.js') || !fileName) {
|
|
838
884
|
continue
|
|
839
885
|
}
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
for (let str of arg.split(',').filter(item => trim(item) !== '')) {
|
|
843
|
-
let reg = new RegExp(str, 'i')
|
|
844
|
-
if (!reg.test(fileName)) {
|
|
845
|
-
r = false
|
|
846
|
-
break
|
|
847
|
-
}
|
|
848
|
-
}
|
|
849
|
-
if (r && Object.values(cmdMap).indexOf(file) === -1) {
|
|
886
|
+
isArgsMatch(fileName, args, () => {
|
|
887
|
+
if (Object.values(cmdMap).indexOf(file) === -1) {
|
|
850
888
|
cmdMap[i] = file
|
|
851
889
|
i ++
|
|
852
890
|
}
|
|
853
|
-
}
|
|
891
|
+
})
|
|
854
892
|
}
|
|
855
893
|
return cmdMap
|
|
856
894
|
}
|
|
857
895
|
|
|
896
|
+
function isArgsMatch(text, args, callback) {
|
|
897
|
+
let match = false
|
|
898
|
+
for (let arg of args) {
|
|
899
|
+
let r = true
|
|
900
|
+
for (let str of arg.split(',').filter(item => trim(item) !== '')) {
|
|
901
|
+
let reg = new RegExp(str, 'i')
|
|
902
|
+
if (!reg.test(text)) {
|
|
903
|
+
r = false
|
|
904
|
+
break
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
if (r) {
|
|
908
|
+
match = true
|
|
909
|
+
break
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
if (match && callback) {
|
|
913
|
+
callback()
|
|
914
|
+
}
|
|
915
|
+
return match
|
|
916
|
+
}
|
|
917
|
+
|
|
858
918
|
function getArgComments(argDef) {
|
|
859
919
|
let comments = []
|
|
860
920
|
let keys = Object.keys(argDef)
|
|
@@ -865,15 +925,15 @@ function getArgComments(argDef) {
|
|
|
865
925
|
comments.push(aLine.join(", ") + ", ")
|
|
866
926
|
aLine = []
|
|
867
927
|
}
|
|
868
|
-
aLine.push(
|
|
928
|
+
aLine.push(`\x1B[33m${k}\x1B[39m` + (argDef[k] ? ` <${argDef[k]}>`:''))
|
|
869
929
|
}
|
|
870
930
|
if (aLine.length > 0) {
|
|
871
931
|
comments.push(aLine.join(", "))
|
|
872
932
|
}
|
|
873
|
-
comments[0] = '
|
|
933
|
+
comments[0] = '$args: ' + comments[0]
|
|
874
934
|
if (comments.length > 1) {
|
|
875
935
|
for(let i = 1; i< comments.length; i++) {
|
|
876
|
-
comments[i] = '
|
|
936
|
+
comments[i] = ' ' + comments[i]
|
|
877
937
|
}
|
|
878
938
|
}
|
|
879
939
|
}
|
|
@@ -887,6 +947,55 @@ async function runCmd(str) {
|
|
|
887
947
|
_noAppendNextLine || nextLine()
|
|
888
948
|
return
|
|
889
949
|
}
|
|
950
|
+
|
|
951
|
+
let path = _home + '/' + _cmdMap[strs[0]]
|
|
952
|
+
let text = _cmdMap[strs[0]] ? String(_fs.readFileSync(path)) : await getCbText()
|
|
953
|
+
|
|
954
|
+
let args = enrichArgs(str, text)
|
|
955
|
+
let argDef = getArgDef(text)
|
|
956
|
+
let argNames = Object.keys(argDef)
|
|
957
|
+
for (let i = 0; i<args.length; i++) {
|
|
958
|
+
let arg = args[i]
|
|
959
|
+
let pair = arg.split('=', 2).map(i => trim(i))
|
|
960
|
+
let argVal = argDef.hasOwnProperty(pair[0]) ? pair[1]:arg
|
|
961
|
+
argVal = translateArgs(argVal)
|
|
962
|
+
|
|
963
|
+
args[i] = argVal
|
|
964
|
+
if (argNames[i]) {
|
|
965
|
+
args[argNames[i]] = argVal
|
|
966
|
+
}
|
|
967
|
+
if (argDef.hasOwnProperty(pair[0])) {
|
|
968
|
+
args[i] = argVal
|
|
969
|
+
delete args[argNames[i]]
|
|
970
|
+
args[pair[0]] = argVal
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
let exit = false
|
|
975
|
+
for(let name of argNames) {
|
|
976
|
+
if (name.startsWith("_")) {
|
|
977
|
+
continue
|
|
978
|
+
}
|
|
979
|
+
if (args[name] === undefined || args[name] === null) {
|
|
980
|
+
exit = true
|
|
981
|
+
let comment = trim(argDef[name]);
|
|
982
|
+
warn(`require \x1B[33m${name}\x1B[39m` + (comment ? ` <${comment}>`:''))
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
if (exit) {
|
|
986
|
+
_noAppendNextLine || nextLine()
|
|
987
|
+
return
|
|
988
|
+
}
|
|
989
|
+
|
|
990
|
+
process.argv = [process.argv[0], path, ...args]
|
|
991
|
+
try {
|
|
992
|
+
await evalText(text, strs[0], _cmdMap[strs[0]], args)
|
|
993
|
+
} catch (e) {
|
|
994
|
+
error(e)
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
function enrichArgs(str) {
|
|
890
999
|
let args = []
|
|
891
1000
|
let temp = []
|
|
892
1001
|
str = str.replace(/^\d+\s*/, '')
|
|
@@ -911,8 +1020,10 @@ async function runCmd(str) {
|
|
|
911
1020
|
}
|
|
912
1021
|
i ++
|
|
913
1022
|
})
|
|
914
|
-
|
|
1023
|
+
return args.map(trim)
|
|
1024
|
+
}
|
|
915
1025
|
|
|
1026
|
+
function translateArgs(arg) {
|
|
916
1027
|
let rpStr = s => {
|
|
917
1028
|
let ori = s;
|
|
918
1029
|
s = s.replace(/^\$\{/, '').replace(/}$/, '')
|
|
@@ -921,85 +1032,38 @@ async function runCmd(str) {
|
|
|
921
1032
|
}
|
|
922
1033
|
return eval(`(${s})`)
|
|
923
1034
|
}
|
|
924
|
-
for (let i = 0; i < args.length; i++) {
|
|
925
|
-
if (typeof args[i] !== 'string') {
|
|
926
|
-
continue
|
|
927
|
-
}
|
|
928
|
-
if (/^\$\{.+}$/.test(args[i])) {
|
|
929
|
-
args[i] = rpStr(args[i])
|
|
930
|
-
} else {
|
|
931
|
-
args[i] = args[i].replace(/\$\{.+}/g, rpStr)
|
|
932
|
-
}
|
|
933
|
-
}
|
|
934
|
-
process.argv = [process.argv[0], path, ...args]
|
|
935
1035
|
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
let arg = args[i]
|
|
941
|
-
if (argNames[i]) {
|
|
942
|
-
args[argNames[i]] = arg
|
|
943
|
-
}
|
|
944
|
-
|
|
945
|
-
let pair = arg.split('=', 2).map(i => trim(i))
|
|
946
|
-
if (argDef.hasOwnProperty(pair[0])) {
|
|
947
|
-
args[i] = pair[1]
|
|
948
|
-
args[argNames[i]] = pair[1]
|
|
949
|
-
args[pair[0]] = pair[1]
|
|
950
|
-
}
|
|
951
|
-
}
|
|
952
|
-
|
|
953
|
-
let exit = false
|
|
954
|
-
for(let name of argNames) {
|
|
955
|
-
if (name.startsWith("_")) {
|
|
956
|
-
continue
|
|
957
|
-
}
|
|
958
|
-
if (!vl(args[name])) {
|
|
959
|
-
if (!exit) {
|
|
960
|
-
let comments = getArgComments(argDef)
|
|
961
|
-
if (comments.length > 0) {
|
|
962
|
-
console.log(comments.join('\n'))
|
|
963
|
-
}
|
|
964
|
-
}
|
|
965
|
-
exit = true
|
|
966
|
-
warn(`require ${name}`)
|
|
967
|
-
}
|
|
968
|
-
}
|
|
969
|
-
if (exit) {
|
|
970
|
-
_noAppendNextLine || nextLine()
|
|
971
|
-
return
|
|
972
|
-
}
|
|
973
|
-
|
|
974
|
-
try {
|
|
975
|
-
evalText(text, strs[0], _cmdMap[strs[0]], args)
|
|
976
|
-
} catch (e) {
|
|
977
|
-
error(e)
|
|
978
|
-
}
|
|
979
|
-
if (!text.match(/\s+\$nextLine\s*\(\s*\)/)) {
|
|
980
|
-
_noAppendNextLine || nextLine()
|
|
1036
|
+
if (/^\$\{.+}$/.test(arg)) {
|
|
1037
|
+
return rpStr(arg)
|
|
1038
|
+
} else {
|
|
1039
|
+
return arg.replace(/\$\{.+}/g, rpStr)
|
|
981
1040
|
}
|
|
982
1041
|
}
|
|
983
1042
|
|
|
984
1043
|
function getArgDef(text) {
|
|
985
|
-
let exeStr = trim(reget(text, /\$defArgs\(([
|
|
1044
|
+
let exeStr = trim(reget(text, /\$defArgs\(\s*(\{[\s\S]*?\})\s*\)/))
|
|
986
1045
|
let argDef = {}
|
|
987
1046
|
try {
|
|
988
1047
|
argDef = eval(`(${exeStr || '{}'})`) || {}
|
|
989
1048
|
} catch (e) {
|
|
990
|
-
|
|
1049
|
+
$log(e.stack)
|
|
991
1050
|
}
|
|
992
1051
|
if (typeof argDef !== 'object') {
|
|
993
1052
|
argDef = {}
|
|
994
1053
|
}
|
|
995
1054
|
let temp = {}
|
|
1055
|
+
let tempOpt = {}
|
|
996
1056
|
for (let key of Object.keys(argDef)) {
|
|
997
1057
|
let vlKey = trim(key)
|
|
998
1058
|
if (vlKey) {
|
|
999
|
-
|
|
1059
|
+
if (vlKey.startsWith('_')) {
|
|
1060
|
+
tempOpt[vlKey] = argDef[key]
|
|
1061
|
+
} else {
|
|
1062
|
+
temp[vlKey] = argDef[key]
|
|
1063
|
+
}
|
|
1000
1064
|
}
|
|
1001
1065
|
}
|
|
1002
|
-
return temp
|
|
1066
|
+
return Object.assign(temp, tempOpt)
|
|
1003
1067
|
}
|
|
1004
1068
|
|
|
1005
1069
|
function setTips(key, value, onRm) {
|
|
@@ -1039,15 +1103,14 @@ function evalText($text, $cmdIndex, $cmdName, $args) {
|
|
|
1039
1103
|
const $nextText = nextText;
|
|
1040
1104
|
const $setTips = setTips;
|
|
1041
1105
|
const $delTips = delTips;
|
|
1106
|
+
const $info = info;
|
|
1107
|
+
const $warn = warn;
|
|
1108
|
+
const $error = error;
|
|
1042
1109
|
const $cmdDir = _home + '/';
|
|
1043
1110
|
const $cmdMap = _cmdMap;
|
|
1044
1111
|
const $defArgs = () => $args
|
|
1045
1112
|
|
|
1046
|
-
|
|
1047
|
-
return eval(`(()=>{${$text}})()`)
|
|
1048
|
-
} else {
|
|
1049
|
-
return eval(`${$text}`)
|
|
1050
|
-
}
|
|
1113
|
+
return eval(`(async ()=>{${$text}\n})()`)
|
|
1051
1114
|
}
|
|
1052
1115
|
|
|
1053
1116
|
process.on('uncaughtException',function(err){
|