jsir 2.6.10 → 2.6.12

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 (3) hide show
  1. package/cmd/oaa.js +83 -28
  2. package/deps/util.js +11 -1
  3. package/package.json +1 -1
package/cmd/oaa.js CHANGED
@@ -13,7 +13,7 @@ const {
13
13
  getFullPath, parseUniqueName, toUniqueName, isJsirFileName, toJsirFileName,
14
14
  getAlias, wrapperJsirText, eia, getKeyTips, getValTips, getJsirTypeKey,
15
15
  createDetachedProcess, interceptStdStreams,
16
- draftModify, isRunningInBackground, fileJson, fileLock, processLock, cleanFileLocks
16
+ draftModify, isRunningInBackground, fileJson, fileLock, processLock, cleanFileLocks, getMd5Key
17
17
  } = $lib;
18
18
  const _args = process.argv.slice(2).map(trim);
19
19
  const evalCode = require('../deps/evalCode')
@@ -827,17 +827,28 @@ function dealStarGlobal(items, text) {
827
827
  }
828
828
 
829
829
  function wrapperClassLine(className, classLines) {
830
- let str = trimText(classLines.join('\n'))
830
+ let firstNoEmptyLine = classLines.find(i => i.trim()) || '';
831
+ if (!firstNoEmptyLine || firstNoEmptyLine.startsWith(firstNoEmptyLine.trim().substring(0, 1))) {
832
+ return classLines;
833
+ }
834
+ let spaceLen = firstNoEmptyLine.indexOf(firstNoEmptyLine.trim());
835
+ classLines = classLines.map(i => {
836
+ let prefix = i.substring(0, spaceLen);
837
+ if (prefix.trim()) {
838
+ return ''
839
+ } else {
840
+ return i.substring(spaceLen);
841
+ }
842
+ });
831
843
  let results = []
832
- for (let line of str.split('\n')) {
844
+ for (let line of classLines) {
833
845
  let asyncMethod = false;
834
846
  if (line.startsWith("async ")) {
835
847
  // 检查是否为异步方法
836
848
  asyncMethod = true;
837
849
  line = line.replace("async ", "").trim(); // 去除 'async ' 前缀
838
850
  }
839
- const methodMatch = line.match(/^\w+\s*\(/);
840
- if (methodMatch) {
851
+ if (/^[a-zA-Z_$][a-zA-Z0-9_$]*\s*\(/.test(line)) {
841
852
  let prefix = `function ${className}.`;
842
853
  if (asyncMethod) {
843
854
  prefix = "async " + prefix;
@@ -850,7 +861,7 @@ function wrapperClassLine(className, classLines) {
850
861
  return results;
851
862
  }
852
863
 
853
- function wrapperClass(lines) {
864
+ function wrapperClass(lines, wrapperFn, trimClass = true) {
854
865
  let results = [];
855
866
  let className = null;
856
867
  let inClass = false;
@@ -858,12 +869,19 @@ function wrapperClass(lines) {
858
869
  let classLines = []
859
870
  for (let line of lines) {
860
871
  if (line.startsWith("class ")) {
872
+ if (!trimClass) {
873
+ results.push(line);
874
+ }
861
875
  inClass = true;
862
- className = reget(line, /^class\s+(\w+)/);
876
+ className = reget(line, /^class\s+([a-zA-Z_$][a-zA-Z0-9_$]*)/);
863
877
  continue
864
- } else if (inClass && line.startsWith("}")) {
878
+ } else if (inClass && line.startsWith("}") && line.trim() === "}") {
865
879
  inClass = false
866
- results.push(...wrapperClassLine(className, classLines));
880
+ results.push(...wrapperFn(className, classLines));
881
+
882
+ if (!trimClass) {
883
+ results.push(line);
884
+ }
867
885
  classLines = []
868
886
  continue;
869
887
  }
@@ -879,7 +897,7 @@ function wrapperClass(lines) {
879
897
 
880
898
  function dealStarCmd(rows, cmd, filterStr) {
881
899
  let content = String(fs.readFileSync(getFullPath(cmd)));
882
- let lines = wrapperClass(content.split('\n'));
900
+ let lines = wrapperClass(content.split('\n'), wrapperClassLine);
883
901
  let capturingFunction = false;
884
902
  let inMultiLineComment = false;
885
903
  let potentialComments = [];
@@ -918,7 +936,7 @@ function dealStarCmd(rows, cmd, filterStr) {
918
936
 
919
937
  // Check for function start
920
938
  if ((line.startsWith('function ') || line.startsWith('async function '))) {
921
- let _fnName = reget(line, /function\s+([\w.]+)\s*\(/);
939
+ let _fnName = reget(line, /function\s+(\S+)\s*\(/);
922
940
  let matchKey = _fnName + "(" + getFnArgsStr(lines.slice(index, index + 9).join("\n")) + ")"
923
941
  if (isMatch(matchKey, filterStr)) {
924
942
  fnName = _fnName;
@@ -935,13 +953,13 @@ function dealStarCmd(rows, cmd, filterStr) {
935
953
  }
936
954
 
937
955
  // Check for function end and process
938
- if (fnName && line.startsWith("}") && capturingFunction) {
956
+ if (fnName && line.startsWith("}") && line.trim() === "}" && capturingFunction) {
939
957
  let functionContent = functionLines.join('\n');
940
958
  let commentContent = trimText(comments.join('\n'));
941
959
  let row = getInfo(functionContent, fnName, fnType);
942
960
  let pair = parseUniqueName(cmd);
943
961
  row.value = [
944
- infoStr(pair[0] + '/' + trimJsirFileName(pair[1])) + ' ' + getCmdMd5Key(parseUniqueName(cmd)[1]),
962
+ infoStr(pair[0] + '/' + trimJsirFileName(pair[1])) + ' ' + getMd5Key(parseUniqueName(cmd)[1]),
945
963
  [commentContent, row.value].filter(i => trim(i)).join("\n")
946
964
  ].filter(i => trim(i)).join("\n");
947
965
  rows.push(row);
@@ -1861,7 +1879,7 @@ function getScriptRequires(uniqueName) {
1861
1879
  }
1862
1880
 
1863
1881
  function getComments(i, cmdName, text, cols = [], col) {
1864
- let docLines = [getCmdMd5Key(parseUniqueName(cmdName)[1]) + " - " + i]
1882
+ let docLines = [getMd5Key(parseUniqueName(cmdName)[1]) + " - " + i]
1865
1883
  text = trim(text)
1866
1884
  docLines.push(...getTextComments(text))
1867
1885
  let argDef = getArgDef(text, cmdName)
@@ -2259,14 +2277,6 @@ function getArgDef(text, uniqueName) {
2259
2277
  return Object.assign(temp, tempOpt)
2260
2278
  }
2261
2279
 
2262
- function getCmdMd5Key(str) {
2263
- return '0x' + md5(str).substr(0, 8);
2264
- }
2265
-
2266
- function isMd5Key(str) {
2267
- return /^0x[a-z\d]{8}$/.test(str);
2268
- }
2269
-
2270
2280
  function filterRequire(currSpace, cmdMatchStr) {
2271
2281
  if (typeof cmdMatchStr === 'number') {
2272
2282
  cmdMatchStr = '0x' + pad(8, BigNumber(cmdMatchStr).toString(16), '0');
@@ -2337,17 +2347,60 @@ async function _requireSource(currSpace, cmdMatchStr, force = false) {
2337
2347
  return result
2338
2348
  }
2339
2349
 
2350
+ function _wrapperClassErrorTag(className, lines) {
2351
+ let firstNoEmptyLine = lines.find(i => i.trim()) || '';
2352
+ if (!firstNoEmptyLine || firstNoEmptyLine.startsWith(firstNoEmptyLine.trim().substring(0, 1))) {
2353
+ return lines;
2354
+ }
2355
+ let spaceLen = firstNoEmptyLine.indexOf(firstNoEmptyLine.trim());
2356
+ let prefixMap = {}
2357
+ let ignoreMap = {}
2358
+ lines = lines.map((i, index) => {
2359
+ let prefix = i.substring(0, spaceLen);
2360
+ if (prefix.trim()) {
2361
+ ignoreMap[index] = i;
2362
+ i = ''
2363
+ return i;
2364
+ } else {
2365
+ prefixMap[index] = prefix;
2366
+ i = i.substring(spaceLen);
2367
+ }
2368
+ if (/^[a-zA-Z_$][a-zA-Z0-9_$]*\s*\(/.test(i)) {
2369
+ i = `function ${className}.` + i;
2370
+ } else if (i.startsWith('async ')) {
2371
+ i = `async function ${className}.` + i.replace(/^async /, '');
2372
+ }
2373
+ return i;
2374
+ })
2375
+ lines = _addErrorTag(lines);
2376
+ return lines.map((i, index) => {
2377
+ if (i.startsWith('function ')) {
2378
+ i = i.replace(/^function [^.]+\./, '');
2379
+ } else if (i.startsWith('async function ')) {
2380
+ i = 'async ' + i.replace(/^async function [^.]+\./, '');
2381
+ }
2382
+ return (ignoreMap[index] || prefixMap[index]) + i;
2383
+ })
2384
+ }
2385
+
2340
2386
  function addErrorTag(text) {
2341
- let result = [];
2342
2387
  let lines = text.split(/\n/);
2388
+ lines = wrapperClass(lines, _wrapperClassErrorTag, false)
2389
+ let result= _addErrorTag(lines);
2390
+ return result.join('\n');
2391
+ }
2392
+
2393
+ function _addErrorTag(lines) {
2394
+ let result = [];
2343
2395
  let functionEnd = true;
2344
2396
  let fnName = '';
2397
+ let fnIsAsync = false;
2345
2398
  let wrapperFn = [];
2346
2399
  for (let i = 0; i < lines.length; i++) {
2347
2400
  let line = lines[i];
2348
2401
  if (line.startsWith("@")) {
2349
2402
  wrapperFn.push(line.substring(1).trim())
2350
- result.push(null);
2403
+ result.push('');
2351
2404
  } else {
2352
2405
  result.push(line);
2353
2406
  }
@@ -2355,28 +2408,30 @@ function addErrorTag(text) {
2355
2408
  let isSyncFn = line.startsWith('function');
2356
2409
  if (isAsyncFn || isSyncFn) {
2357
2410
  fnName = reget(line, /function\s+([\s\S]+)\s*\(/)
2411
+ fnIsAsync = isAsyncFn;
2358
2412
  }
2359
2413
  if (functionEnd && fnName && (/\)\s*\{$/.test(trim(line)) || (line.startsWith("{") && trim(line) === '{'))) {
2360
2414
  result[i] += 'try{';
2361
2415
  if (wrapperFn.length > 0) {
2362
2416
  let defaultArgsList = '("'+ fnName +'")';
2363
2417
  let runner = `([${wrapperFn.map(i => 'new ' + i + (i.endsWith(")") ? '':defaultArgsList)).join(",")}],`
2364
- if (isAsyncFn) {
2418
+ if (fnIsAsync) {
2365
2419
  result[i] += `return await $aopAsync${runner}async ()=>{`;
2366
- } else if (isSyncFn) {
2420
+ } else {
2367
2421
  result[i] += `return $aop${runner}()=>{`;
2368
2422
  }
2369
2423
  }
2370
2424
  functionEnd = false;
2371
2425
  }
2372
- if (line.startsWith("}") && !functionEnd) {
2426
+ if (line.startsWith("}") && line.trim() === "}" && !functionEnd) {
2373
2427
  result[i] = `${wrapperFn.length > 0 ? '},arguments)':''}}catch(e){throw($errorTag(e,$cmdName+'[${fnName}]'))}` + line;
2374
2428
  functionEnd = true;
2375
2429
  fnName = '';
2430
+ fnIsAsync = false;
2376
2431
  wrapperFn = []
2377
2432
  }
2378
2433
  }
2379
- return result.filter(i => i !== null).join('\n');
2434
+ return result;
2380
2435
  }
2381
2436
 
2382
2437
  async function evalText($text = '', $cmdName = '', $args = {}) {
package/deps/util.js CHANGED
@@ -2259,6 +2259,14 @@ async function aopAsync(items, fn, args) {
2259
2259
  return await runners[i](args)
2260
2260
  }
2261
2261
 
2262
+ function getMd5Key(str) {
2263
+ return '0x' + md5(str).substr(0, 8);
2264
+ }
2265
+
2266
+ function isMd5Key(str) {
2267
+ return /^0x[a-z\d]{8}$/.test(str);
2268
+ }
2269
+
2262
2270
  module.exports = {
2263
2271
  formatUptime,
2264
2272
  wrapperJsirText,
@@ -2377,5 +2385,7 @@ module.exports = {
2377
2385
  hasTips,
2378
2386
  tipKeys,
2379
2387
  isPidAlive,
2380
- cleanFileLocks
2388
+ cleanFileLocks,
2389
+ getMd5Key,
2390
+ isMd5Key
2381
2391
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jsir",
3
- "version": "2.6.10",
3
+ "version": "2.6.12",
4
4
  "description": "JavaScript Script Management Tool",
5
5
  "main": "index.js",
6
6
  "scripts": {