jsir 2.0.4 → 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 +8 -15
  3. package/package.json +1 -1
  4. package/util.js +28 -90
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,32 +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} = require("./util");
5
- require = require("./util").requireG;
1
+ const {info: $info, msg: $msg, warn: $warn, error: $error, importG: $import} = require("./util");
2
+ require = require("./util").requireG
6
3
  module.exports = async ($text = '', $cmdName = '', $args = [],
7
- $data,
4
+ $data, $config,
8
5
  $require, $requires,
9
6
  $nextLine, $nextText,
10
7
  $setTips, $delTips,
11
8
  $enter, $filterCmd,
12
- $currentSpace, $log,
13
- $draft, $config,
9
+ $currentSpace,
10
+ $log, $draft,
14
11
  $homeDir, $lib) => {
15
12
  const $defArgs = () => $args;
16
13
  const $context = {
17
- global, $data,
18
- $require, $requires,
14
+ global, $data, $config,
15
+ $require, $requires, $import,
19
16
  $info, $msg, $warn, $error,
20
- $infoStr, $msgStr, $warnStr, $errorStr,
21
- $tableStr, $nableStr,
22
- $errorMsg, $errorStack,
23
17
  $text, $cmdName, $args,
24
18
  $nextLine, $nextText,
25
19
  $setTips, $delTips,
26
20
  $enter, $filterCmd,
27
21
  $currentSpace, $log,
28
- $draft, $config,
29
- $homeDir, $lib
22
+ $draft, $homeDir, $lib
30
23
  }
31
24
  return await eval(`(async ()=>{${$text};
32
25
  })()`)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jsir",
3
- "version": "2.0.4",
3
+ "version": "2.0.5",
4
4
  "description": "js script manager tool",
5
5
  "main": "index.js",
6
6
  "scripts": {
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,11 +338,27 @@ 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
  }
396
346
 
347
+ async function importG(module) {
348
+ try {
349
+ let result = await import(module);
350
+ if (result) {
351
+ return result
352
+ }
353
+ } catch (e) {}
354
+ let path = globalDirectories.npm.packages + '/' + module;
355
+ if (!fs.existsSync(path)) {
356
+ console.log(warnStr(`npm install -g ${module}`))
357
+ throw `${module} not found, use above cmd to install it`;
358
+ }
359
+ return await import(path);
360
+ }
361
+
397
362
  function validStr(str, name) {
398
363
  if (!vl(str) || typeof str !== 'string') {
399
364
  throw "invalid cipher " + name;
@@ -434,11 +399,11 @@ function aesDecipher(str, key){
434
399
  }
435
400
  }
436
401
 
437
- function getConfig(key, defaultVal = '') {
438
- let configInit = {}
439
- if (key) {
440
- configInit[key] = ''
402
+ function getConfig(key, defaultVal) {
403
+ if (typeof defaultVal === "string") {
404
+ defaultVal = trim(defaultVal)
441
405
  }
406
+ let configInit = {}
442
407
  let configFile = getLibDataDir() + '/config.json';
443
408
  if (!fs.existsSync(configFile)) {
444
409
  fs.writeFileSync(configFile, JSON.stringify(configInit, null, 2));
@@ -448,23 +413,15 @@ function getConfig(key, defaultVal = '') {
448
413
  if (key === undefined) {
449
414
  return config
450
415
  }
451
- let actConfigKeys = Object.keys(config);
452
416
  let writeFlag = false
453
- for (let k of Object.keys(configInit)) {
454
- if (actConfigKeys.indexOf(k) === -1) {
455
- writeFlag = true
456
- config[k] = ''
457
- }
417
+ if (!(key in config) && defaultVal !== undefined) {
418
+ writeFlag = true
419
+ config[key] = defaultVal
458
420
  }
459
421
  if (writeFlag) {
460
422
  fs.writeFileSync(configFile, JSON.stringify(config, null, 2))
461
423
  }
462
- let val
463
- if (!(key in config)) {
464
- val = defaultVal
465
- } else {
466
- val = config[key]
467
- }
424
+ let val = config[key]
468
425
  if (typeof val === "string") {
469
426
  val = trim(val)
470
427
  }
@@ -491,25 +448,6 @@ function setConfig(key, val) {
491
448
  return val
492
449
  }
493
450
 
494
- async function getUniConfig(key, defaultVal) {
495
- let remoteConfigIp = getConfig('remoteConfigIp')
496
- let val
497
- if (remoteConfigIp) {
498
- if (remoteConfigIp.indexOf(":") === -1) {
499
- remoteConfigIp = remoteConfigIp+":9563"
500
- }
501
- let machine = getConfig('machine')
502
- val = (await got.get(`http://${remoteConfigIp}/${machine}${machine ? '-':''}${key}`)).data;
503
- if (!vl(val)) {
504
- val = (await got.get(`http://${remoteConfigIp}/${key}`)).data;
505
- }
506
- }
507
- if (!vl(val)) {
508
- val = getConfig(key, defaultVal)
509
- }
510
- return val;
511
- }
512
-
513
451
  function bAdd(str1, str2) {
514
452
  return new BigNumber(String(str1)).plus(new BigNumber(String(str2))).toString()
515
453
  }
@@ -1246,6 +1184,7 @@ module.exports = {
1246
1184
  aesCipher,
1247
1185
  aesDecipher,
1248
1186
  requireG,
1187
+ importG,
1249
1188
  toBigNum,
1250
1189
  objDataFile,
1251
1190
  appendLog,
@@ -1258,7 +1197,6 @@ module.exports = {
1258
1197
  eFn,
1259
1198
  removeFirst,
1260
1199
  sleep,
1261
- getUniConfig,
1262
1200
  timeLimit,
1263
1201
  dayJs,
1264
1202
  setCbText,
@@ -1266,7 +1204,6 @@ module.exports = {
1266
1204
  vl,
1267
1205
  getVl,
1268
1206
  getFnVl,
1269
- $config,
1270
1207
  timeStr,
1271
1208
  splitArray,
1272
1209
  BigNumber,
@@ -1310,5 +1247,6 @@ module.exports = {
1310
1247
  iarrayDataFile,
1311
1248
  isArgsMatch,
1312
1249
  draftQuery,
1313
- getTextComments
1250
+ getTextComments,
1251
+ globalModulesPath: globalDirectories.npm.packages
1314
1252
  }