@makano/rew 1.2.66 → 1.2.67
Sign up to get free protection for your applications and to get access to all the features.
- package/lib/rew/functions/match.js +13 -0
- package/lib/rew/models/struct.js +13 -3
- package/lib/rew/modules/context.js +10 -0
- package/package.json +1 -1
@@ -2,6 +2,8 @@ const SerializableData = ['string', 'number', 'boolean'];
|
|
2
2
|
|
3
3
|
const isRegExp = (obj) => Object.prototype.toString.call(obj) === '[object RegExp]';
|
4
4
|
|
5
|
+
const AnySymbol = Symbol('any');
|
6
|
+
|
5
7
|
module.exports.match = function match(value, templates, props) {
|
6
8
|
const matchProps = (pattern, value) => {
|
7
9
|
if (!props) return false;
|
@@ -23,6 +25,8 @@ module.exports.match = function match(value, templates, props) {
|
|
23
25
|
|
24
26
|
const entries = templates instanceof Map ? templates.entries() : Array.isArray(templates) ? templates : Object.entries(templates);
|
25
27
|
|
28
|
+
let any;
|
29
|
+
|
26
30
|
for (const [pattern, callback] of entries) {
|
27
31
|
if (
|
28
32
|
isRegExp(pattern)
|
@@ -36,8 +40,17 @@ module.exports.match = function match(value, templates, props) {
|
|
36
40
|
: matchProps(pattern, value)
|
37
41
|
) {
|
38
42
|
return callback(...(isRegExp(pattern) ? pattern.exec(value) : [value]));
|
43
|
+
} else if(pattern == AnySymbol){
|
44
|
+
any = callback;
|
39
45
|
}
|
40
46
|
}
|
41
47
|
|
48
|
+
if(any){
|
49
|
+
return any(value);
|
50
|
+
}
|
51
|
+
|
42
52
|
return null;
|
43
53
|
};
|
54
|
+
|
55
|
+
|
56
|
+
module.exports.match.any = AnySymbol;
|
package/lib/rew/models/struct.js
CHANGED
@@ -9,14 +9,17 @@ module.exports.struct = function struct(template) {
|
|
9
9
|
types[key] = typeof value;
|
10
10
|
}
|
11
11
|
|
12
|
-
const fun = function (properties = {}) {
|
12
|
+
const fun = function (properties = {}, extra) {
|
13
13
|
var defaultValue, instance;
|
14
14
|
instance = {};
|
15
15
|
for (key in template) {
|
16
16
|
defaultValue = template[key];
|
17
|
-
if
|
17
|
+
if(key.startsWith('@') && typeof template[key] == "function"){
|
18
|
+
const realname = key.slice(1);
|
19
|
+
instance[realname] = defaultValue(properties[realname]);
|
20
|
+
} else if (key in properties) {
|
18
21
|
value = properties[key];
|
19
|
-
if (defaultValue != '!any' && typeof value !== types[key]) {
|
22
|
+
if (defaultValue != '!any' && typeof value !== types[key] && types[key] !== '!any') {
|
20
23
|
throw new Error(`Type error: Expected ${types[key]} for ${key}, got ${typeof value}`);
|
21
24
|
}
|
22
25
|
instance[key] = value;
|
@@ -24,10 +27,17 @@ module.exports.struct = function struct(template) {
|
|
24
27
|
instance[key] = defaultValue == '!any' ? null : defaultValue;
|
25
28
|
}
|
26
29
|
}
|
30
|
+
if(typeof extra == "object"){
|
31
|
+
for(let i in extra){
|
32
|
+
instance[i] = extra[i];
|
33
|
+
}
|
34
|
+
}
|
27
35
|
instance.__proto__ = { '@instance': fun };
|
28
36
|
return instance;
|
29
37
|
};
|
30
38
|
|
39
|
+
fun.extends = (stuff) => struct({ ...template, ...stuff });
|
40
|
+
|
31
41
|
return fun;
|
32
42
|
};
|
33
43
|
|
@@ -5,6 +5,7 @@ const { imp } = require("../functions/import");
|
|
5
5
|
const { customRequire } = require("../functions/require");
|
6
6
|
const fsLib = require("../functions/fs");
|
7
7
|
const pathLib = require("../functions/path");
|
8
|
+
const path = require("path");
|
8
9
|
const execLib = require("../functions/exec");
|
9
10
|
const { findAppInfo } = require("../misc/findAppInfo");
|
10
11
|
const { USING_DEFAULT, Usage } = require("../const/usage");
|
@@ -60,6 +61,7 @@ module.exports.prepareContext = function (
|
|
60
61
|
env: process.env,
|
61
62
|
cwd: () => process.cwd(),
|
62
63
|
arch: process.arch,
|
64
|
+
pid: process.pid
|
63
65
|
};
|
64
66
|
|
65
67
|
context.global = context;
|
@@ -106,6 +108,14 @@ module.exports.prepareContext = function (
|
|
106
108
|
}
|
107
109
|
};
|
108
110
|
|
111
|
+
if(context.app?.config?.exec?.['auto import']){
|
112
|
+
const autoipath = path.join(context.app.path, context.app.config?.exec?.['auto import']);
|
113
|
+
if(autoipath !== filepath){
|
114
|
+
const all = context.imp(path.relative(path.dirname(filepath), autoipath));
|
115
|
+
for(let i in all) context[i] = all[i];
|
116
|
+
}
|
117
|
+
}
|
118
|
+
|
109
119
|
if (
|
110
120
|
context.module.main ||
|
111
121
|
(options.fromMain == true && options.as == "main")
|