jsir 2.2.7 → 2.2.9
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 +18 -12
- package/deps/evalCode.js +1 -3
- package/deps/example.js +4 -1
- package/deps/util.js +66 -15
- package/package.json +1 -1
package/cmd/oaa.js
CHANGED
|
@@ -11,7 +11,7 @@ const {
|
|
|
11
11
|
createConsole, setTips, delTips,
|
|
12
12
|
getEditor, errorStr, getConfigDir,
|
|
13
13
|
getFullPath, parseUniqueName, toUniqueName, isJsirFileName, toJsirFileName,
|
|
14
|
-
|
|
14
|
+
getOr, getAlias, wrapperJsirText
|
|
15
15
|
} = $lib;
|
|
16
16
|
const _args = process.argv.slice(2).map(trim);
|
|
17
17
|
const evalCode = require('../deps/evalCode')
|
|
@@ -1179,18 +1179,17 @@ const keywordDef = {
|
|
|
1179
1179
|
let text = removeComment(String(fs.readFileSync(path)))
|
|
1180
1180
|
let [space] = parseUniqueName(uniqueName)
|
|
1181
1181
|
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1182
|
+
text = wrapperJsirText(text)
|
|
1183
|
+
|
|
1184
|
+
await preLoad(text, "[\\s=;]require", requireG, packages, space)
|
|
1185
|
+
await preLoad(text, "^require", requireG, packages, space)
|
|
1186
|
+
await preLoad(text, "\\$import", importG, packages, space)
|
|
1187
1187
|
}
|
|
1188
1188
|
Object.keys(packages).forEach(key => {
|
|
1189
1189
|
let info = packages[key];
|
|
1190
1190
|
info.version = getPackageVersion(info.space, info.name)
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
info.loaded = loadedSpace
|
|
1191
|
+
if (global.$packages.hasOwnProperty(info.name)) {
|
|
1192
|
+
info.loaded = global.$packages[info.name]
|
|
1194
1193
|
}
|
|
1195
1194
|
if (info.loaded && info.loaded !== info.space) {
|
|
1196
1195
|
let version = getPackageVersion(info.loaded, info.name)
|
|
@@ -1284,7 +1283,7 @@ const keywordDef = {
|
|
|
1284
1283
|
let keyword = args[0]
|
|
1285
1284
|
let justList = false;
|
|
1286
1285
|
if (keyword.endsWith('.')) {
|
|
1287
|
-
let _tmp =
|
|
1286
|
+
let _tmp = getAlias(keyword.substring(0, keyword.length - 1));
|
|
1288
1287
|
if (_tmp) {
|
|
1289
1288
|
if (keywordDef[_tmp] || Object.values(keywordDef).map(item => item.short).indexOf(_tmp) !== -1) {
|
|
1290
1289
|
help(i => i === _tmp || keywordDef[i].short === _tmp);
|
|
@@ -1296,6 +1295,8 @@ const keywordDef = {
|
|
|
1296
1295
|
console.warn('invalid keyword')
|
|
1297
1296
|
return
|
|
1298
1297
|
}
|
|
1298
|
+
} else {
|
|
1299
|
+
keyword = getAlias(keyword)
|
|
1299
1300
|
}
|
|
1300
1301
|
let pair = parseUniqueName(keyword)
|
|
1301
1302
|
let cmds;
|
|
@@ -1362,7 +1363,9 @@ const keywordDef = {
|
|
|
1362
1363
|
let type = '';
|
|
1363
1364
|
for (let arg of args) {
|
|
1364
1365
|
if (_cmdMap[arg]) {
|
|
1365
|
-
|
|
1366
|
+
let pair = parseUniqueName(_cmdMap[arg])
|
|
1367
|
+
let fileName = pair[0] + '/' + pair[1].split(".")[0]
|
|
1368
|
+
path = `${getLibDataDir()}/log/${fileName}`
|
|
1366
1369
|
} else {
|
|
1367
1370
|
type = arg;
|
|
1368
1371
|
}
|
|
@@ -1380,7 +1383,7 @@ const keywordDef = {
|
|
|
1380
1383
|
console.warn('log file not found')
|
|
1381
1384
|
return;
|
|
1382
1385
|
}
|
|
1383
|
-
ei(cmdStr, [path], true)
|
|
1386
|
+
ei(cmdStr, [`"${path}"`], true)
|
|
1384
1387
|
},
|
|
1385
1388
|
short: 'l'
|
|
1386
1389
|
},
|
|
@@ -1506,6 +1509,9 @@ function removeComment(text) {
|
|
|
1506
1509
|
function _getScriptRequires(uniqueName, cmds, links) {
|
|
1507
1510
|
let path = getFullPath(uniqueName);
|
|
1508
1511
|
let text = removeComment(String(fs.readFileSync(path)))
|
|
1512
|
+
|
|
1513
|
+
text = wrapperJsirText(text);
|
|
1514
|
+
|
|
1509
1515
|
let temp = []
|
|
1510
1516
|
regEach(text, /\$require\s*\(([^()]+)\)/g, r=> {
|
|
1511
1517
|
if (r[1]) {
|
package/deps/evalCode.js
CHANGED
|
@@ -19,9 +19,7 @@ module.exports = async ($text = '', $cmdName = '', $args = [],
|
|
|
19
19
|
};
|
|
20
20
|
|
|
21
21
|
// 兼容
|
|
22
|
-
$text = $
|
|
23
|
-
$text = $text.replace(/([\s=;])import\s*\(/g, '$1$import(');
|
|
24
|
-
$text = $text.replace(/^import\s*\(/mg, '$import(');
|
|
22
|
+
$text = $lib.wrapperJsirText($text)
|
|
25
23
|
|
|
26
24
|
const $setTips = $lib.setTips;
|
|
27
25
|
const $delTips = $lib.delTips;
|
package/deps/example.js
CHANGED
package/deps/util.js
CHANGED
|
@@ -11,12 +11,14 @@ const crypto = require('crypto');
|
|
|
11
11
|
const dayJs = require('dayjs')
|
|
12
12
|
const table = require('console.table')
|
|
13
13
|
const initModulePaths = Object.assign([], module.paths)
|
|
14
|
+
const path = require('path')
|
|
14
15
|
|
|
15
16
|
global.$newInput = false
|
|
16
17
|
global.$workspaceMap = {}
|
|
17
18
|
global.$defaultSpace = 'local'
|
|
18
19
|
global.$newError = false
|
|
19
20
|
global.$tips = {}
|
|
21
|
+
global.$packages = {}
|
|
20
22
|
|
|
21
23
|
let libDataDir;
|
|
22
24
|
let lockDir;
|
|
@@ -197,10 +199,12 @@ function createConsole(uniqueName) {
|
|
|
197
199
|
result.$error = $error;
|
|
198
200
|
result.$draft = draftLog;
|
|
199
201
|
if (uniqueName) {
|
|
200
|
-
|
|
202
|
+
let pair = parseUniqueName(uniqueName)
|
|
203
|
+
let fileName = pair[0] + '/' + pair[1].split(".")[0]
|
|
204
|
+
result.$log = createLimitLogger(fileName + '.log', {
|
|
201
205
|
logInfo: false
|
|
202
206
|
});
|
|
203
|
-
result.$error = createLimitLogger(
|
|
207
|
+
result.$error = createLimitLogger(fileName + '.error', {
|
|
204
208
|
logInfo: false,
|
|
205
209
|
error: true
|
|
206
210
|
});
|
|
@@ -296,7 +300,7 @@ function toJsirFileName(name) {
|
|
|
296
300
|
}
|
|
297
301
|
|
|
298
302
|
function isJsirFileName(name) {
|
|
299
|
-
return /^[^./]*[^./\s]\.[^./\s]+$/.test(name)
|
|
303
|
+
return /^[^./]*[^./\s]\.[^./\s]+$/.test(name) && name.indexOf(".") > 1
|
|
300
304
|
}
|
|
301
305
|
|
|
302
306
|
function isMatch(text,
|
|
@@ -487,8 +491,14 @@ function trimEmptyLine(text) {
|
|
|
487
491
|
return lines.slice(start, end + 1).join('\n');
|
|
488
492
|
}
|
|
489
493
|
|
|
494
|
+
let defaultEditor = "vi";
|
|
490
495
|
function getEditor() {
|
|
491
|
-
|
|
496
|
+
try {
|
|
497
|
+
defaultEditor = getConfig("defaultEditor", "vi");
|
|
498
|
+
} catch (e) {
|
|
499
|
+
console.$error('getConfig failed', e);
|
|
500
|
+
}
|
|
501
|
+
return defaultEditor
|
|
492
502
|
}
|
|
493
503
|
|
|
494
504
|
function timeStr(fmt, date) {
|
|
@@ -597,6 +607,20 @@ function getLogDir() {
|
|
|
597
607
|
return logDir;
|
|
598
608
|
}
|
|
599
609
|
|
|
610
|
+
/**
|
|
611
|
+
* 根据文件完整路径,检查并创建其父级目录
|
|
612
|
+
* 示例:/a/b/c/d.txt -> 检查并创建 /a/b/c
|
|
613
|
+
* @param {string} filePath 文件的完整路径
|
|
614
|
+
*/
|
|
615
|
+
function createDirs(filePath) {
|
|
616
|
+
// 1. 获取父目录,比如 '/a/b/c'
|
|
617
|
+
const dirPath = path.dirname(filePath);
|
|
618
|
+
|
|
619
|
+
// 2. 使用 mkdirSync + { recursive: true } 来递归创建目录
|
|
620
|
+
// 如果目录已存在,则不会报错
|
|
621
|
+
fs.mkdirSync(dirPath, { recursive: true });
|
|
622
|
+
}
|
|
623
|
+
|
|
600
624
|
function createLimitLogger(fileName, {
|
|
601
625
|
maxChars = 49 * 1024 * 1024,
|
|
602
626
|
logInfo = true,
|
|
@@ -609,6 +633,7 @@ function createLimitLogger(fileName, {
|
|
|
609
633
|
}
|
|
610
634
|
let logDir = getLogDir()
|
|
611
635
|
let logPath = logDir + "/" + fileName
|
|
636
|
+
createDirs(logPath)
|
|
612
637
|
if (logInfo) {
|
|
613
638
|
console.info(`log: ${logPath}`)
|
|
614
639
|
}
|
|
@@ -749,7 +774,7 @@ async function fileJson(key, fn, fmt = true) {
|
|
|
749
774
|
}
|
|
750
775
|
|
|
751
776
|
function setModulePaths(space) {
|
|
752
|
-
let moduleDir = $workspaceMap[space
|
|
777
|
+
let moduleDir = $workspaceMap[space] + '/node_modules'
|
|
753
778
|
if (module.paths.indexOf(moduleDir) === -1) {
|
|
754
779
|
module.paths.splice(0, module.paths.length)
|
|
755
780
|
module.paths.push(moduleDir)
|
|
@@ -758,7 +783,14 @@ function setModulePaths(space) {
|
|
|
758
783
|
return moduleDir;
|
|
759
784
|
}
|
|
760
785
|
|
|
786
|
+
function enrichPackages(moduleName, space) {
|
|
787
|
+
if (!global.$packages.hasOwnProperty(moduleName)) {
|
|
788
|
+
global.$packages[moduleName] = space
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
|
|
761
792
|
function requireG(moduleName, space){
|
|
793
|
+
space = space || global.$defaultSpace;
|
|
762
794
|
let moduleDir = setModulePaths(space);
|
|
763
795
|
if (module.paths.indexOf(moduleDir) === -1) {
|
|
764
796
|
module.paths.splice(0, module.paths.length)
|
|
@@ -772,21 +804,15 @@ function requireG(moduleName, space){
|
|
|
772
804
|
} catch (e) {}
|
|
773
805
|
let path = moduleDir + '/' + moduleName;
|
|
774
806
|
if (!fs.existsSync(path)) {
|
|
775
|
-
throw `${space
|
|
807
|
+
throw `${space} package [${moduleName}] not found`;
|
|
776
808
|
}
|
|
777
809
|
let result = require(path);
|
|
778
810
|
enrichPackages(moduleName, space);
|
|
779
811
|
return result;
|
|
780
812
|
}
|
|
781
813
|
|
|
782
|
-
function enrichPackages(moduleName, space) {
|
|
783
|
-
let packages = getOr(global, '$packages', {});
|
|
784
|
-
if (!packages[moduleName]) {
|
|
785
|
-
packages[moduleName] = space
|
|
786
|
-
}
|
|
787
|
-
}
|
|
788
|
-
|
|
789
814
|
async function importG(moduleName, space) {
|
|
815
|
+
space = space || global.$defaultSpace;
|
|
790
816
|
let moduleDir = setModulePaths(space);
|
|
791
817
|
try {
|
|
792
818
|
let result = await import(moduleName);
|
|
@@ -795,7 +821,7 @@ async function importG(moduleName, space) {
|
|
|
795
821
|
} catch (e) {}
|
|
796
822
|
let path = moduleDir + '/' + moduleName;
|
|
797
823
|
if (!fs.existsSync(path)) {
|
|
798
|
-
throw `${space
|
|
824
|
+
throw `${space} package [${moduleName}] not found`;
|
|
799
825
|
}
|
|
800
826
|
let result = await import(require.resolve(path));
|
|
801
827
|
enrichPackages(moduleName, space);
|
|
@@ -855,6 +881,18 @@ function getConfigDir() {
|
|
|
855
881
|
return configDir;
|
|
856
882
|
}
|
|
857
883
|
|
|
884
|
+
function getAlias(key) {
|
|
885
|
+
key = trim(key)
|
|
886
|
+
if (!key) {
|
|
887
|
+
return key;
|
|
888
|
+
}
|
|
889
|
+
let aliasMap = getConfig('alias', {});
|
|
890
|
+
if (aliasMap.hasOwnProperty(key) && vl(aliasMap[key])) {
|
|
891
|
+
return aliasMap[key].trim();
|
|
892
|
+
}
|
|
893
|
+
return key
|
|
894
|
+
}
|
|
895
|
+
|
|
858
896
|
function _getConfig(key, defaultVal, uniqueName) {
|
|
859
897
|
let configInit = {}
|
|
860
898
|
let configFile = getLibDataDir() + '/config.json';
|
|
@@ -1758,7 +1796,18 @@ function getTextComments(text) {
|
|
|
1758
1796
|
return results.map(i => i.replace(/\s+$/, ''))
|
|
1759
1797
|
}
|
|
1760
1798
|
|
|
1799
|
+
function wrapperJsirText(text) {
|
|
1800
|
+
return text
|
|
1801
|
+
.replace(/^require\s*\(\s*(["'`][ei]\s+)/mg, 'await $require($2')
|
|
1802
|
+
.replace(/([\s=;])require\s*\(\s*(["'`][ei]\s+)/g, '$1await $require($2')
|
|
1803
|
+
.replace(/([\s=;])import\s*\(/g, '$1$import(')
|
|
1804
|
+
.replace(/^import\s*\(/mg, '$import(')
|
|
1805
|
+
.replace(/^module\.exports\s*=\s*/m, 'return ')
|
|
1806
|
+
;
|
|
1807
|
+
}
|
|
1808
|
+
|
|
1761
1809
|
module.exports = {
|
|
1810
|
+
wrapperJsirText,
|
|
1762
1811
|
run,
|
|
1763
1812
|
reget,
|
|
1764
1813
|
trim,
|
|
@@ -1852,5 +1901,7 @@ module.exports = {
|
|
|
1852
1901
|
isJsirFileName,
|
|
1853
1902
|
toJsirFileName,
|
|
1854
1903
|
fileJson,
|
|
1855
|
-
debugStr
|
|
1904
|
+
debugStr,
|
|
1905
|
+
getAlias,
|
|
1906
|
+
createDirs
|
|
1856
1907
|
}
|