jsir 1.3.0 → 1.3.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.
package/cmd/ooa.js CHANGED
@@ -3,7 +3,7 @@ const {
3
3
  run, getLibDataDir, trim, regEach, getConfig, mkdir, reget,
4
4
  getCbText, e, sleep, objDataFile, setConfig, vl, md5, BigNumber,
5
5
  info, warn, error, arrayDataFile, infoStr, warnStr, errorStack,
6
- getInfo, ei, pad, msgStr, getType, objProfileRows,
6
+ getInfo, ei, pad, msgStr, getType,
7
7
  errorTag, isArgsMatch, draftQuery
8
8
  } = require('../util')
9
9
  const evalCode = require('../evalCode')
@@ -118,13 +118,8 @@ function isMainCmd(mainList) {
118
118
  return mainList.indexOf(_args[0]) !== -1;
119
119
  }
120
120
 
121
- async function initRuntime() {
122
- global.$lib = {...require('../util'), evalText}
123
- let runtimeScript = 'e RUNTIME.js'
124
- let runtimePath = getLibDataDir() + '/ooa/' + runtimeScript
125
- if (_fs.existsSync(runtimePath)) {
126
- await runCmd('', runtimeScript)
127
- }
121
+ function initRuntime() {
122
+ global.$lib = {...require('../util')}
128
123
  }
129
124
 
130
125
  function getFileOpenExe(fileName) {
@@ -154,7 +149,7 @@ run(async () => {
154
149
  dealSourceCmds()
155
150
 
156
151
  if (isMainCmd(['repl'])) {
157
- await initRuntime()
152
+ initRuntime()
158
153
  _noAppendNextLine = false
159
154
  } else if (isMainCmd()){
160
155
  if (_noAppendNextLine) {
@@ -167,7 +162,7 @@ run(async () => {
167
162
  return
168
163
  }
169
164
  if (isMainCmd(['lib'])) {
170
- await initRuntime()
165
+ initRuntime()
171
166
  }
172
167
  if (isMainCmd(['add', 'note', 'init'])
173
168
  && (!_args[1] || /^\d+$/.test(_args[1]))) {
@@ -185,7 +180,7 @@ run(async () => {
185
180
  _args[0] = '@'
186
181
  }
187
182
  if (isMainCmd(['file'])) {
188
- await initRuntime()
183
+ initRuntime()
189
184
  }
190
185
  let info = _mainCmdMap[_args[0]]
191
186
  if (info) {
@@ -195,7 +190,7 @@ run(async () => {
195
190
  } else {
196
191
  await wrapperInput(`${_args[0]}${argStr}`)
197
192
  }
198
- } else if (_args[0] && _args[0].startsWith('--')) {
193
+ } else if (_args[0] && _args[0].startsWith('-')) {
199
194
  let cols = []
200
195
  for (let key of Object.keys(_mainCmdMap)) {
201
196
  cols.push({
@@ -216,7 +211,7 @@ run(async () => {
216
211
  }).join(' ').replace(/^@/, '')
217
212
  if (/^\d+$/.test(_args[0])) {
218
213
  if (_args[0] === '0' || (_cmdMap[_args[0]] && ['e ', 'f '].filter(i => _cmdMap[_args[0]].startsWith(i)).length > 0)) {
219
- await initRuntime()
214
+ initRuntime()
220
215
  }
221
216
  }
222
217
  await wrapperInput(line)
@@ -257,6 +252,10 @@ async function workFile(name) {
257
252
  name = 'workFile'
258
253
  }
259
254
  let fileName = `f ${toJsirFileName(name)}`
255
+ await watchFile(fileName)
256
+ }
257
+
258
+ async function watchFile(fileName) {
260
259
  let workFilePath = `${_home}/${fileName}`;
261
260
  if (!_fs.existsSync(workFilePath)) {
262
261
  _fs.writeFileSync(workFilePath, '');
@@ -272,7 +271,7 @@ async function workFile(name) {
272
271
  if (trim(exeStr)) {
273
272
  try {
274
273
  console.log("\n" + infoStr("------ workFile run ------"))
275
- await evalText(exeStr)
274
+ await wrapperInput("# " + exeStr)
276
275
  } catch (e) {
277
276
  error(e)
278
277
  }
@@ -303,6 +302,7 @@ function getExeStr(oldText, newText) {
303
302
  }
304
303
  let oldLine = olds[i];
305
304
  let newLine = news[i];
305
+
306
306
  if (reg.test(trim(newLine))) {
307
307
  if (flagChange) {
308
308
  break;
@@ -317,6 +317,9 @@ function getExeStr(oldText, newText) {
317
317
  currText = []
318
318
  } else {
319
319
  currText.push(newLine)
320
+ if (flagChange && trim(newLine).endsWith(";;")) {
321
+ break;
322
+ }
320
323
  }
321
324
 
322
325
  if (oldLine !== newLine) {
@@ -683,12 +686,7 @@ async function _wrapperInput(str) {
683
686
  let isStar = str.startsWith('*')
684
687
  let text = trim(str.replace(/^[$#*]/, ''))
685
688
  if (is$) {
686
- let result = await evalText('return ' + text)
687
- if (result === $lib) {
688
- console.table(objProfileRows(result))
689
- } else {
690
- console.log(result)
691
- }
689
+ console.log(await evalText('return ' + text))
692
690
  } else if (isStar) {
693
691
  let items = text.split(/\s+/).map(trim).filter(i => i)
694
692
  let result = await evalText('return ' + items[0])
@@ -924,8 +922,17 @@ async function dealKeyword(str, strs, fstr, ostr) {
924
922
  } else if (fstr === 'e' && ostr.length === 0) {
925
923
  hisToCmdMap()
926
924
  listCmd()
927
- } else if (fstr === 'f') {
928
- await workFile(trim(ostr.join(' ')))
925
+ } else if (/^f\d*$/.test(fstr)) {
926
+ if (fstr === 'f' ) {
927
+ await workFile(trim(ostr.join(' ')))
928
+ return;
929
+ }
930
+ let name = _cmdMap[trim(fstr.replace(/^f/, ''))]
931
+ if (!name) {
932
+ warn("no items")
933
+ } else {
934
+ await watchFile(name)
935
+ }
929
936
  } else if (fstr === '%') {
930
937
  let newRepo = trim(ostr[0])
931
938
  let currRepo = getConfig('jsLibSource');
@@ -1323,6 +1330,10 @@ async function _requireSource(cmdMatchStr, ignoreLog = false) {
1323
1330
  nullable = true;
1324
1331
  cmdMatchStr = trim(cmdMatchStr.substr(1));
1325
1332
  }
1333
+ if (_fs.existsSync(_home + '/' + cmdMatchStr)) {
1334
+ cmdMatchStr = '0x' + md5(cmdMatchStr).substr(0, 8);
1335
+ }
1336
+
1326
1337
  let cmdMap = filterCmd(cmdMatchStr.split(/\s+/))
1327
1338
  if (Object.keys(cmdMap).length !== 1) {
1328
1339
  if (nullable) {
@@ -1358,7 +1369,7 @@ async function _requireSource(cmdMatchStr, ignoreLog = false) {
1358
1369
  }
1359
1370
  throw `invalid returned: ${cmdMatchStr}`
1360
1371
  }
1361
- if (typeof result === 'function') {
1372
+ if (getType(result) === 'Function' || getType(result) === 'AsyncFunction') {
1362
1373
  let tmp = result;
1363
1374
  result = (...args) => {
1364
1375
  let resp
@@ -1400,10 +1411,14 @@ async function _requireSources(...matchItem) {
1400
1411
  return result
1401
1412
  }
1402
1413
 
1403
- function evalText($text = '', $cmdName = '', $args = []) {
1404
- return evalCode($text, $cmdName, $args,
1405
- _home, _cmdMap, _requireSources, $data,
1406
- nextLine, nextText, setTips, delTips, wrapperInput)
1414
+ async function evalText($text = '', $cmdName = '', $args = []) {
1415
+ try {
1416
+ return await evalCode($text, $cmdName, $args,
1417
+ _home, _cmdMap, _requireSources, $data,
1418
+ nextLine, nextText, setTips, delTips, wrapperInput, filterCmd)
1419
+ } catch(e) {
1420
+ throw errorTag(e, $cmdName);
1421
+ }
1407
1422
  }
1408
1423
 
1409
1424
  process.on('uncaughtException',function(err){
package/evalCode.js CHANGED
@@ -1,17 +1,14 @@
1
1
  const {info: $info, msg: $msg, warn: $warn, error: $error,
2
2
  infoStr: $infoStr, msgStr: $msgStr, warnStr: $warnStr, errorStr: $errorStr,
3
3
  tableStr: $tableStr, nableStr: $nableStr,
4
- errorMsg: $errorMsg, errorStack: $errorStack, errorTag: $errorTag} = require("./util");
4
+ errorMsg: $errorMsg, errorStack: $errorStack,
5
+ importG: $import} = require("./util");
5
6
  require = require("./util").requireG;
6
- module.exports = ($text = '', $cmdName = '', $args = [],
7
- $cmdDir, $cmdMap, $require, $data,
8
- $nextLine, $nextText, $setTips, $delTips,
9
- $enter) => {
7
+ module.exports = async ($text = '', $cmdName = '', $args = [],
8
+ $cmdDir, $cmdMap, $require, $data,
9
+ $nextLine, $nextText, $setTips, $delTips,
10
+ $enter, $filterCmd) => {
10
11
  const $defArgs = () => $args;
11
-
12
- return eval(`(async ()=>{try {${$text};
13
- } catch(e) {
14
- throw $errorTag(e, $cmdName);
15
- }
12
+ return await eval(`(async ()=>{${$text};
16
13
  })()`)
17
14
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jsir",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "description": "js script manager tool",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -14,7 +14,6 @@
14
14
  "jsir-scd": "cmd/solCd.js",
15
15
  "jsir-stt": "cmd/solTt.js",
16
16
  "jsir-cleanLog": "cmd/cleanLog.js",
17
- "jsir-ethPrivateHit": "cmd/ethPrivateHit.js",
18
17
  "jsir-staticServer": "cmd/staticServer.js",
19
18
  "jsir-dirServer": "cmd/dirServer.js",
20
19
  "jsir-stop": "cmd/stop.js",
@@ -40,9 +39,7 @@
40
39
  "ethers": "^5.1.0",
41
40
  "global-dirs": "^3.0.0",
42
41
  "keccak": "^3.0.2",
43
- "md5": "^2.3.0",
44
42
  "pad": "^3.2.0",
45
- "randomhex": "^0.1.5",
46
43
  "server": "^1.0.30",
47
44
  "urlencode": "^1.1.0",
48
45
  "web3": "^1.6.1",
package/util.js CHANGED
@@ -391,6 +391,20 @@ function requireG(module){
391
391
  return require(path);
392
392
  }
393
393
 
394
+ async function importG(module) {
395
+ try {
396
+ let result = await import(module);
397
+ if (result) {
398
+ return result
399
+ }
400
+ } catch (e) {}
401
+ let path = globalDirectories.npm.packages + '/' + module + '/index.js';
402
+ if (!fs.existsSync(path)) {
403
+ throw `ERROR: ${module} not found, use [npm install -g ${module}] to install it`;
404
+ }
405
+ return await import(path);
406
+ }
407
+
394
408
  function validStr(str, name) {
395
409
  if (!vl(str) || typeof str !== 'string') {
396
410
  throw "invalid cipher " + name;
@@ -929,6 +943,9 @@ function wrapRows(rows) {
929
943
  let temp = []
930
944
  for(let key of Object.keys(row)) {
931
945
  let val = row[key]
946
+ if (getType(val) !== "String") {
947
+ val = JSON.stringify(val, null, 2)
948
+ }
932
949
  let items = String(val).split('\n')
933
950
  for(let i = 0; i<items.length; i++) {
934
951
  let r = getOr(temp, i, {})
@@ -941,18 +958,30 @@ function wrapRows(rows) {
941
958
  }
942
959
 
943
960
  function info(msg) {
961
+ if (typeof msg === 'string' && msg && msg.indexOf('\n') === -1) {
962
+ msg = infoStr(msg)
963
+ }
944
964
  console.log(infoStr('[info]'), msg)
945
965
  }
946
966
 
947
967
  function msg(msg) {
968
+ if (typeof msg === 'string' && msg && msg.indexOf('\n') === -1) {
969
+ msg = msgStr(msg)
970
+ }
948
971
  console.log(msgStr('[msg]'), msg)
949
972
  }
950
973
 
951
974
  function warn(msg) {
975
+ if (typeof msg === 'string' && msg && msg.indexOf('\n') === -1) {
976
+ msg = warnStr(msg)
977
+ }
952
978
  console.warn(warnStr('[warn]'), msg)
953
979
  }
954
980
 
955
981
  function error(msg, tag) {
982
+ if (typeof msg === 'string' && msg && msg.indexOf('\n') === -1) {
983
+ msg = errorStr(msg)
984
+ }
956
985
  console.error(errorStr(`[${tag || 'error'}]`), msg)
957
986
  }
958
987
 
@@ -1015,7 +1044,7 @@ function errorMsg(e) {
1015
1044
 
1016
1045
  function getType(obj) {
1017
1046
  if (obj === '') {
1018
- return typeof obj;
1047
+ return 'String';
1019
1048
  }
1020
1049
  if (!vl(obj)) {
1021
1050
  return String(obj)
@@ -1236,5 +1265,6 @@ module.exports = {
1236
1265
  iobjDataFile,
1237
1266
  iarrayDataFile,
1238
1267
  isArgsMatch,
1239
- draftQuery
1268
+ draftQuery,
1269
+ importG
1240
1270
  }