koishi-plugin-rzgtboeyndxsklmq-commons 1.0.1
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/index.d.ts +8 -0
- package/lib/index.js +233 -0
- package/lib/utils/BeanHelper.d.ts +40 -0
- package/lib/utils/File.d.ts +1 -0
- package/package.json +35 -0
- package/readme.md +5 -0
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Context, Schema } from 'koishi';
|
|
2
|
+
export * from './utils/BeanHelper';
|
|
3
|
+
export * from './utils/File';
|
|
4
|
+
export declare const name = "rzgtboeyndxsklmq-commons";
|
|
5
|
+
export interface Config {
|
|
6
|
+
}
|
|
7
|
+
export declare const Config: Schema<Config>;
|
|
8
|
+
export declare function apply(ctx: Context, config: Config): void;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name2 in all)
|
|
10
|
+
__defProp(target, name2, { get: all[name2], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var src_exports = {};
|
|
32
|
+
__export(src_exports, {
|
|
33
|
+
BeanHelper: () => BeanHelper,
|
|
34
|
+
BeanType: () => BeanType,
|
|
35
|
+
Config: () => Config,
|
|
36
|
+
apply: () => apply,
|
|
37
|
+
name: () => name,
|
|
38
|
+
readDirFiles: () => readDirFiles
|
|
39
|
+
});
|
|
40
|
+
module.exports = __toCommonJS(src_exports);
|
|
41
|
+
var import_koishi = require("koishi");
|
|
42
|
+
|
|
43
|
+
// src/utils/BeanHelper.ts
|
|
44
|
+
var BeanType = class {
|
|
45
|
+
constructor(beanHelper) {
|
|
46
|
+
this.beanHelper = beanHelper;
|
|
47
|
+
this.ctx = beanHelper.getByName("ctx");
|
|
48
|
+
this.config = beanHelper.getByName("config");
|
|
49
|
+
return this;
|
|
50
|
+
}
|
|
51
|
+
static {
|
|
52
|
+
__name(this, "BeanType");
|
|
53
|
+
}
|
|
54
|
+
ctx;
|
|
55
|
+
config;
|
|
56
|
+
start() {
|
|
57
|
+
}
|
|
58
|
+
destroy() {
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
var BeanHelper = class _BeanHelper {
|
|
62
|
+
static {
|
|
63
|
+
__name(this, "BeanHelper");
|
|
64
|
+
}
|
|
65
|
+
static buildLazyProxyHandler(getObj, hooks) {
|
|
66
|
+
const handlerMap = {};
|
|
67
|
+
let needInit = true;
|
|
68
|
+
Reflect.ownKeys(Reflect).forEach((key) => {
|
|
69
|
+
handlerMap[key] = (target, ...args) => {
|
|
70
|
+
const obj = getObj();
|
|
71
|
+
if (needInit) {
|
|
72
|
+
needInit = false;
|
|
73
|
+
Reflect.ownKeys(obj).forEach((k) => target[k] = obj[k]);
|
|
74
|
+
Reflect.setPrototypeOf(target, Reflect.getPrototypeOf(obj));
|
|
75
|
+
}
|
|
76
|
+
if (key === "set") {
|
|
77
|
+
Reflect[key].apply(Reflect, [target, args[0], args[1]]);
|
|
78
|
+
} else if (key === "deleteProperty") {
|
|
79
|
+
Reflect[key].apply(Reflect, [target, args]);
|
|
80
|
+
}
|
|
81
|
+
let res;
|
|
82
|
+
if (key === "get") {
|
|
83
|
+
res = hooks?.get ? hooks.get(obj, args[0]) : Reflect[key].apply(Reflect, [obj, args[0]]);
|
|
84
|
+
if (typeof res === "function") {
|
|
85
|
+
res = res.bind(args[1]);
|
|
86
|
+
}
|
|
87
|
+
} else {
|
|
88
|
+
res = Reflect[key].apply(Reflect, [obj, ...args]);
|
|
89
|
+
}
|
|
90
|
+
return res;
|
|
91
|
+
};
|
|
92
|
+
});
|
|
93
|
+
return handlerMap;
|
|
94
|
+
}
|
|
95
|
+
static buildLazyProxy(getObj) {
|
|
96
|
+
return new Proxy({}, _BeanHelper.buildLazyProxyHandler(getObj));
|
|
97
|
+
}
|
|
98
|
+
static buildLazyRevocableProxy(getObj) {
|
|
99
|
+
return Proxy.revocable({}, _BeanHelper.buildLazyProxyHandler(getObj));
|
|
100
|
+
}
|
|
101
|
+
classPool = [];
|
|
102
|
+
ctx;
|
|
103
|
+
config;
|
|
104
|
+
constructor() {
|
|
105
|
+
this.proxy(() => this.ctx, "ctx");
|
|
106
|
+
this.proxy(() => this.config, "config");
|
|
107
|
+
}
|
|
108
|
+
setCtx(ctx, config) {
|
|
109
|
+
this.ctx = ctx;
|
|
110
|
+
this.config = config;
|
|
111
|
+
}
|
|
112
|
+
touchAll() {
|
|
113
|
+
let i = 0;
|
|
114
|
+
let len = 0;
|
|
115
|
+
while (len != this.classPool.length && i++ < 99999) {
|
|
116
|
+
len = this.classPool.length;
|
|
117
|
+
this.classPool.forEach((classInfo) => {
|
|
118
|
+
return classInfo?.proxy?.["__"];
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
async start() {
|
|
123
|
+
this.touchAll();
|
|
124
|
+
for (const classInfo of this.classPool) {
|
|
125
|
+
if (classInfo.proxy instanceof BeanType) {
|
|
126
|
+
await classInfo.proxy.start();
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
async destroy() {
|
|
131
|
+
for (const classInfo of this.classPool) {
|
|
132
|
+
if (classInfo.proxy instanceof BeanType) {
|
|
133
|
+
await classInfo.proxy.destroy();
|
|
134
|
+
}
|
|
135
|
+
classInfo.proxyRevoke?.();
|
|
136
|
+
}
|
|
137
|
+
this.classPool = null;
|
|
138
|
+
}
|
|
139
|
+
instance(clazz) {
|
|
140
|
+
let classInfo = this.classPool.find(
|
|
141
|
+
(classInfo2) => classInfo2.class === clazz
|
|
142
|
+
);
|
|
143
|
+
if (classInfo) {
|
|
144
|
+
return classInfo.proxy;
|
|
145
|
+
}
|
|
146
|
+
classInfo = {
|
|
147
|
+
name: clazz.name,
|
|
148
|
+
class: clazz,
|
|
149
|
+
instance: null,
|
|
150
|
+
proxy: null,
|
|
151
|
+
proxyRevoke: null
|
|
152
|
+
};
|
|
153
|
+
const proxyRevocable = _BeanHelper.buildLazyRevocableProxy(() => {
|
|
154
|
+
if (!classInfo.instance) {
|
|
155
|
+
classInfo.instance = new classInfo.class(this);
|
|
156
|
+
}
|
|
157
|
+
return classInfo.instance;
|
|
158
|
+
});
|
|
159
|
+
classInfo.proxy = proxyRevocable.proxy;
|
|
160
|
+
classInfo.proxyRevoke = proxyRevocable.revoke;
|
|
161
|
+
this.classPool.push(classInfo);
|
|
162
|
+
return classInfo.proxy;
|
|
163
|
+
}
|
|
164
|
+
proxy(getObj, name2) {
|
|
165
|
+
const proxyRevocable = _BeanHelper.buildLazyRevocableProxy(getObj);
|
|
166
|
+
const classInfo = {
|
|
167
|
+
name: name2,
|
|
168
|
+
proxy: proxyRevocable.proxy,
|
|
169
|
+
proxyRevoke: proxyRevocable.revoke
|
|
170
|
+
};
|
|
171
|
+
this.classPool.push(classInfo);
|
|
172
|
+
return classInfo.proxy;
|
|
173
|
+
}
|
|
174
|
+
getByName(name2) {
|
|
175
|
+
const classInfo = this.classPool.find(
|
|
176
|
+
(classInfo2) => classInfo2.name === name2
|
|
177
|
+
);
|
|
178
|
+
return classInfo.proxy || classInfo.instance;
|
|
179
|
+
}
|
|
180
|
+
put(instance, name2, clazz) {
|
|
181
|
+
if (!clazz) {
|
|
182
|
+
clazz = instance.constructor;
|
|
183
|
+
}
|
|
184
|
+
if (!name2) {
|
|
185
|
+
name2 = clazz.name;
|
|
186
|
+
}
|
|
187
|
+
this.classPool.push({
|
|
188
|
+
name: name2,
|
|
189
|
+
class: clazz,
|
|
190
|
+
instance
|
|
191
|
+
});
|
|
192
|
+
return instance;
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
// src/utils/File.ts
|
|
197
|
+
var import_promises = __toESM(require("node:fs/promises"));
|
|
198
|
+
var import_node_path = __toESM(require("node:path"));
|
|
199
|
+
async function readDirFiles(dirPath, needDir = false) {
|
|
200
|
+
const files = await import_promises.default.readdir(dirPath, {
|
|
201
|
+
withFileTypes: true
|
|
202
|
+
});
|
|
203
|
+
const paths = [];
|
|
204
|
+
files.sort((a, b) => (a.isFile() ? 1 : 0) - (b.isFile() ? 1 : 0));
|
|
205
|
+
for (const file of files) {
|
|
206
|
+
if (file.isDirectory()) {
|
|
207
|
+
if (needDir) {
|
|
208
|
+
paths.push(import_node_path.default.join(import_node_path.default.resolve(dirPath, file.name), import_node_path.default.sep));
|
|
209
|
+
}
|
|
210
|
+
paths.push(...await readDirFiles(import_node_path.default.join(dirPath, file.name)));
|
|
211
|
+
} else if (file.isFile()) {
|
|
212
|
+
paths.push(import_node_path.default.resolve(dirPath, file.name));
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return paths;
|
|
216
|
+
}
|
|
217
|
+
__name(readDirFiles, "readDirFiles");
|
|
218
|
+
|
|
219
|
+
// src/index.ts
|
|
220
|
+
var name = "rzgtboeyndxsklmq-commons";
|
|
221
|
+
var Config = import_koishi.Schema.object({});
|
|
222
|
+
function apply(ctx, config) {
|
|
223
|
+
}
|
|
224
|
+
__name(apply, "apply");
|
|
225
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
226
|
+
0 && (module.exports = {
|
|
227
|
+
BeanHelper,
|
|
228
|
+
BeanType,
|
|
229
|
+
Config,
|
|
230
|
+
apply,
|
|
231
|
+
name,
|
|
232
|
+
readDirFiles
|
|
233
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { Context, Awaitable } from "koishi";
|
|
2
|
+
export declare abstract class BeanType<C extends object> {
|
|
3
|
+
protected beanHelper: BeanHelper<C>;
|
|
4
|
+
protected ctx: Context;
|
|
5
|
+
protected config: C;
|
|
6
|
+
constructor(beanHelper: BeanHelper<C>);
|
|
7
|
+
start(): Awaitable<void>;
|
|
8
|
+
destroy(): Awaitable<void>;
|
|
9
|
+
}
|
|
10
|
+
type Constructor<T = any> = new (...args: any[]) => T;
|
|
11
|
+
export interface ClassInfo<T> {
|
|
12
|
+
name: string;
|
|
13
|
+
class?: Constructor<T>;
|
|
14
|
+
instance?: T;
|
|
15
|
+
proxy?: T;
|
|
16
|
+
proxyRevoke?: () => void;
|
|
17
|
+
}
|
|
18
|
+
export declare class BeanHelper<C extends object> {
|
|
19
|
+
static buildLazyProxyHandler<T extends object>(getObj: () => T, hooks?: {
|
|
20
|
+
get: (obj: any, prop: PropertyKey) => any;
|
|
21
|
+
}): ProxyHandler<T>;
|
|
22
|
+
static buildLazyProxy<T extends object>(getObj: () => T): T;
|
|
23
|
+
static buildLazyRevocableProxy<T extends object>(getObj: () => T): {
|
|
24
|
+
proxy: T;
|
|
25
|
+
revoke: () => void;
|
|
26
|
+
};
|
|
27
|
+
private classPool;
|
|
28
|
+
ctx: Context;
|
|
29
|
+
config: C;
|
|
30
|
+
constructor();
|
|
31
|
+
setCtx(ctx: Context, config: C): void;
|
|
32
|
+
private touchAll;
|
|
33
|
+
start(): Promise<void>;
|
|
34
|
+
destroy(): Promise<void>;
|
|
35
|
+
instance<T extends BeanType<C>>(clazz: Constructor<T>): T;
|
|
36
|
+
proxy<T extends object>(getObj: () => T, name: string): T;
|
|
37
|
+
getByName<T>(name: string): T;
|
|
38
|
+
put<T>(instance: T, name?: string, clazz?: Constructor): T;
|
|
39
|
+
}
|
|
40
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function readDirFiles(dirPath: string, needDir?: boolean): Promise<string[]>;
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "koishi-plugin-rzgtboeyndxsklmq-commons",
|
|
3
|
+
"description": "",
|
|
4
|
+
"version": "1.0.1",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"typings": "lib/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "cd ../../ && yarn build rzgtboeyndxsklmq-commons",
|
|
9
|
+
"pub": "cd ../../ && yarn pub rzgtboeyndxsklmq-commons",
|
|
10
|
+
"fastPub": "cd ../../ && yarn bump -3 rzgtboeyndxsklmq-commons && yarn build rzgtboeyndxsklmq-commons && yarn pub rzgtboeyndxsklmq-commons"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"lib",
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"contributors": [
|
|
17
|
+
"pgnqukezrdxmhjso"
|
|
18
|
+
],
|
|
19
|
+
"homepage": "https://github.com/pgnqukezrdxmhjso/koishi-plugin-rzgtboeyndxsklmq-commons",
|
|
20
|
+
"bugs": {
|
|
21
|
+
"url": "https://github.com/pgnqukezrdxmhjso/koishi-plugin-rzgtboeyndxsklmq-commons/issues"
|
|
22
|
+
},
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "git+https://github.com/pgnqukezrdxmhjso/koishi-plugin-rzgtboeyndxsklmq-commons.git"
|
|
26
|
+
},
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"keywords": [
|
|
29
|
+
"koishi",
|
|
30
|
+
"commons"
|
|
31
|
+
],
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"koishi": "^4.18.10"
|
|
34
|
+
}
|
|
35
|
+
}
|