jsir 3.0.2 → 3.0.4
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 +99 -52
- package/deps/evalCode.js +8 -1
- package/deps/room.js +5 -2
- package/deps/util.js +5 -4
- package/package.json +5 -5
package/cmd/oaa.js
CHANGED
|
@@ -404,6 +404,10 @@ function closeRl() {
|
|
|
404
404
|
}
|
|
405
405
|
|
|
406
406
|
function _nextLine(callback, promptStr, hidden, resolve, reject, end, isText) {
|
|
407
|
+
if (isRunningInBackground()) {
|
|
408
|
+
reject("Unsupported Operation")
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
407
411
|
callback = callback || wrapperInput;
|
|
408
412
|
if (!setting.enableNextLine) {
|
|
409
413
|
console.$warn("NextLine Disabled");
|
|
@@ -469,6 +473,7 @@ function _nextLine(callback, promptStr, hidden, resolve, reject, end, isText) {
|
|
|
469
473
|
if (callback === wrapperInput) {
|
|
470
474
|
setting.promptId = Date.now();
|
|
471
475
|
setting.lastOutput = null;
|
|
476
|
+
terminalTitle();
|
|
472
477
|
}
|
|
473
478
|
}
|
|
474
479
|
|
|
@@ -1185,20 +1190,33 @@ const keywordDef = {
|
|
|
1185
1190
|
clear: {
|
|
1186
1191
|
comment: 'Clear task',
|
|
1187
1192
|
exeFn: async (args) => {
|
|
1188
|
-
if (args.length > 0
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1193
|
+
if (args.length > 0) {
|
|
1194
|
+
if (_cmdMap[args[0]]) {
|
|
1195
|
+
let cmdName = _cmdMap[args[0]];
|
|
1196
|
+
if (args.length === 1) {
|
|
1197
|
+
$data.del(cmdName)
|
|
1198
|
+
console.msg('remove metadata of', cmdName)
|
|
1199
|
+
} else {
|
|
1200
|
+
let metadata = $data.get(cmdName);
|
|
1201
|
+
if (!vl(metadata) || typeof metadata !== 'object') {
|
|
1202
|
+
return;
|
|
1203
|
+
}
|
|
1204
|
+
for (let key of args.slice(1)) {
|
|
1205
|
+
if (metadata.hasOwnProperty(key)) {
|
|
1206
|
+
delete metadata[key]
|
|
1207
|
+
console.msg('remove metadata of', cmdName, key)
|
|
1208
|
+
}
|
|
1209
|
+
}
|
|
1196
1210
|
}
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1211
|
+
} else if ('0' === args[0]) {
|
|
1212
|
+
for (let key of $data.key()) {
|
|
1213
|
+
if (setting.workspaceMap[key.split("/")[0]]) {
|
|
1214
|
+
$data.del(key)
|
|
1215
|
+
console.msg('remove metadata of', key)
|
|
1200
1216
|
}
|
|
1201
1217
|
}
|
|
1218
|
+
} else {
|
|
1219
|
+
await delTips(...args)
|
|
1202
1220
|
}
|
|
1203
1221
|
} else {
|
|
1204
1222
|
await delTips(...args)
|
|
@@ -1708,15 +1726,24 @@ const keywordDef = {
|
|
|
1708
1726
|
if (args.length > 0) {
|
|
1709
1727
|
let have = false;
|
|
1710
1728
|
let tasks = []
|
|
1729
|
+
let targets = []
|
|
1730
|
+
let inputArgs = []
|
|
1731
|
+
for (let arg of args) {
|
|
1732
|
+
if (inputArgs.length > 0 || !/^\d/.test(arg)) {
|
|
1733
|
+
inputArgs.push(arg)
|
|
1734
|
+
} else {
|
|
1735
|
+
targets.push(arg)
|
|
1736
|
+
}
|
|
1737
|
+
}
|
|
1711
1738
|
for (let room of setting.rooms) {
|
|
1712
1739
|
for (let pid of Object.keys(room.jsirs || {})) {
|
|
1713
|
-
if (
|
|
1740
|
+
if (targets.indexOf(room.selfNode) === -1 && targets.indexOf(pid) === -1) {
|
|
1714
1741
|
continue;
|
|
1715
1742
|
}
|
|
1716
1743
|
if (setting.selfRoom.selfNode === room.selfNode && String(process.pid) === pid) {
|
|
1717
1744
|
continue;
|
|
1718
1745
|
}
|
|
1719
|
-
if (setting.selfRoom.selfNode !== room.selfNode &&
|
|
1746
|
+
if (setting.selfRoom.selfNode !== room.selfNode && targets.includes(room.selfNode) && !targets.includes(pid)
|
|
1720
1747
|
&& room.jsirs[pid].port === setting.defaultPort) {
|
|
1721
1748
|
continue;
|
|
1722
1749
|
}
|
|
@@ -1729,7 +1756,8 @@ const keywordDef = {
|
|
|
1729
1756
|
})
|
|
1730
1757
|
}
|
|
1731
1758
|
}
|
|
1732
|
-
let
|
|
1759
|
+
let inputStr = trim(inputArgs.join(" "))
|
|
1760
|
+
let input = have ? (inputStr || await nextLine(i => i)):''
|
|
1733
1761
|
if (vl(input)) {
|
|
1734
1762
|
for (let task of tasks) {
|
|
1735
1763
|
try {
|
|
@@ -1824,11 +1852,11 @@ async function _dealKeyword(keyword, args) {
|
|
|
1824
1852
|
let shortKey = keyword === key ? item.short : keyword;
|
|
1825
1853
|
if (item.short === shortKey) {
|
|
1826
1854
|
unMatched = false;
|
|
1855
|
+
console.$log('Execute', key)
|
|
1856
|
+
terminalTitle(key)
|
|
1827
1857
|
if (item === keywordDef.run) {
|
|
1828
|
-
console.$log('Execute', key)
|
|
1829
1858
|
await item.exeFn(args);
|
|
1830
1859
|
} else {
|
|
1831
|
-
console.$log('Execute', key)
|
|
1832
1860
|
await item.exeFn(args.map(trim));
|
|
1833
1861
|
}
|
|
1834
1862
|
break;
|
|
@@ -2133,6 +2161,31 @@ async function runScript(uniqueName, oriArgs) {
|
|
|
2133
2161
|
return await evalText(text, uniqueName, scriptArgs)
|
|
2134
2162
|
}
|
|
2135
2163
|
|
|
2164
|
+
function verifyArgs(scriptArgs = {}, argDef, _uniqueName) {
|
|
2165
|
+
let argNames = Object.keys(argDef)
|
|
2166
|
+
let argAbsent = false
|
|
2167
|
+
let scriptArgKeys = Object.keys(scriptArgs);
|
|
2168
|
+
for (let name of argNames) {
|
|
2169
|
+
let defStr = argDef[name]
|
|
2170
|
+
let beginIdx = defStr.indexOf("^");
|
|
2171
|
+
let endIdx = defStr.indexOf("$");
|
|
2172
|
+
let reg
|
|
2173
|
+
if (beginIdx !== -1 && endIdx !== -1 && beginIdx < endIdx) {
|
|
2174
|
+
reg = new RegExp(defStr.substring(beginIdx, endIdx + 1));
|
|
2175
|
+
}
|
|
2176
|
+
if (name.startsWith("_") && scriptArgKeys.indexOf(name) === -1) {
|
|
2177
|
+
continue
|
|
2178
|
+
}
|
|
2179
|
+
if (scriptArgKeys.indexOf(name) === -1 || (reg && !reg.test(scriptArgs[name]))) {
|
|
2180
|
+
argAbsent = true
|
|
2181
|
+
console.warn(`require ${warnStr(name)}` + (defStr ? ` ${defStr}` : ''))
|
|
2182
|
+
}
|
|
2183
|
+
}
|
|
2184
|
+
if (argAbsent) {
|
|
2185
|
+
throw _uniqueName ? `invalid args for ${_uniqueName}`:'invalid args';
|
|
2186
|
+
}
|
|
2187
|
+
}
|
|
2188
|
+
|
|
2136
2189
|
async function getScriptArgs(argDef, oriArgs) {
|
|
2137
2190
|
let shortDefMap = getShortDefMap(Object.keys(argDef))
|
|
2138
2191
|
oriArgs = oriArgs.map(i => shortDefMap[i] ? shortDefMap[i]:i)
|
|
@@ -2184,27 +2237,7 @@ async function getScriptArgs(argDef, oriArgs) {
|
|
|
2184
2237
|
for (let key of Object.keys(exactArgs)) {
|
|
2185
2238
|
scriptArgs[key] = exactArgs[key]
|
|
2186
2239
|
}
|
|
2187
|
-
|
|
2188
|
-
let scriptArgKeys = Object.keys(scriptArgs);
|
|
2189
|
-
for(let name of argNames) {
|
|
2190
|
-
let defStr = argDef[name]
|
|
2191
|
-
let beginIdx = defStr.indexOf("^");
|
|
2192
|
-
let endIdx = defStr.indexOf("$");
|
|
2193
|
-
let reg
|
|
2194
|
-
if (beginIdx !== -1 && endIdx !== -1 && beginIdx < endIdx) {
|
|
2195
|
-
reg = new RegExp(defStr.substring(beginIdx, endIdx + 1));
|
|
2196
|
-
}
|
|
2197
|
-
if (name.startsWith("_") && scriptArgKeys.indexOf(name) === -1) {
|
|
2198
|
-
continue
|
|
2199
|
-
}
|
|
2200
|
-
if (scriptArgKeys.indexOf(name) === -1 || (reg && !reg.test(scriptArgs[name]))) {
|
|
2201
|
-
argAbsent = true
|
|
2202
|
-
console.warn(`require ${warnStr(name)}` + (defStr ? ` ${defStr}`:''))
|
|
2203
|
-
}
|
|
2204
|
-
}
|
|
2205
|
-
if (argAbsent) {
|
|
2206
|
-
throw 'invalid args';
|
|
2207
|
-
}
|
|
2240
|
+
verifyArgs(scriptArgs, argDef);
|
|
2208
2241
|
return scriptArgs;
|
|
2209
2242
|
}
|
|
2210
2243
|
|
|
@@ -2286,7 +2319,7 @@ function parseArgDef(inputString) {
|
|
|
2286
2319
|
}
|
|
2287
2320
|
|
|
2288
2321
|
function getArgDef(text, uniqueName) {
|
|
2289
|
-
if (uniqueName && getJsirTypeKey(parseUniqueName(uniqueName)[1])
|
|
2322
|
+
if (uniqueName && ![setting.exeKey, setting.initKey].includes(getJsirTypeKey(parseUniqueName(uniqueName)[1]))) {
|
|
2290
2323
|
return {}
|
|
2291
2324
|
}
|
|
2292
2325
|
if (text.indexOf("$defArgs") === -1) {
|
|
@@ -2350,7 +2383,7 @@ function filterRequire(currSpace, cmdMatchStr) {
|
|
|
2350
2383
|
return cmds[0];
|
|
2351
2384
|
}
|
|
2352
2385
|
|
|
2353
|
-
async function _requireSource(currSpace, cmdMatchStr, force = false) {
|
|
2386
|
+
async function _requireSource(currSpace, cmdMatchStr, force = false, $args) {
|
|
2354
2387
|
let result
|
|
2355
2388
|
let uniqueName = filterRequire(currSpace, cmdMatchStr);
|
|
2356
2389
|
let path = getFullPath(uniqueName)
|
|
@@ -2364,19 +2397,29 @@ async function _requireSource(currSpace, cmdMatchStr, force = false) {
|
|
|
2364
2397
|
} else {
|
|
2365
2398
|
if (setting.serviceReg.test(uniqueName)) {
|
|
2366
2399
|
let serviceKey = `${pair[0]}/${trimJsirFileName(pair[1])}`
|
|
2367
|
-
|
|
2368
|
-
|
|
2369
|
-
|
|
2370
|
-
|
|
2371
|
-
|
|
2400
|
+
return new Proxy({}, {
|
|
2401
|
+
get(target, prop) {
|
|
2402
|
+
if (prop === 'then') {
|
|
2403
|
+
return undefined;
|
|
2404
|
+
}
|
|
2405
|
+
let fns = setting.services[serviceKey]
|
|
2406
|
+
if (!fns) {
|
|
2407
|
+
throw `invoke ${prop} failed cause service ${serviceKey} not found`;
|
|
2408
|
+
}
|
|
2409
|
+
if (!fns.includes(prop)) {
|
|
2410
|
+
throw `service function ${serviceKey}/${prop} not found`;
|
|
2411
|
+
}
|
|
2412
|
+
return async (...args) => {
|
|
2413
|
+
return await Room.reqFn(`${serviceKey}/${prop}`, args);
|
|
2372
2414
|
}
|
|
2373
2415
|
}
|
|
2374
|
-
|
|
2375
|
-
} else {
|
|
2376
|
-
throw `service ${serviceKey} not found`;
|
|
2377
|
-
}
|
|
2416
|
+
})
|
|
2378
2417
|
} else {
|
|
2379
|
-
|
|
2418
|
+
let argDef = getArgDef(text)
|
|
2419
|
+
if (Object.keys(argDef).length > 0) {
|
|
2420
|
+
verifyArgs($args, argDef, uniqueName);
|
|
2421
|
+
}
|
|
2422
|
+
result = await evalText(text, uniqueName, $args)
|
|
2380
2423
|
}
|
|
2381
2424
|
}
|
|
2382
2425
|
} else if (typeKey === setting.exeKey) {
|
|
@@ -2384,7 +2427,11 @@ async function _requireSource(currSpace, cmdMatchStr, force = false) {
|
|
|
2384
2427
|
return await runCmd(trim(argsStr), uniqueName, text)
|
|
2385
2428
|
}
|
|
2386
2429
|
} else {
|
|
2387
|
-
|
|
2430
|
+
if (path.endsWith(".json")) {
|
|
2431
|
+
result = JSON.parse(text);
|
|
2432
|
+
} else {
|
|
2433
|
+
result = text;
|
|
2434
|
+
}
|
|
2388
2435
|
}
|
|
2389
2436
|
if (!vl(result)) {
|
|
2390
2437
|
throw `invalid result: ${cmdMatchStr}`
|
|
@@ -2504,8 +2551,8 @@ async function evalText($text = '', $cmdName = '', $args = {}) {
|
|
|
2504
2551
|
}
|
|
2505
2552
|
return result
|
|
2506
2553
|
}
|
|
2507
|
-
let $require = async (matchItem) => {
|
|
2508
|
-
return await _requireSource(currSpace, matchItem)
|
|
2554
|
+
let $require = async (matchItem, $args) => {
|
|
2555
|
+
return await _requireSource(currSpace, matchItem, false, $args)
|
|
2509
2556
|
}
|
|
2510
2557
|
return await evalCode(addErrorTag($text), $cmdName, $args,
|
|
2511
2558
|
$data,
|
package/deps/evalCode.js
CHANGED
|
@@ -16,11 +16,18 @@ module.exports = async ($text = '', $cmdName = '', $args = [],
|
|
|
16
16
|
|
|
17
17
|
// 兼容
|
|
18
18
|
$text = $lib.wrapperJsirText($text)
|
|
19
|
+
$text = $text.split("\n").map(i => {
|
|
20
|
+
const argPass = /\)\s*\|\|/
|
|
21
|
+
if (/\$require\s*\(/.test(i) && argPass.test(i)) {
|
|
22
|
+
return i.replace(argPass, ',').trimEnd() + ")"
|
|
23
|
+
}
|
|
24
|
+
return i
|
|
25
|
+
}).join("\n")
|
|
19
26
|
|
|
20
27
|
const $tips = $lib.tipFns;
|
|
21
|
-
const $errorTag = $lib.errorTag;
|
|
22
28
|
|
|
23
29
|
// 不在context中
|
|
30
|
+
const $errorTag = $lib.errorTag;
|
|
24
31
|
const $setTips = $lib.setTips;
|
|
25
32
|
const $delTips = $lib.delTips;
|
|
26
33
|
const $aop = $lib.aop;
|
package/deps/room.js
CHANGED
|
@@ -582,7 +582,10 @@ function busyPick(busyItems) {
|
|
|
582
582
|
}
|
|
583
583
|
|
|
584
584
|
async function reqFn(fnKey, args = []) {
|
|
585
|
-
let targets = setting.serviceFns[fnKey]
|
|
585
|
+
let targets = setting.serviceFns[fnKey]
|
|
586
|
+
if (!targets) {
|
|
587
|
+
throw `service function ${fnKey} instance not found`;
|
|
588
|
+
}
|
|
586
589
|
targets = targets
|
|
587
590
|
.filter(i => i.active)
|
|
588
591
|
.filter(i => !isDisableConnect(i.node, i.port));
|
|
@@ -590,7 +593,7 @@ async function reqFn(fnKey, args = []) {
|
|
|
590
593
|
if (targets.length > 0) {
|
|
591
594
|
target = busyPick(targets);
|
|
592
595
|
} else {
|
|
593
|
-
throw `service function ${fnKey} not
|
|
596
|
+
throw `service function ${fnKey} instance not enable`;
|
|
594
597
|
}
|
|
595
598
|
let resp = await reqNode(target.node, 'post',
|
|
596
599
|
`/${fnKey}`, target.port, JSON.stringify(args || []))
|
package/deps/util.js
CHANGED
|
@@ -6,7 +6,7 @@ const fs = require('fs')
|
|
|
6
6
|
const fp = require('fs').promises
|
|
7
7
|
const BigNumber = require('bignumber.js');
|
|
8
8
|
const crypto = require('crypto');
|
|
9
|
-
const
|
|
9
|
+
const dayjs = require('dayjs')
|
|
10
10
|
const table = require('console.table')
|
|
11
11
|
const initModulePaths = Object.assign([], module.paths)
|
|
12
12
|
const path = require('path')
|
|
@@ -678,7 +678,7 @@ function getFileOpenExe() {
|
|
|
678
678
|
}
|
|
679
679
|
|
|
680
680
|
function timeStr(fmt, date) {
|
|
681
|
-
return
|
|
681
|
+
return dayjs(date || new Date()).format(fmt || 'YYYY-MM-DD HH:mm:ss')
|
|
682
682
|
}
|
|
683
683
|
|
|
684
684
|
function vl(obj) {
|
|
@@ -2065,8 +2065,9 @@ function getTextComments(text) {
|
|
|
2065
2065
|
function wrapperJsirText(text) {
|
|
2066
2066
|
return text
|
|
2067
2067
|
.replace(/^require\s*\(\s*(["'`][ei][^a-zA-Z]+)/mg, 'await $require($1')
|
|
2068
|
-
.replace(/([\s=;]require\s*\(\s*["'`])\.\/([ei][^a-zA-Z]+)/g, '$1$2')
|
|
2068
|
+
// .replace(/([\s=;]require\s*\(\s*["'`])\.\/([ei][^a-zA-Z]+)/g, '$1$2')
|
|
2069
2069
|
.replace(/([\s=;])require\s*\(\s*(["'`][ei][^a-zA-Z]+)/g, '$1await $require($2')
|
|
2070
|
+
.replace(/([\s=;])require\s*\(\s*(["'`])\.\//g, '$1await $require($2')
|
|
2070
2071
|
.replace(/([\s=;])import\s*\(/g, '$1$import(')
|
|
2071
2072
|
.replace(/^import\s*\(/mg, '$import(')
|
|
2072
2073
|
.replace(/^module\.exports\s*=\s*/m, 'return ')
|
|
@@ -2280,7 +2281,7 @@ module.exports = {
|
|
|
2280
2281
|
removeFirst,
|
|
2281
2282
|
sleep,
|
|
2282
2283
|
timeLimit,
|
|
2283
|
-
|
|
2284
|
+
dayjs,
|
|
2284
2285
|
setCbText,
|
|
2285
2286
|
randomInt,
|
|
2286
2287
|
vl,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jsir",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.4",
|
|
4
4
|
"description": "JavaScript Script Management Tool",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -18,9 +18,9 @@
|
|
|
18
18
|
"author": "",
|
|
19
19
|
"license": "ISC",
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"bignumber.js": "
|
|
22
|
-
"chokidar": "
|
|
23
|
-
"console.table": "
|
|
24
|
-
"dayjs": "
|
|
21
|
+
"bignumber.js": "9.0.0",
|
|
22
|
+
"chokidar": "3.5.2",
|
|
23
|
+
"console.table": "0.10.0",
|
|
24
|
+
"dayjs": "1.10.4"
|
|
25
25
|
}
|
|
26
26
|
}
|