jsir 2.0.3 → 2.0.5

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 (4) hide show
  1. package/cmd/oaa.js +47 -29
  2. package/evalCode.js +7 -15
  3. package/package.json +3 -26
  4. package/util.js +16 -88
package/cmd/oaa.js CHANGED
@@ -1,41 +1,57 @@
1
1
  #!/usr/bin/env node
2
+ const $lib = require('../util');
3
+ const path = require('path');
2
4
  const {
3
5
  getLibDataDir, trim, regEach, getConfig, mkdir, reget,
4
6
  getCbText, e, sleep, objDataFile, vl, md5, BigNumber,
5
7
  info, warn, error, arrayDataFile, infoStr, warnStr, errorStack,
6
8
  getInfo, ei, pad, msgStr, getType,
7
9
  errorTag, isArgsMatch, draftQuery, setConfig,
8
- $log, $draft, $config, getTextComments
9
- } = require('../util')
10
- const evalCode = require('../evalCode')
11
-
12
- const _types = {
13
- 'e': "exe",
14
- 'i': "init",
15
- 'f': "file"
10
+ $log, $draft, getTextComments, globalModulesPath
11
+ } = $lib;
12
+ const _args = process.argv.slice(2).map(trim)
13
+ let nodePath = trim(process.env.NODE_PATH)
14
+ let avoidFlag = path.delimiter + path.delimiter + path.delimiter;
15
+ if (!nodePath.endsWith(avoidFlag) && nodePath.indexOf(globalModulesPath) === -1) {
16
+ process.env.NODE_PATH = [globalModulesPath, nodePath].filter(i => i).join(path.delimiter) + avoidFlag
17
+ process.on('SIGINT', function () {});
18
+ ei(`jsir`, _args);
19
+ return;
16
20
  }
21
+
22
+ const evalCode = require('../evalCode')
17
23
  const _chokidar = require('chokidar');
24
+ const setting = require('../setting')
18
25
  const _fs = require('fs')
26
+ const readline = require("readline");
27
+
28
+ const _workspaceConfigFile = 'workspace.json';
19
29
  const _libDataDir = getLibDataDir()
20
- const _args = process.argv.slice(2).map(trim)
21
30
  const _history9 = []
22
31
  const _tipsOnRm = {}
23
- const readline = require("readline");
24
32
  const _fileWatcherMap = {}
25
- const setting = require('../setting')
33
+ const _types = {
34
+ 'e': "exe",
35
+ 'i': "init",
36
+ 'f': "file"
37
+ }
38
+ const $config = {
39
+ get: getConfig,
40
+ set: setConfig
41
+ }
42
+
26
43
  let _cmdMapFile = setting.name + 'CmdMap.json'
27
44
  let _cmdMap = {}
28
45
  let _rl
29
46
  let _rlHistory = []
30
47
  let _haveWrapperInput = true
31
- let _workspaceConfigFile = 'workspace.json';
32
48
  let _exit = false
33
49
 
34
50
  const _data = {}
35
51
  const $data = {
36
- get: (key, val) => {
37
- if (!(key in _data) && val !== undefined) {
38
- _data[key] = val
52
+ get: (key, defaultVal) => {
53
+ if (!(key in _data) && defaultVal !== undefined) {
54
+ _data[key] = defaultVal
39
55
  }
40
56
  return _data[key]
41
57
  },
@@ -48,7 +64,6 @@ const $data = {
48
64
  keys: () => Object.keys(_data)
49
65
  }
50
66
  const $homeDir = getLibDataDir()
51
- const $lib = {...require('../util')}
52
67
 
53
68
  global.$tips = {}
54
69
  global.$newInput = false
@@ -549,9 +564,9 @@ async function _wrapperInput(str) {
549
564
  await evalText(text)
550
565
  }
551
566
  } else if (str.match(/^\./)) {
552
- let argsStr = trim(str.substring(1))
553
- if (argsStr) {
554
- await dealKeyword(argsStr)
567
+ let items = enrichArgs(trim(str.substring(1))).map(trim)
568
+ if (items.length > 0) {
569
+ await dealKeyword(items[0], items.slice(1))
555
570
  } else {
556
571
  help()
557
572
  }
@@ -1012,17 +1027,14 @@ const keywordDef = {
1012
1027
  }
1013
1028
  }
1014
1029
 
1015
- async function dealKeyword(str) {
1030
+ async function dealKeyword(keyword, args) {
1016
1031
  let unMatched = true;
1017
1032
  for (let key of Object.keys(keywordDef)) {
1018
1033
  let item = keywordDef[key]
1019
- let currStr = str === key ? item.short : str.replace(new RegExp(`^${key}\\s+`), item.short + ' ')
1020
- let strs = trim(currStr).split(/\s+/)
1021
- let fstr = strs[0]
1022
- let ostr = strs.slice(1)
1023
- if (item.short === fstr) {
1034
+ let shortKey = keyword === key ? item.short : keyword;
1035
+ if (item.short === shortKey) {
1024
1036
  unMatched = false;
1025
- await item.exeFn(ostr);
1037
+ await item.exeFn(args);
1026
1038
  break;
1027
1039
  }
1028
1040
  }
@@ -1575,9 +1587,15 @@ async function evalText($text = '', $cmdName = '', $args = []) {
1575
1587
  return (await _requireSources(currSpace, matchItem, printLog))[0]
1576
1588
  }
1577
1589
  try {
1578
- return await evalCode($text, $cmdName, $args, $data, $require, $requires,
1579
- nextLine, nextText, setTips, delTips, wrapperInput, filterCmd,
1580
- currSpace, $log, $draft, $config, $homeDir, $lib)
1590
+ return await evalCode($text, $cmdName, $args,
1591
+ $data, $config,
1592
+ $require, $requires,
1593
+ nextLine, nextText,
1594
+ setTips, delTips,
1595
+ wrapperInput, filterCmd,
1596
+ currSpace,
1597
+ $log, $draft,
1598
+ $homeDir, $lib)
1581
1599
  } catch(e) {
1582
1600
  throw errorTag(e, $cmdName);
1583
1601
  }
package/evalCode.js CHANGED
@@ -1,33 +1,25 @@
1
- const {info: $info, msg: $msg, warn: $warn, error: $error,
2
- infoStr: $infoStr, msgStr: $msgStr, warnStr: $warnStr, errorStr: $errorStr,
3
- tableStr: $tableStr, nableStr: $nableStr,
4
- errorMsg: $errorMsg, errorStack: $errorStack,
5
- importG: $import} = require("./util");
6
- require = require("./util").requireG;
1
+ const {info: $info, msg: $msg, warn: $warn, error: $error, importG: $import} = require("./util");
2
+ require = require("./util").requireG
7
3
  module.exports = async ($text = '', $cmdName = '', $args = [],
8
- $data,
4
+ $data, $config,
9
5
  $require, $requires,
10
6
  $nextLine, $nextText,
11
7
  $setTips, $delTips,
12
8
  $enter, $filterCmd,
13
- $currentSpace, $log,
14
- $draft, $config,
9
+ $currentSpace,
10
+ $log, $draft,
15
11
  $homeDir, $lib) => {
16
12
  const $defArgs = () => $args;
17
13
  const $context = {
18
- global, $data,
14
+ global, $data, $config,
19
15
  $require, $requires, $import,
20
16
  $info, $msg, $warn, $error,
21
- $infoStr, $msgStr, $warnStr, $errorStr,
22
- $tableStr, $nableStr,
23
- $errorMsg, $errorStack,
24
17
  $text, $cmdName, $args,
25
18
  $nextLine, $nextText,
26
19
  $setTips, $delTips,
27
20
  $enter, $filterCmd,
28
21
  $currentSpace, $log,
29
- $draft, $config,
30
- $homeDir, $lib
22
+ $draft, $homeDir, $lib
31
23
  }
32
24
  return await eval(`(async ()=>{${$text};
33
25
  })()`)
package/package.json CHANGED
@@ -1,47 +1,24 @@
1
1
  {
2
2
  "name": "jsir",
3
- "version": "2.0.3",
3
+ "version": "2.0.5",
4
4
  "description": "js script manager tool",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
- "test": "node cmd/oaa.js --repl"
7
+ "test": "cmd/oaa.js"
8
8
  },
9
9
  "bin": {
10
- "jsir-see": "cmd/see.js",
11
- "jsir-sc": "cmd/solComplie.js",
12
- "jsir-sd": "cmd/solDeploy.js",
13
- "jsir-st": "cmd/solTest.js",
14
- "jsir-scd": "cmd/solCd.js",
15
- "jsir-stt": "cmd/solTt.js",
16
- "jsir-cleanLog": "cmd/cleanLog.js",
17
- "jsir-staticServer": "cmd/staticServer.js",
18
- "jsir-dirServer": "cmd/dirServer.js",
19
- "jsir-stop": "cmd/stop.js",
20
- "jsir-mock": "cmd/mockServer.js",
21
- "jsir-ethDataServer": "cmd/ethDataServer.js",
22
- "jsir-configServer": "cmd/configServer.js",
23
10
  "jsir": "cmd/oaa.js"
24
11
  },
25
12
  "keywords": [],
26
13
  "author": "",
27
14
  "license": "ISC",
28
15
  "dependencies": {
29
- "abi-decoder": "^2.4.0",
30
- "address": "^1.1.2",
31
16
  "axios": "^0.20.0",
32
17
  "bignumber.js": "^9.0.0",
33
18
  "chokidar": "^3.5.2",
34
19
  "console.table": "^0.10.0",
35
20
  "dayjs": "^1.10.4",
36
- "ethereumjs-tx": "^1.3.7",
37
- "ethereumjs-util": "^7.1.3",
38
- "ethers": "^5.1.0",
39
21
  "global-dirs": "^3.0.0",
40
- "keccak": "^3.0.2",
41
- "pad": "^3.2.0",
42
- "server": "^1.0.30",
43
- "urlencode": "^1.1.0",
44
- "web3": "^1.6.1",
45
- "web3-eth-contract": "^1.6.1"
22
+ "pad": "^3.2.0"
46
23
  }
47
24
  }
package/util.js CHANGED
@@ -37,57 +37,6 @@ const $draft = str => {
37
37
  }
38
38
  const $fnCache = {}
39
39
 
40
- const $config = {
41
- _buildId: null,
42
- build: async function () {
43
- if ($config._buildId == null) {
44
- await _buildConfig()
45
- }
46
- },
47
- close: () => {
48
- clearTimeout($config._buildId)
49
- $config._buildId = null
50
- },
51
- localConfig: {},
52
- remoteConfig: {},
53
- getLocal: (key, defaultVal) => {
54
- if (key && Object.keys($config.localConfig).indexOf(key) === -1) {
55
- getConfig(key)
56
- }
57
- return getVl($config.localConfig[key], defaultVal)
58
- },
59
- getRemote: (key, defaultVal) => {
60
- let machine = $config.getLocal('machine')
61
- let remoteKey = `${machine}${machine ? '-':''}${key}`
62
- return getVl($config.remoteConfig[remoteKey], $config.remoteConfig[key], defaultVal)
63
- },
64
- get: (key, defaultVal) => {
65
- return getFnVl(() => $config.getRemote(key),
66
- () => $config.getLocal(key), defaultVal)
67
- },
68
- set: (key, val) => {
69
- $config.localConfig[key] = val
70
- setConfig(key, val)
71
- }
72
- }
73
-
74
- async function _buildConfig() {
75
- try {
76
- $config.localConfig = getConfig()
77
- let remoteConfigIp = getConfig('remoteConfigIp')
78
-
79
- if (remoteConfigIp) {
80
- if (remoteConfigIp.indexOf(":") === -1) {
81
- remoteConfigIp = remoteConfigIp+":9563"
82
- }
83
- $config.remoteConfig = (await got.get(`http://${remoteConfigIp}`)).data || $config.remoteConfig
84
- } else {
85
- $config.remoteConfig = {}
86
- }
87
- } catch (e) {}
88
- $config._buildId = setTimeout(_buildConfig, 3000);
89
- }
90
-
91
40
  function isArgsMatch(text, args, callback, useMd5, md5Str) {
92
41
  let match = false
93
42
  for (let arg of args) {
@@ -389,7 +338,8 @@ function requireG(module){
389
338
  } catch (e) {}
390
339
  let path = globalDirectories.npm.packages + '/' + module;
391
340
  if (!fs.existsSync(path)) {
392
- throw `ERROR: ${module} not found, use [npm install -g ${module}] to install it`;
341
+ console.log(warnStr(`npm install -g ${module}`))
342
+ throw `${module} not found, use above cmd to install it`;
393
343
  }
394
344
  return require(path);
395
345
  }
@@ -401,9 +351,10 @@ async function importG(module) {
401
351
  return result
402
352
  }
403
353
  } catch (e) {}
404
- let path = globalDirectories.npm.packages + '/' + module + '/index.js';
354
+ let path = globalDirectories.npm.packages + '/' + module;
405
355
  if (!fs.existsSync(path)) {
406
- throw `ERROR: ${module} not found, use [npm install -g ${module}] to install it`;
356
+ console.log(warnStr(`npm install -g ${module}`))
357
+ throw `${module} not found, use above cmd to install it`;
407
358
  }
408
359
  return await import(path);
409
360
  }
@@ -449,31 +400,28 @@ function aesDecipher(str, key){
449
400
  }
450
401
 
451
402
  function getConfig(key, defaultVal) {
452
- let configInit = {}
453
- if (key) {
454
- configInit[key] = ''
403
+ if (typeof defaultVal === "string") {
404
+ defaultVal = trim(defaultVal)
455
405
  }
406
+ let configInit = {}
456
407
  let configFile = getLibDataDir() + '/config.json';
457
408
  if (!fs.existsSync(configFile)) {
458
409
  fs.writeFileSync(configFile, JSON.stringify(configInit, null, 2));
459
410
  }
460
411
 
461
412
  let config = JSON.parse(String(fs.readFileSync(configFile)))
462
- if (!key) {
413
+ if (key === undefined) {
463
414
  return config
464
415
  }
465
- let actConfigKeys = Object.keys(config);
466
416
  let writeFlag = false
467
- for (let k of Object.keys(configInit)) {
468
- if (actConfigKeys.indexOf(k) === -1) {
469
- writeFlag = true
470
- config[k] = ''
471
- }
417
+ if (!(key in config) && defaultVal !== undefined) {
418
+ writeFlag = true
419
+ config[key] = defaultVal
472
420
  }
473
421
  if (writeFlag) {
474
422
  fs.writeFileSync(configFile, JSON.stringify(config, null, 2))
475
423
  }
476
- let val = getVl(config[key], defaultVal)
424
+ let val = config[key]
477
425
  if (typeof val === "string") {
478
426
  val = trim(val)
479
427
  }
@@ -500,25 +448,6 @@ function setConfig(key, val) {
500
448
  return val
501
449
  }
502
450
 
503
- async function getUniConfig(key, defaultVal) {
504
- let remoteConfigIp = getConfig('remoteConfigIp')
505
- let val
506
- if (remoteConfigIp) {
507
- if (remoteConfigIp.indexOf(":") === -1) {
508
- remoteConfigIp = remoteConfigIp+":9563"
509
- }
510
- let machine = getConfig('machine')
511
- val = (await got.get(`http://${remoteConfigIp}/${machine}${machine ? '-':''}${key}`)).data;
512
- if (!vl(val)) {
513
- val = (await got.get(`http://${remoteConfigIp}/${key}`)).data;
514
- }
515
- }
516
- if (!vl(val)) {
517
- val = getConfig(key, defaultVal)
518
- }
519
- return val;
520
- }
521
-
522
451
  function bAdd(str1, str2) {
523
452
  return new BigNumber(String(str1)).plus(new BigNumber(String(str2))).toString()
524
453
  }
@@ -1255,6 +1184,7 @@ module.exports = {
1255
1184
  aesCipher,
1256
1185
  aesDecipher,
1257
1186
  requireG,
1187
+ importG,
1258
1188
  toBigNum,
1259
1189
  objDataFile,
1260
1190
  appendLog,
@@ -1267,7 +1197,6 @@ module.exports = {
1267
1197
  eFn,
1268
1198
  removeFirst,
1269
1199
  sleep,
1270
- getUniConfig,
1271
1200
  timeLimit,
1272
1201
  dayJs,
1273
1202
  setCbText,
@@ -1275,7 +1204,6 @@ module.exports = {
1275
1204
  vl,
1276
1205
  getVl,
1277
1206
  getFnVl,
1278
- $config,
1279
1207
  timeStr,
1280
1208
  splitArray,
1281
1209
  BigNumber,
@@ -1319,6 +1247,6 @@ module.exports = {
1319
1247
  iarrayDataFile,
1320
1248
  isArgsMatch,
1321
1249
  draftQuery,
1322
- importG,
1323
- getTextComments
1250
+ getTextComments,
1251
+ globalModulesPath: globalDirectories.npm.packages
1324
1252
  }