@makano/rew 1.2.68 → 1.2.70
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/lib/rew/const/default.js
CHANGED
|
@@ -13,6 +13,7 @@ const { wait } = require('../functions/wait');
|
|
|
13
13
|
const { scheduleFrame } = require('../functions/misc');
|
|
14
14
|
const { jsons, yaml, json, yamls } = require('../functions/json');
|
|
15
15
|
const { generateRandomID } = require('../functions/id');
|
|
16
|
+
const { namespace } = require('./usage');
|
|
16
17
|
|
|
17
18
|
module.exports = {
|
|
18
19
|
cenum,
|
|
@@ -50,6 +51,8 @@ module.exports = {
|
|
|
50
51
|
yaml,
|
|
51
52
|
yamls,
|
|
52
53
|
|
|
54
|
+
namespace,
|
|
55
|
+
|
|
53
56
|
genID: generateRandomID,
|
|
54
57
|
|
|
55
58
|
curl,
|
package/lib/rew/const/usage.js
CHANGED
|
@@ -16,14 +16,31 @@ module.exports.USING_DEFAULT = {
|
|
|
16
16
|
|
|
17
17
|
module.exports.Usage = class Usage {
|
|
18
18
|
name = "null";
|
|
19
|
-
trigger = () => {}
|
|
19
|
+
trigger = () => {};
|
|
20
|
+
save = true;
|
|
20
21
|
|
|
21
|
-
constructor(name, trigger){
|
|
22
|
+
constructor(name, trigger, save){
|
|
22
23
|
this.name = name;
|
|
23
24
|
this.trigger = trigger;
|
|
25
|
+
this.save = save;
|
|
24
26
|
}
|
|
25
27
|
|
|
26
|
-
create(name, trigger){
|
|
27
|
-
return new Usage(name, trigger);
|
|
28
|
+
create(name, trigger, save = true){
|
|
29
|
+
return new Usage(name, trigger, save);
|
|
28
30
|
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
class Namespace extends module.exports.Usage {
|
|
34
|
+
namespace = {};
|
|
35
|
+
constructor(ns, cb){
|
|
36
|
+
super('namespace');
|
|
37
|
+
this.save = false;
|
|
38
|
+
this.trigger = cb;
|
|
39
|
+
this.namespace = ns;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
module.exports.Namespace = Namespace;
|
|
43
|
+
|
|
44
|
+
module.exports.namespace = (namespace, cb) => {
|
|
45
|
+
return new Namespace(namespace, cb);
|
|
29
46
|
}
|
|
@@ -5,9 +5,9 @@ const { wait } = require('./wait');
|
|
|
5
5
|
|
|
6
6
|
const cahcedRequires = {};
|
|
7
7
|
|
|
8
|
-
const doImp = (path) => wait(async () => await import(
|
|
8
|
+
const doImp = (path) => wait(async () => await import(path));
|
|
9
9
|
|
|
10
|
-
module.exports.customRequire = function customRequire(modulePath, filePath) {
|
|
10
|
+
module.exports.customRequire = function customRequire(modulePath, filePath, esm) {
|
|
11
11
|
let pathname = modulePath;
|
|
12
12
|
if (modulePath.startsWith('./') || modulePath.startsWith('../') || path.isAbsolute(modulePath)) {
|
|
13
13
|
pathname = path.resolve(modulePath);
|
|
@@ -17,7 +17,24 @@ module.exports.customRequire = function customRequire(modulePath, filePath) {
|
|
|
17
17
|
}
|
|
18
18
|
const resolvedPath = resolveModulePath(modulePath, filePath);
|
|
19
19
|
if(!resolvedPath) throw new Error('Module '+modulePath+' not found');
|
|
20
|
-
|
|
20
|
+
|
|
21
|
+
let isEsm = esm || false;
|
|
22
|
+
const get_file = () => cahcedRequires[resolvedPath] ? cahcedRequires[resolvedPath] : execOptions.useImport || isEsm ? doImp(resolvedPath) : require(resolvedPath);
|
|
23
|
+
|
|
24
|
+
let r;
|
|
25
|
+
|
|
26
|
+
try{
|
|
27
|
+
r = get_file();
|
|
28
|
+
} catch(e){
|
|
29
|
+
if(e.code === 'ERR_REQUIRE_ESM') {
|
|
30
|
+
isEsm = true;
|
|
31
|
+
console.log('Trying with esm');
|
|
32
|
+
r = get_file();
|
|
33
|
+
} else {
|
|
34
|
+
throw e;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
21
38
|
if(!cahcedRequires[resolvedPath]) cahcedRequires[resolvedPath] = r;
|
|
22
39
|
if(!cahcedRequires[pathname]) cahcedRequires[pathname] = r;
|
|
23
40
|
return r;
|
|
@@ -40,10 +57,13 @@ function resolveModulePath(modulePath, filePath) {
|
|
|
40
57
|
|
|
41
58
|
if (fs.existsSync(fullPath) && fs.statSync(fullPath).isDirectory()) {
|
|
42
59
|
return searchInPath(fullPath);
|
|
60
|
+
} else if (fs.existsSync(fullPath) && fs.statSync(fullPath).isFile()) {
|
|
61
|
+
return fullPath;
|
|
43
62
|
}
|
|
44
63
|
|
|
45
64
|
const rootPath = modulePath.split('/').shift();
|
|
46
65
|
const halfFullPath = path.join(basePath, rootPath);
|
|
66
|
+
|
|
47
67
|
if (fs.existsSync(halfFullPath) && fs.statSync(halfFullPath).isDirectory()) {
|
|
48
68
|
return searchInPath(halfFullPath, ['.'].concat(fullPath.split('/').slice(1)).join('/'));
|
|
49
69
|
}
|
|
@@ -61,8 +81,8 @@ function searchInPath(fullPath, exportses){
|
|
|
61
81
|
}
|
|
62
82
|
}
|
|
63
83
|
if(typeof main == "object"){
|
|
64
|
-
if(Array.isArray(main)) main = main[0].require;
|
|
65
|
-
else main = main.require;
|
|
84
|
+
if(Array.isArray(main)) main = execOptions.useImport ? main[0].import || main[0].require : main[0].require || main[0].import;
|
|
85
|
+
else main = execOptions.useImport ? main.import || main.require : main.require || main.import;
|
|
66
86
|
}
|
|
67
87
|
const mainPath = path.join(fullPath, main);
|
|
68
88
|
if (fs.existsSync(mainPath)) {
|
|
@@ -8,7 +8,8 @@ const pathLib = require("../functions/path");
|
|
|
8
8
|
const path = require("path");
|
|
9
9
|
const execLib = require("../functions/exec");
|
|
10
10
|
const { findAppInfo } = require("../misc/findAppInfo");
|
|
11
|
-
const { USING_DEFAULT, Usage } = require("../const/usage");
|
|
11
|
+
const { USING_DEFAULT, Usage, Namespace } = require("../const/usage");
|
|
12
|
+
const runtime = require("./runtime");
|
|
12
13
|
|
|
13
14
|
let mainFile = "";
|
|
14
15
|
const isMainFile = (filepath) => filepath == mainFile;
|
|
@@ -67,7 +68,8 @@ module.exports.prepareContext = function (
|
|
|
67
68
|
context.global = context;
|
|
68
69
|
context.imports.assert = options.import ?? {};
|
|
69
70
|
context.imp = imp(runPath, context);
|
|
70
|
-
context.
|
|
71
|
+
context.import = context.imp;
|
|
72
|
+
context.require = (package, esm) => {
|
|
71
73
|
try {
|
|
72
74
|
const search = execOptions.nativeRequire || package.startsWith("node:")
|
|
73
75
|
? require(
|
|
@@ -75,7 +77,7 @@ module.exports.prepareContext = function (
|
|
|
75
77
|
? package.split("node:")[1]
|
|
76
78
|
: package,
|
|
77
79
|
)
|
|
78
|
-
: customRequire(package, filepath);
|
|
80
|
+
: customRequire(package, filepath, esm);
|
|
79
81
|
if(!search) throw new Error("Module " + package + " not found");
|
|
80
82
|
return search;
|
|
81
83
|
} catch (e) {
|
|
@@ -88,8 +90,15 @@ module.exports.prepareContext = function (
|
|
|
88
90
|
throw new Error("");
|
|
89
91
|
return context.imp(package, asserts);
|
|
90
92
|
} catch (e) {
|
|
93
|
+
let pname = package.startsWith("pkg:") ? package.split("pkg:")[1] : package;
|
|
94
|
+
if(pname.endsWith('#esm')){
|
|
95
|
+
pname = pname.slice(0, -4);
|
|
96
|
+
if(!asserts) asserts = { esm: true };
|
|
97
|
+
else asserts.esm = true;
|
|
98
|
+
}
|
|
91
99
|
return context.require(
|
|
92
|
-
|
|
100
|
+
pname,
|
|
101
|
+
asserts?.esm
|
|
93
102
|
);
|
|
94
103
|
}
|
|
95
104
|
};
|
|
@@ -101,8 +110,16 @@ module.exports.prepareContext = function (
|
|
|
101
110
|
if(USING_DEFAULT[name].param) {
|
|
102
111
|
context.__using__[name] = USING_DEFAULT[name].param(...params);
|
|
103
112
|
}
|
|
104
|
-
}
|
|
105
|
-
|
|
113
|
+
} else if(name instanceof Namespace) {
|
|
114
|
+
const trigger = name.trigger;
|
|
115
|
+
const childContext = {...context, ...name.namespace, trigger};
|
|
116
|
+
childContext.currentNamespace = name.namespace;
|
|
117
|
+
childContext.parentNamespace = context;
|
|
118
|
+
const code = `(${trigger.toString()})()`;
|
|
119
|
+
runtime.exec(code, childContext, code, context.module.filepath);
|
|
120
|
+
} else if(name instanceof Usage) {
|
|
121
|
+
const v = name.trigger(...params) || true;
|
|
122
|
+
if(name.save !== false) context.__using__[name.name] = v;
|
|
106
123
|
} else {
|
|
107
124
|
context.__using__[name] = params.length ? params.length > 1 ? [...params] : params : true;
|
|
108
125
|
}
|
|
@@ -7,9 +7,9 @@ const path = require('path');
|
|
|
7
7
|
|
|
8
8
|
const preScript = readFileSync(path.join(__dirname, '../const/pre-exec.js'), { encoding: 'utf-8' });
|
|
9
9
|
|
|
10
|
-
const exec = (module.exports.exec = function (code, context, original = '') {
|
|
10
|
+
const exec = (module.exports.exec = function (code, context, original = '', filepath) {
|
|
11
11
|
return vm.runInNewContext(code, context.do ? null : vm.createContext(context), {
|
|
12
|
-
filename: context.module.filepath,
|
|
12
|
+
filename: filepath || context.module.filepath,
|
|
13
13
|
lineOffset: (original.split('\n').length + preScript.split('\n').length) - code.split('\n').length,
|
|
14
14
|
displayErrors: true,
|
|
15
15
|
});
|
package/package.json
CHANGED
package/runtime.d.ts
CHANGED
|
@@ -942,9 +942,12 @@ declare function using(fn: any, ...args: any[]): any;
|
|
|
942
942
|
declare function wait(fn: CallableFunction, ...args: any[]): any;
|
|
943
943
|
declare function clear(): void;
|
|
944
944
|
|
|
945
|
+
declare function namespace(object: any, callback: () => any): Usage;
|
|
946
|
+
|
|
945
947
|
declare class Usage<T = () => void> {
|
|
946
948
|
name: string;
|
|
947
949
|
trigger: T;
|
|
948
|
-
|
|
949
|
-
|
|
950
|
+
save: boolean;
|
|
951
|
+
constructor(name: string, trigger: T, save: boolean);
|
|
952
|
+
create(name: string, trigger: T, save: boolean): Usage;
|
|
950
953
|
}
|