jsir 3.0.6 → 3.0.8

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/oaa.js CHANGED
@@ -192,7 +192,10 @@ function checkWorkspaces() {
192
192
  continue
193
193
  }
194
194
  if (fs.existsSync(workspace)) {
195
- setting.workspaceMap[space] = workspace;
195
+ let stat = fs.lstatSync(workspace)
196
+ if (stat.isDirectory()) {
197
+ setting.workspaceMap[space] = workspace;
198
+ }
196
199
  }
197
200
  }
198
201
  arrayDataFile(_workspaceConfigFile, () => Object.values(setting.workspaceMap))
@@ -667,13 +670,21 @@ async function wrapperInput(str, packOutput = false) {
667
670
  }
668
671
  }
669
672
 
670
- function wrapperAlias(str) {
673
+ function wrapperAlias(items) {
671
674
  let aliasMap = getConfig("alias");
672
675
  if (!aliasMap || Object.keys(aliasMap).length <= 0) {
673
- return str;
676
+ return items;
677
+ }
678
+ let result = []
679
+ for (let i = 0; i < items.length; i++) {
680
+ let item = items[i]
681
+ if (!item.endsWith(" ") && item.startsWith(".")) {
682
+ result.push(...enrichArgs(getAlias(aliasMap, item)))
683
+ } else {
684
+ result.push(item)
685
+ }
674
686
  }
675
- let fstr = str.split(/\s+/)[0];
676
- return [getAlias(aliasMap, fstr), str.substring(fstr.length)].map(trim).filter(i => i).join(' ')
687
+ return result;
677
688
  }
678
689
 
679
690
  async function _wrapperInput(str) {
@@ -683,7 +694,6 @@ async function _wrapperInput(str) {
683
694
  return;
684
695
  }
685
696
  setting.newError = false;
686
- str = wrapperAlias(str);
687
697
 
688
698
  if (/^[`'"]/.test(str)) {
689
699
  let fstr = str.substring(0, 1);
@@ -714,7 +724,7 @@ async function _wrapperInput(str) {
714
724
  await evalText(text)
715
725
  }
716
726
  } else if (str.match(/^\./)) {
717
- await dealKeyword(enrichArgs(str))
727
+ await dealKeyword(wrapperAlias(enrichArgs(str)))
718
728
  } else if (!str.split(/\s+/)[0].match(/^\d+$/)) {
719
729
  filterCmdAndList(str);
720
730
  } else {
@@ -1535,6 +1545,13 @@ const keywordDef = {
1535
1545
  let workspaces = Object.values(setting.workspaceMap)
1536
1546
  workspaces.push(newWorkspace)
1537
1547
  arrayDataFile(_workspaceConfigFile, () => workspaces)
1548
+ } else if (newWorkspace.startsWith("+")) {
1549
+ let name = newWorkspace.replace(/^\+\s*/, '');
1550
+ let path = setting.workspaceMap[name]
1551
+ if (path && name !== setting.defaultSpace) {
1552
+ let target = setting.workspaceMap[setting.defaultSpace] + '/' + name
1553
+ recreateSymlink(path, target)
1554
+ }
1538
1555
  } else {
1539
1556
  console.warn('invalid args')
1540
1557
  }
@@ -1610,7 +1627,7 @@ const keywordDef = {
1610
1627
  }
1611
1628
  }
1612
1629
  } else {
1613
- console.warn(`require "${keyword}" not unique`)
1630
+ console.warn(`require "${keyword}" not exist&unique`)
1614
1631
  }
1615
1632
  },
1616
1633
  args: {
@@ -1755,10 +1772,6 @@ const keywordDef = {
1755
1772
  if (setting.selfRoom.selfNode === room.selfNode && String(process.pid) === pid) {
1756
1773
  continue;
1757
1774
  }
1758
- if (setting.selfRoom.selfNode !== room.selfNode && targets.includes(room.selfNode) && !targets.includes(pid)
1759
- && room.jsirs[pid].port === setting.defaultPort) {
1760
- continue;
1761
- }
1762
1775
  tasks.push(async (input) => {
1763
1776
  let pidKey = `[${room.name} ${pid}]`
1764
1777
  if (taskPids.includes(pid)) {
@@ -2027,6 +2040,15 @@ function initWorkspace(space) {
2027
2040
  setting.defaultSpace = workspace;
2028
2041
  }
2029
2042
 
2043
+ function recreateSymlink(src, dest) {
2044
+ // 如果存在则删除(无论是文件还是软链)
2045
+ if (fs.existsSync(dest)) {
2046
+ fs.unlinkSync(dest);
2047
+ }
2048
+ // 重新创建软链
2049
+ fs.symlinkSync(src, dest, 'dir'); // dir/file 根据实际类型
2050
+ }
2051
+
2030
2052
  function filterCmdAndList(arg) {
2031
2053
  arg = trim(arg) || lastFilterArg;
2032
2054
  if (arg) {
@@ -2399,7 +2421,7 @@ function filterRequire(currSpace, cmdMatchStr) {
2399
2421
  }
2400
2422
  }
2401
2423
  if (cmds.length !== 1) {
2402
- throw `require "${cmdMatchStr}" not unique`
2424
+ throw `require "${cmdMatchStr}" not exist&unique`
2403
2425
  }
2404
2426
  return cmds[0];
2405
2427
  }
@@ -2431,7 +2453,7 @@ async function _requireSource(currSpace, cmdMatchStr, force = false, $args) {
2431
2453
  throw `service function ${serviceKey}/${prop} not found`;
2432
2454
  }
2433
2455
  return async (...args) => {
2434
- return await Room.reqFn(`${serviceKey}/${prop}`, args);
2456
+ return await Room.reqFn(`${serviceKey}/${prop}`, args, $args);
2435
2457
  }
2436
2458
  }
2437
2459
  })
package/deps/room.js CHANGED
@@ -44,11 +44,11 @@ function onRoom() {
44
44
  res.output = warnStr("Disable Remote")
45
45
  return;
46
46
  }
47
+ if (!isRunningInBackground()) {
48
+ res.output = warnStr("Disable Remote")
49
+ return;
50
+ }
47
51
  if (vl(req.input)) {
48
- if (!isRunningInBackground() && ['.nsir', '.N'].indexOf(req.input.trim()) === -1) {
49
- res.output = warnStr("Disable Remote")
50
- return;
51
- }
52
52
  res.output = await setting.wrapperInput(req.input, true)
53
53
  setting.promptId = Date.now()
54
54
  }
@@ -113,12 +113,11 @@ async function initNodes() {
113
113
  console.$debug("initNodes", e)
114
114
  }
115
115
  for (let line of resp.split("\n").map(trim)) {
116
- let offline = line.indexOf('offline') !== -1
117
116
  let ss = line.split(/\s+/);
118
117
  if (/^\d+\.\d+\.\d+\.\d+$/.test(ss[0])) {
119
118
  nodes[ss[0]] = {
120
119
  name: ss[1],
121
- offline,
120
+ offline: line.indexOf('offline') !== -1 ,
122
121
  account: ss[2]
123
122
  }
124
123
  }
@@ -253,9 +252,11 @@ async function _syncSetting(room) {
253
252
  }
254
253
  for (const pid of Object.keys(room.jsirs || {})) {
255
254
  let jsir = room.jsirs[pid];
256
- for (const service of Object.keys(jsir.services || {})) {
255
+ for (let service of Object.keys(jsir.services || {})) {
257
256
  let serviceFns = jsir.services[service] || [];
258
-
257
+ if (service.endsWith("+")) {
258
+ service = service.substring(0, service.length - 1)
259
+ }
259
260
  let existFns = setting.services[service] || [];
260
261
  existFns.push(...serviceFns)
261
262
  setting.services[service] = [...new Set(existFns)]
@@ -421,7 +422,12 @@ async function initRoomJsir(room) {
421
422
  let ss = key.split("/").map(trim).filter(i => i);
422
423
  if (ss.length > 3) {
423
424
  // 脚本空间service
424
- getOr(services, ss[1] + '/' + ss[2], []).push(ss[3])
425
+ let route = setting.routes[key]
426
+ let serviceKey = ss[1] + '/' + ss[2]
427
+ if (route.isApi) {
428
+ serviceKey = serviceKey + '+'
429
+ }
430
+ getOr(services, serviceKey, []).push(ss[3])
425
431
  } else if (ss.length > 2) {
426
432
  // jsir service
427
433
  getOr(services, ss[1], []).push(ss[2])
@@ -581,14 +587,24 @@ function busyPick(busyItems) {
581
587
  }
582
588
  }
583
589
 
584
- async function reqFn(fnKey, args = []) {
590
+ function isMatchTarget(target, node, port) {
591
+ if (!target) {
592
+ return true;
593
+ }
594
+ target = String(target).trim()
595
+ if (target.includes(":")) {
596
+ return target === `${node}:${port}`
597
+ } else {
598
+ return target === node;
599
+ }
600
+ }
601
+
602
+ async function reqFn(fnKey, args = [], reqTarget) {
585
603
  let targets = setting.serviceFns[fnKey]
586
604
  if (!targets) {
587
605
  throw `service function ${fnKey} instance not found`;
588
606
  }
589
- targets = targets
590
- .filter(i => i.active)
591
- .filter(i => !isDisableConnect(i.node, i.port));
607
+ targets = targets.filter(i => isMatchTarget(reqTarget, i.node, i.port) && i.active && !isDisableConnect(i.node, i.port));
592
608
  let target = null;
593
609
  if (targets.length > 0) {
594
610
  target = busyPick(targets);
package/deps/util.js CHANGED
@@ -333,6 +333,10 @@ function createConsole(uniqueName, _logToFile, _errorToFile) {
333
333
  }
334
334
  setting.lastChangeFlag = flag
335
335
  }
336
+ if (!is$ && uniqueName && quite && hasFlag) {
337
+ // ignore
338
+ return;
339
+ }
336
340
  if (is$ || (uniqueName && quite) || (setting.lastOutput && !hasFlag && 'debug' === key)) {
337
341
  if ((key === 'table' || key === 'nable') && typeof args[0] === "object") {
338
342
  args = ['', ...args]
@@ -2066,11 +2070,17 @@ function getTextComments(text) {
2066
2070
  function wrapperJsirText(text) {
2067
2071
  return text
2068
2072
  .replace(/^require\s*\(\s*(["'`][ei][^a-zA-Z]+)/mg, 'await $require($1')
2069
- // .replace(/([\s=;]require\s*\(\s*["'`])\.\/([ei][^a-zA-Z]+)/g, '$1$2')
2070
2073
  .replace(/([\s=;])require\s*\(\s*(["'`][ei][^a-zA-Z]+)/g, '$1await $require($2')
2074
+
2075
+ .replace(/^require\s*\(\s*(["'`][^/.]+\/[ei][^a-zA-Z]+)/mg, 'await $require($1')
2076
+ .replace(/([\s=;])require\s*\(\s*(["'`][^/.]+\/[ei][^a-zA-Z]+)/g, '$1await $require($2')
2077
+
2078
+ .replace(/^require\s*\(\s*(["'`])\.\//mg, 'await $require($1')
2071
2079
  .replace(/([\s=;])require\s*\(\s*(["'`])\.\//g, '$1await $require($2')
2072
- .replace(/([\s=;])import\s*\(/g, '$1$import(')
2080
+
2073
2081
  .replace(/^import\s*\(/mg, '$import(')
2082
+ .replace(/([\s=;])import\s*\(/g, '$1$import(')
2083
+
2074
2084
  .replace(/^module\.exports\s*=\s*/m, 'return ')
2075
2085
  ;
2076
2086
  }
@@ -2375,5 +2385,6 @@ module.exports = {
2375
2385
  currentJsir,
2376
2386
  currentRoom,
2377
2387
  currentRooms,
2378
- uid
2388
+ uid,
2389
+ consoleStrs
2379
2390
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jsir",
3
- "version": "3.0.6",
3
+ "version": "3.0.8",
4
4
  "description": "JavaScript Script Management Tool",
5
5
  "main": "index.js",
6
6
  "scripts": {