jsir 1.1.0 → 1.1.1

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.
Files changed (3) hide show
  1. package/cmd/ooa.js +133 -92
  2. package/ethWeb.js +4 -4
  3. package/package.json +1 -1
package/cmd/ooa.js CHANGED
@@ -99,11 +99,12 @@ const _mainCmdMap = {
99
99
  cmd: ['@*']
100
100
  },
101
101
  repo: {
102
- comment: "设置本地仓库目录"
102
+ comment: "设置本地仓库目录",
103
+ cmd: ['% ']
103
104
  },
104
105
  lib: {
105
106
  comment: "查看内置资源",
106
- cmd: ['$lib']
107
+ cmd: ['*lib ']
107
108
  }
108
109
  }
109
110
 
@@ -112,29 +113,6 @@ run(async () => {
112
113
  _fs.mkdirSync(_home)
113
114
  } catch (e) {}
114
115
 
115
- if (_args[0] === 'repo') {
116
- let newRepo = _args[1]
117
- if (!newRepo) {
118
- console.log(`current: ${getConfig('jsLibSource')}`)
119
- _repos = _repos.filter(i => _fs.existsSync(i))
120
- for(let i = 0; i<_repos.length; i++) {
121
- console.log(`${i+1}: ${_repos[i]}`)
122
- }
123
- let repoStr = trim(await nextLine(line => line, 'reset(or blank): '))
124
- if (/^\d+$/.test(repoStr)) {
125
- repoStr = _repos[Number(repoStr) - 1]
126
- }
127
- newRepo = repoStr
128
- }
129
- if (newRepo) {
130
- setConfig('jsLibSource', newRepo)
131
- if (_repos.indexOf(newRepo) === -1) {
132
- _repos.push(newRepo)
133
- }
134
- }
135
- arrayDataFile('repos.json', () => _repos)
136
- return
137
- }
138
116
  if (_args[0] === 'config') {
139
117
  let configFile = getLibDataDir() + '/config.json';
140
118
  e(`idea "${configFile}"`)
@@ -197,14 +175,6 @@ run(async () => {
197
175
  if (['ls'].indexOf(_args[0]) !== -1 && !trim(argStr)) {
198
176
  argStr = ','
199
177
  }
200
- if (['lib'].indexOf(_args[0]) !== -1 && trim(argStr)) {
201
- argStr = trim(argStr)
202
- if (typeof lib[argStr] === "function") {
203
- argStr = '.' + argStr + '.toString()'
204
- } else {
205
- argStr = '.' + argStr
206
- }
207
- }
208
178
 
209
179
  let info = _mainCmdMap[_args[0]]
210
180
  if (info) {
@@ -258,7 +228,9 @@ async function fileLine(name) {
258
228
  && newLines.length >= lines.length
259
229
  && newLine !== line) {
260
230
  let exeStrs
261
- if (newLine.startsWith('/*')) {
231
+ if (newLine.startsWith("//")) {
232
+ exeStrs = []
233
+ } else if (newLine.startsWith('/*')) {
262
234
  exeStrs = newLine.replace(/^\/\*|\*\/$/g, '').split('\n')
263
235
  .map(i => trim(trim(i).replace(/^\*/, '')))
264
236
  .filter(i => i)
@@ -304,7 +276,7 @@ function getLines(text) {
304
276
  lines.push(cLine.join('\n'))
305
277
  cLine = []
306
278
  }
307
- cLine.push(line)
279
+ lines.push(line)
308
280
  continue
309
281
  }
310
282
 
@@ -339,10 +311,11 @@ async function dealInitData() {
339
311
  .replace(/\.js$/, '')
340
312
  .split(/\s+/).filter(item => item).join("_");
341
313
  let text = String(_fs.readFileSync(filePath))
314
+ $i[initDataName] = null
342
315
  try {
343
316
  $i[initDataName] = await evalText(text)
344
317
  } catch (e) {
345
- error(e, `$i.${initDataName} loading failed`)
318
+ $log(`$i.${initDataName} loading failed\n${e.stack}`);
346
319
  }
347
320
  if (!vl($i[initDataName])) {
348
321
  warn(`$i.${initDataName} loading failed`)
@@ -381,7 +354,7 @@ function _nextLine(callback, preStr, hidden, resolve) {
381
354
  _rl.on('line', async line => {
382
355
  line = trim(line)
383
356
  _haveWrapperInput = true;
384
- if (hidden) {
357
+ if (hidden || (callback && callback !== wrapperInput)) {
385
358
  _rl.history = _rl.history.slice(1)
386
359
  }
387
360
  if (_noAppendNextLine && _rl) {
@@ -393,11 +366,13 @@ function _nextLine(callback, preStr, hidden, resolve) {
393
366
  } finally {
394
367
  resolve && resolve(line)
395
368
  callback = null
369
+ hidden = false
396
370
  }
397
371
  })
372
+ let repoTip = trim(trim(getConfig("jsLibSource")).split('/').map(trim).reverse().filter(i => i)[0])
398
373
  let promitStr = (preStr
399
374
  || ((callback && callback !== wrapperInput) ? "-> ":"")
400
- || Object.values(global.$tips).filter(i => String(i)).join(',') + "> ")
375
+ || [Object.values(global.$tips).filter(i => String(i)).join(','), repoTip].filter(i => i).join('-') + `> `)
401
376
  _rl.setPrompt(promitStr)
402
377
  process.stdout.write('\x1B[32m')
403
378
  _rl.prompt()
@@ -553,6 +528,35 @@ function listCmd() {
553
528
  }
554
529
  }
555
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
+
556
560
  async function wrapperInput(str) {
557
561
  global.$newInput = true;
558
562
 
@@ -561,45 +565,48 @@ async function wrapperInput(str) {
561
565
  _noAppendNextLine || nextLine()
562
566
  return;
563
567
  }
564
- if (/^[$#]/.test(str)) {
568
+ if (/^[$#*]/.test(str)) {
565
569
  let is$ = str.startsWith('$')
566
- let text = trim(str.replace(/^[$#]/, ''))
570
+ let isStar = str.startsWith('*')
571
+ let text = trim(str.replace(/^[$#*]/, ''))
567
572
  try {
568
- if (!is$) {
569
- await evalText(text)
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
+ }
570
580
  return
571
581
  }
572
- let result = await evalText('return ' + text)
573
- if (result === $i || result === $lib) {
574
- result = Object.keys(result).map(key => {
575
- return {
576
- key,
577
- type: typeof result[key]
578
- }})
579
- .sort((a,b) => {
580
- return a.type >= b.type ? 1:-1
581
- })
582
-
583
- let temp = []
584
- for(let i = 0; i< Object.keys(result).length; i+=3) {
585
- let item = {}
586
- temp.push(item)
587
-
588
- item['type'] = result[i].type
589
- item['name']= result[i].key
590
- if (result[i+1]) {
591
- item['type '] = result[i+1].type
592
- item['name ']= result[i+1].key
593
- }
594
- if (result[i+2]) {
595
- item['type '] = result[i+2].type
596
- item['name ']= result[i+2].key
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
+ })
597
600
  }
598
601
  }
599
- console.table(temp)
600
- } else {
601
- console.log(result)
602
+ if (rows.length === 0) {
603
+ warn("no items")
604
+ } else {
605
+ console.table(rows)
606
+ }
607
+ return
602
608
  }
609
+ await evalText(text)
603
610
  } catch (e) {
604
611
  error(e)
605
612
  }
@@ -615,6 +622,31 @@ async function wrapperInput(str) {
615
622
  } else {
616
623
  listCmd()
617
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
+
618
650
  _noAppendNextLine || nextLine()
619
651
  } else if (!str.split(/\s+/)[0].match(/^\d+$/)) {
620
652
  _cmdMap = filterCmd(str.split(/\s+/))
@@ -830,7 +862,7 @@ function getComments(text, cols = [], col) {
830
862
  docLines.push(...comments)
831
863
  }
832
864
  if (docLines.length > 0) {
833
- col.doc = '-' + docLines[0]
865
+ col.doc = '-\x1B[32m' + docLines[0] + '\x1B[39m'
834
866
  if (docLines.length > 1) {
835
867
  for (let line of docLines.slice(1)) {
836
868
  cols.push({
@@ -851,24 +883,38 @@ function filterCmd(args){
851
883
  if (!file.endsWith('.js') || !fileName) {
852
884
  continue
853
885
  }
854
- for (let arg of args) {
855
- let r = true
856
- for (let str of arg.split(',').filter(item => trim(item) !== '')) {
857
- let reg = new RegExp(str, 'i')
858
- if (!reg.test(fileName)) {
859
- r = false
860
- break
861
- }
862
- }
863
- if (r && Object.values(cmdMap).indexOf(file) === -1) {
886
+ isArgsMatch(fileName, args, () => {
887
+ if (Object.values(cmdMap).indexOf(file) === -1) {
864
888
  cmdMap[i] = file
865
889
  i ++
866
890
  }
867
- }
891
+ })
868
892
  }
869
893
  return cmdMap
870
894
  }
871
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
+
872
918
  function getArgComments(argDef) {
873
919
  let comments = []
874
920
  let keys = Object.keys(argDef)
@@ -879,15 +925,15 @@ function getArgComments(argDef) {
879
925
  comments.push(aLine.join(", ") + ", ")
880
926
  aLine = []
881
927
  }
882
- aLine.push(`${k}` + (argDef[k] ? `(${argDef[k]})`:''))
928
+ aLine.push(`\x1B[33m${k}\x1B[39m` + (argDef[k] ? ` <${argDef[k]}>`:''))
883
929
  }
884
930
  if (aLine.length > 0) {
885
931
  comments.push(aLine.join(", "))
886
932
  }
887
- comments[0] = 'ARGS: ' + comments[0]
933
+ comments[0] = '$args: ' + comments[0]
888
934
  if (comments.length > 1) {
889
935
  for(let i = 1; i< comments.length; i++) {
890
- comments[i] = ' ' + comments[i]
936
+ comments[i] = ' ' + comments[i]
891
937
  }
892
938
  }
893
939
  }
@@ -931,14 +977,9 @@ async function runCmd(str) {
931
977
  continue
932
978
  }
933
979
  if (args[name] === undefined || args[name] === null) {
934
- if (!exit) {
935
- let comments = getArgComments(argDef)
936
- if (comments.length > 0) {
937
- console.log(comments.join('\n'))
938
- }
939
- }
940
980
  exit = true
941
- warn(`require ${name}`)
981
+ let comment = trim(argDef[name]);
982
+ warn(`require \x1B[33m${name}\x1B[39m` + (comment ? ` <${comment}>`:''))
942
983
  }
943
984
  }
944
985
  if (exit) {
package/ethWeb.js CHANGED
@@ -1000,7 +1000,7 @@ async function batchTranfer(ethTranserWrite, token, froms, tos, min, max) {
1000
1000
  if (Number(fb) < max) {
1001
1001
  continue
1002
1002
  }
1003
- let transferAmt = max - Number(tb)
1003
+ let transferAmt = String(max - Number(tb))
1004
1004
  let fmtTransferAmt = exDcmNum(transferAmt, -decimals).toString()
1005
1005
  pros.push(ethTranserWrite(sender, token, {
1006
1006
  abi: erc20Abi,
@@ -1018,7 +1018,7 @@ async function batchTranfer(ethTranserWrite, token, froms, tos, min, max) {
1018
1018
  if (Number(fb) < max) {
1019
1019
  continue
1020
1020
  }
1021
- let transferAmt = max - Number(tb)
1021
+ let transferAmt = String(max - Number(tb))
1022
1022
  let fmtTransferAmt = exDcmNum(transferAmt, -decimals).toString()
1023
1023
  pros.push(ethTranserWrite(sender, toAddress, {
1024
1024
  value: transferAmt,
@@ -1138,14 +1138,14 @@ async function transferToken(ethTranserWrite, sender, address, token, amt, msg)
1138
1138
  }
1139
1139
  }
1140
1140
 
1141
- async function tokenApprove(ethSwapWrite, sender, senderAddress, erc20, spender, opt) {
1141
+ async function tokenApprove(ethWrite, sender, senderAddress, erc20, spender, opt) {
1142
1142
  if (!erc20) {
1143
1143
  return
1144
1144
  }
1145
1145
  let {amt = '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', msg} = opt
1146
1146
  let bal = await ethRead(erc20, erc20Abi, 'allowance', [senderAddress, spender])
1147
1147
  if (bal <Number(amt) ) {
1148
- await ethSwapWrite(sender, erc20, {
1148
+ await ethWrite(sender, erc20, {
1149
1149
  abi: erc20Abi,
1150
1150
  method: 'approve',
1151
1151
  args: [spender, amt],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jsir",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {