@eggjs/tegg-common-util 4.0.0-beta.6 → 4.0.0-beta.8

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/dist/index.d.ts CHANGED
@@ -1,13 +1,136 @@
1
- import { MapUtil } from "./MapUtil.js";
2
- import { NameUtil } from "./NameUtil.js";
3
- import { EdgeMeta, Graph, GraphNode, GraphPath } from "./Graph.js";
4
- import { ObjectUtils } from "./ObjectUtils.js";
5
- import { FSUtil } from "./FSUtil.js";
6
- import { StackUtil } from "./StackUtil.js";
7
- import { ProxyUtil } from "./ProxyUtil.js";
8
- import { ModuleConfigUtil, ModuleReferenceConfigHelp } from "./ModuleConfig.js";
9
- import { ModuleConfig, ModuleConfigs } from "./ModuleConfigs.js";
10
- import { TimerUtil } from "./TimerUtil.js";
11
1
  import "reflect-metadata";
2
+ import { EggProtoImplClass, GraphNodeObj, InlineModuleReferenceConfig, ModuleConfig, ModuleConfig as ModuleConfig$1, ModuleConfigHolder, ModuleReference, ModuleReferenceConfig, NpmModuleReferenceConfig, ReadModuleReferenceOptions } from "@eggjs/tegg-types";
12
3
  export * from "@eggjs/tegg-types/common";
13
- export { EdgeMeta, FSUtil, Graph, GraphNode, GraphPath, MapUtil, ModuleConfig, ModuleConfigUtil, ModuleConfigs, ModuleReferenceConfigHelp, NameUtil, ObjectUtils, ProxyUtil, StackUtil, TimerUtil };
4
+
5
+ //#region src/MapUtil.d.ts
6
+ declare class MapUtil {
7
+ static getOrStore<K, V>(map: Map<K, V>, key: K, value: V): V;
8
+ }
9
+ //#endregion
10
+ //#region src/NameUtil.d.ts
11
+ declare class NameUtil {
12
+ static getClassName(constructor: Function): string;
13
+ }
14
+ //#endregion
15
+ //#region src/Graph.d.ts
16
+ declare const inspect: unique symbol;
17
+ interface EdgeMeta {
18
+ equal(meta: EdgeMeta): boolean;
19
+ toString(): string;
20
+ }
21
+ declare class GraphNode<T extends GraphNodeObj, M extends EdgeMeta = EdgeMeta> {
22
+ val: T;
23
+ toNodeMap: Map<string, {
24
+ node: GraphNode<T, M>;
25
+ meta?: M;
26
+ }>;
27
+ fromNodeMap: Map<string, {
28
+ node: GraphNode<T, M>;
29
+ meta?: M;
30
+ }>;
31
+ constructor(val: T);
32
+ get id(): string;
33
+ addToVertex(node: GraphNode<T, M>, meta?: M): boolean;
34
+ addFromVertex(node: GraphNode<T, M>, meta?: M): boolean;
35
+ [inspect](): {
36
+ val: T;
37
+ toNodes: {
38
+ node: GraphNode<T, M>;
39
+ meta?: M;
40
+ }[];
41
+ fromNodes: {
42
+ node: GraphNode<T, M>;
43
+ meta?: M;
44
+ }[];
45
+ };
46
+ toJSON(): {
47
+ val: T;
48
+ toNodes: {
49
+ node: GraphNode<T, M>;
50
+ meta?: M;
51
+ }[];
52
+ fromNodes: {
53
+ node: GraphNode<T, M>;
54
+ meta?: M;
55
+ }[];
56
+ };
57
+ toString(): string;
58
+ }
59
+ declare class GraphPath<T extends GraphNodeObj, M extends EdgeMeta = EdgeMeta> {
60
+ nodeIdMap: Map<string, number>;
61
+ nodes: Array<{
62
+ node: GraphNode<T, M>;
63
+ meta?: M;
64
+ }>;
65
+ pushVertex(node: GraphNode<T, M>, meta?: M): boolean;
66
+ popVertex(): void;
67
+ toString(): string;
68
+ [inspect](): string;
69
+ }
70
+ declare class Graph<T extends GraphNodeObj, M extends EdgeMeta = EdgeMeta> {
71
+ nodes: Map<string, GraphNode<T, M>>;
72
+ addVertex(node: GraphNode<T, M>): boolean;
73
+ addEdge(from: GraphNode<T, M>, to: GraphNode<T, M>, meta?: M): boolean;
74
+ findToNode(id: string, meta: M): GraphNode<T, M> | undefined;
75
+ appendVertexToPath(node: GraphNode<T, M>, accessPath: GraphPath<T, M>, meta?: M): boolean;
76
+ loopPath(): GraphPath<T, M> | undefined;
77
+ accessNode(node: GraphNode<T, M>, nodes: Array<GraphNode<T, M>>, accessed: boolean[], res: Array<GraphNode<T, M>>): void;
78
+ sort(): Array<GraphNode<T, M>>;
79
+ }
80
+ //#endregion
81
+ //#region src/ObjectUtils.d.ts
82
+ declare class ObjectUtils {
83
+ static getProperties(obj: object): string[];
84
+ static getFunctionArgNameList(func: Function): string[];
85
+ static getConstructorArgNameList(clazz: EggProtoImplClass): string[];
86
+ }
87
+ //#endregion
88
+ //#region src/FSUtil.d.ts
89
+ declare class FSUtil {
90
+ static fileExists(filePath: string): Promise<boolean>;
91
+ }
92
+ //#endregion
93
+ //#region src/StackUtil.d.ts
94
+ declare class StackUtil {
95
+ static getCalleeFromStack(withLine: boolean, stackIndex?: number): string;
96
+ }
97
+ //#endregion
98
+ //#region src/ProxyUtil.d.ts
99
+ declare class ProxyUtil {
100
+ static safeProxy<T extends object>(obj: T, getter: (obj: T, p: PropertyKey) => any): T;
101
+ }
102
+ //#endregion
103
+ //#region src/ModuleConfig.d.ts
104
+ declare class ModuleReferenceConfigHelp {
105
+ static isInlineModuleReference(moduleReference: ModuleReferenceConfig): moduleReference is InlineModuleReferenceConfig;
106
+ static isNpmModuleReference(moduleReference: ModuleReferenceConfig): moduleReference is NpmModuleReferenceConfig;
107
+ }
108
+ declare class ModuleConfigUtil {
109
+ #private;
110
+ static configNames: string[] | undefined;
111
+ static setConfigNames(configNames: string[] | undefined): void;
112
+ static readModuleReference(baseDir: string, options?: ReadModuleReferenceOptions): readonly ModuleReference[];
113
+ private static readModuleReferenceFromModuleJson;
114
+ private static readModuleReferenceFromScan;
115
+ static readModuleFromNodeModules(baseDir: string): ModuleReference[];
116
+ static resolveModuleDir(moduleDir: string, baseDir?: string): string;
117
+ private static getModuleName;
118
+ static readModuleName(baseDir: string, moduleDir: string): Promise<string>;
119
+ static readModuleNameSync(moduleDir: string, baseDir?: string): string;
120
+ static loadModuleConfig(moduleDir: string, baseDir?: string, env?: string): Promise<ModuleConfig$1>;
121
+ static loadModuleConfigSync(moduleDir: string, baseDir?: string, env?: string): ModuleConfig$1;
122
+ }
123
+ //#endregion
124
+ //#region src/ModuleConfigs.d.ts
125
+ declare class ModuleConfigs {
126
+ readonly inner: Record<string, ModuleConfigHolder>;
127
+ constructor(inner: Record<string, ModuleConfigHolder>);
128
+ get(moduleName: string): ModuleConfig | undefined;
129
+ }
130
+ //#endregion
131
+ //#region src/TimerUtil.d.ts
132
+ declare class TimerUtil {
133
+ static sleep(ms: number): Promise<void>;
134
+ }
135
+ //#endregion
136
+ export { EdgeMeta, FSUtil, Graph, GraphNode, GraphPath, MapUtil, type ModuleConfig, ModuleConfigUtil, ModuleConfigs, ModuleReferenceConfigHelp, NameUtil, ObjectUtils, ProxyUtil, StackUtil, TimerUtil };
package/dist/index.js CHANGED
@@ -1,15 +1,481 @@
1
- import { MapUtil } from "./MapUtil.js";
2
- import { NameUtil } from "./NameUtil.js";
3
- import { Graph, GraphNode, GraphPath } from "./Graph.js";
4
- import { ObjectUtils } from "./ObjectUtils.js";
5
- import { FSUtil } from "./FSUtil.js";
6
- import { StackUtil } from "./StackUtil.js";
7
- import { ProxyUtil } from "./ProxyUtil.js";
8
- import { ModuleConfigUtil, ModuleReferenceConfigHelp } from "./ModuleConfig.js";
9
- import { ModuleConfigs } from "./ModuleConfigs.js";
10
- import { TimerUtil } from "./TimerUtil.js";
11
1
  import "reflect-metadata";
2
+ import fs from "node:fs/promises";
3
+ import { fileURLToPath } from "node:url";
4
+ import { debuglog } from "node:util";
5
+ import assert from "node:assert";
6
+ import fs$1, { promises } from "node:fs";
7
+ import path from "node:path";
8
+ import { extend } from "extend2";
9
+ import { globbySync } from "globby";
10
+ import { load } from "js-yaml";
11
+ import { importResolve } from "@eggjs/utils";
12
12
 
13
13
  export * from "@eggjs/tegg-types/common"
14
14
 
15
+ //#region src/MapUtil.ts
16
+ var MapUtil = class {
17
+ static getOrStore(map, key, value) {
18
+ if (!map.has(key)) map.set(key, value);
19
+ return map.get(key);
20
+ }
21
+ };
22
+
23
+ //#endregion
24
+ //#region src/NameUtil.ts
25
+ var NameUtil = class {
26
+ static getClassName(constructor) {
27
+ return constructor.name[0].toLowerCase() + constructor.name.substring(1);
28
+ }
29
+ };
30
+
31
+ //#endregion
32
+ //#region src/Graph.ts
33
+ const inspect = Symbol.for("nodejs.util.inspect.custom");
34
+ var GraphNode = class {
35
+ val;
36
+ toNodeMap = /* @__PURE__ */ new Map();
37
+ fromNodeMap = /* @__PURE__ */ new Map();
38
+ constructor(val) {
39
+ this.val = val;
40
+ }
41
+ get id() {
42
+ return this.val.id;
43
+ }
44
+ addToVertex(node, meta) {
45
+ if (this.toNodeMap.has(node.id)) return false;
46
+ this.toNodeMap.set(node.id, {
47
+ node,
48
+ meta
49
+ });
50
+ return true;
51
+ }
52
+ addFromVertex(node, meta) {
53
+ if (this.fromNodeMap.has(node.id)) return false;
54
+ this.fromNodeMap.set(node.id, {
55
+ node,
56
+ meta
57
+ });
58
+ return true;
59
+ }
60
+ [inspect]() {
61
+ return this.toJSON();
62
+ }
63
+ toJSON() {
64
+ return {
65
+ val: this.val,
66
+ toNodes: Array.from(this.toNodeMap.values()),
67
+ fromNodes: Array.from(this.fromNodeMap.values())
68
+ };
69
+ }
70
+ toString() {
71
+ return this.val.toString();
72
+ }
73
+ };
74
+ var GraphPath = class {
75
+ nodeIdMap = /* @__PURE__ */ new Map();
76
+ nodes = [];
77
+ pushVertex(node, meta) {
78
+ const val = this.nodeIdMap.get(node.id) || 0;
79
+ this.nodeIdMap.set(node.id, val + 1);
80
+ this.nodes.push({
81
+ node,
82
+ meta
83
+ });
84
+ return val === 0;
85
+ }
86
+ popVertex() {
87
+ const nodeHandler = this.nodes.pop();
88
+ if (nodeHandler) {
89
+ const val = this.nodeIdMap.get(nodeHandler.node.id);
90
+ this.nodeIdMap.set(nodeHandler.node.id, val - 1);
91
+ }
92
+ }
93
+ toString() {
94
+ return this.nodes.reduce((p, c) => {
95
+ let msg = "";
96
+ if (c.meta) msg += ` ${c.meta.toString()} -> `;
97
+ else if (p.length) msg += " -> ";
98
+ msg += c.node.val.toString();
99
+ p.push(msg);
100
+ return p;
101
+ }, new Array()).join("");
102
+ }
103
+ [inspect]() {
104
+ return this.toString();
105
+ }
106
+ };
107
+ var Graph = class {
108
+ nodes = /* @__PURE__ */ new Map();
109
+ addVertex(node) {
110
+ if (this.nodes.has(node.id)) return false;
111
+ this.nodes.set(node.id, node);
112
+ return true;
113
+ }
114
+ addEdge(from, to, meta) {
115
+ to.addFromVertex(from, meta);
116
+ return from.addToVertex(to, meta);
117
+ }
118
+ findToNode(id, meta) {
119
+ const node = this.nodes.get(id);
120
+ if (!node) return void 0;
121
+ for (const { node: toNode, meta: edgeMeta } of node.toNodeMap.values()) if (edgeMeta && meta.equal(edgeMeta)) return toNode;
122
+ }
123
+ appendVertexToPath(node, accessPath, meta) {
124
+ if (!accessPath.pushVertex(node, meta)) return false;
125
+ for (const toNode of node.toNodeMap.values()) if (!this.appendVertexToPath(toNode.node, accessPath, toNode.meta)) return false;
126
+ accessPath.popVertex();
127
+ return true;
128
+ }
129
+ loopPath() {
130
+ const accessPath = new GraphPath();
131
+ const nodes = Array.from(this.nodes.values());
132
+ for (const node of nodes) if (!this.appendVertexToPath(node, accessPath)) return accessPath;
133
+ }
134
+ accessNode(node, nodes, accessed, res) {
135
+ const index = nodes.indexOf(node);
136
+ if (accessed[index]) return;
137
+ if (!node.toNodeMap.size) {
138
+ accessed[nodes.indexOf(node)] = true;
139
+ res.push(node);
140
+ return;
141
+ }
142
+ for (const toNode of node.toNodeMap.values()) this.accessNode(toNode.node, nodes, accessed, res);
143
+ accessed[nodes.indexOf(node)] = true;
144
+ res.push(node);
145
+ }
146
+ sort() {
147
+ const res = [];
148
+ const nodes = Array.from(this.nodes.values());
149
+ const accessed = [];
150
+ for (let i = 0; i < nodes.length; ++i) accessed.push(false);
151
+ for (let i = 0; i < nodes.length; ++i) {
152
+ const node = nodes[i];
153
+ this.accessNode(node, nodes, accessed, res);
154
+ }
155
+ return res;
156
+ }
157
+ };
158
+
159
+ //#endregion
160
+ //#region src/ObjectUtils.ts
161
+ var ObjectUtils = class {
162
+ static getProperties(obj) {
163
+ const properties = [];
164
+ do
165
+ for (const property of Object.getOwnPropertyNames(obj)) properties.push(property);
166
+ while ((obj = Object.getPrototypeOf(obj)) && obj !== Object.prototype);
167
+ return properties;
168
+ }
169
+ static getFunctionArgNameList(func) {
170
+ if (func.length === 0) return [];
171
+ let sourcecode = func.toString();
172
+ sourcecode = sourcecode.replace(/\/\*[\s\S]*?\*\//g, "").replace(/\/\/(.)*/g, "").replace(/{[\s\S]*}/, "").replace(/=>/g, "").trim();
173
+ let argsString = sourcecode.substring(sourcecode.indexOf("(") + 1, sourcecode.length - 1);
174
+ argsString = argsString.replace(/=\([\s\S]*\)/g, "");
175
+ return argsString.split(",").map((arg) => {
176
+ return arg.replace(/=[\s\S]*/g, "").trim();
177
+ }).filter((arg) => arg.length);
178
+ }
179
+ static getConstructorArgNameList(clazz) {
180
+ if (clazz.length === 0) return [];
181
+ const constructorMatch = clazz.toString().match(/constructor\s*\(([^)]+)\)/);
182
+ if (!constructorMatch) return [];
183
+ return constructorMatch[1].split(",").map((param) => param.trim()).map((param) => param.match(/(\w+)\s*(?=\s*(?:=|\/\/|\s*$))/)).filter(Boolean).map((match) => match[0].trim());
184
+ }
185
+ };
186
+
187
+ //#endregion
188
+ //#region src/FSUtil.ts
189
+ var FSUtil = class {
190
+ static async fileExists(filePath) {
191
+ try {
192
+ await fs.access(filePath);
193
+ } catch {
194
+ return false;
195
+ }
196
+ return true;
197
+ }
198
+ };
199
+
200
+ //#endregion
201
+ //#region src/StackUtil.ts
202
+ const debug = debuglog("@eggjs/tegg-common-util/StackUtil");
203
+ /**
204
+ * Capture call site stack from v8.
205
+ * https://github.com/v8/v8/wiki/Stack-Trace-API
206
+ */
207
+ function prepareObjectStackTrace(_, stack) {
208
+ return stack;
209
+ }
210
+ var StackUtil = class {
211
+ static getCalleeFromStack(withLine, stackIndex) {
212
+ stackIndex = stackIndex === void 0 ? 2 : stackIndex;
213
+ const limit = Error.stackTraceLimit;
214
+ const prep = Error.prepareStackTrace;
215
+ Error.prepareStackTrace = prepareObjectStackTrace;
216
+ Error.stackTraceLimit = 10;
217
+ const obj = { stack: [] };
218
+ Error.captureStackTrace(obj);
219
+ if (debug.enabled) debug("call stack: %o", obj.stack.map((callSite$1) => callSite$1.getFileName() ?? "<anonymous>").join("\n"));
220
+ const callSite = obj.stack[stackIndex];
221
+ let fileName;
222
+ if (callSite) {
223
+ fileName = callSite.getFileName();
224
+ if (fileName?.startsWith("file://")) fileName = fileURLToPath(fileName);
225
+ }
226
+ Error.prepareStackTrace = prep;
227
+ Error.stackTraceLimit = limit;
228
+ /* istanbul ignore if */
229
+ if (!callSite || !fileName) return "<anonymous>";
230
+ if (!withLine) return fileName;
231
+ return `${fileName}:${callSite.getLineNumber()}:${callSite.getColumnNumber()}`;
232
+ }
233
+ };
234
+
235
+ //#endregion
236
+ //#region src/ProxyUtil.ts
237
+ var ProxyUtil = class {
238
+ static safeProxy(obj, getter) {
239
+ return new Proxy(obj, { get(target, p) {
240
+ if (Object.prototype[p]) return Reflect.get(target, p);
241
+ return getter(target, p);
242
+ } });
243
+ }
244
+ };
245
+
246
+ //#endregion
247
+ //#region src/ModuleConfig.ts
248
+ var ModuleReferenceConfigHelp = class {
249
+ static isInlineModuleReference(moduleReference) {
250
+ return !!moduleReference.path;
251
+ }
252
+ static isNpmModuleReference(moduleReference) {
253
+ return !!moduleReference.package;
254
+ }
255
+ };
256
+ const DEFAULT_READ_MODULE_REF_OPTS = { deep: 10 };
257
+ var ModuleConfigUtil = class ModuleConfigUtil {
258
+ static configNames;
259
+ static setConfigNames(configNames) {
260
+ ModuleConfigUtil.configNames = configNames;
261
+ }
262
+ static readModuleReference(baseDir, options) {
263
+ const configDir = path.join(baseDir, "config");
264
+ const moduleJsonPath = path.join(configDir, "module.json");
265
+ if (fs$1.existsSync(moduleJsonPath)) return this.readModuleReferenceFromModuleJson(configDir, moduleJsonPath, options?.cwd || baseDir);
266
+ return this.readModuleReferenceFromScan(baseDir, options);
267
+ }
268
+ static readModuleReferenceFromModuleJson(configDir, moduleJsonPath, cwd) {
269
+ const moduleJsonContent = fs$1.readFileSync(moduleJsonPath, "utf8");
270
+ const moduleJson = JSON.parse(moduleJsonContent);
271
+ const moduleReferenceList = [];
272
+ for (const moduleReferenceConfig of moduleJson) {
273
+ let moduleReference;
274
+ if (ModuleReferenceConfigHelp.isNpmModuleReference(moduleReferenceConfig)) {
275
+ const options = cwd ? { paths: [cwd] } : {};
276
+ const pkgJson = path.posix.join(moduleReferenceConfig.package, "package.json");
277
+ const file = importResolve(pkgJson, options);
278
+ const modulePath = path.dirname(file);
279
+ moduleReference = {
280
+ path: modulePath,
281
+ name: ModuleConfigUtil.readModuleNameSync(modulePath)
282
+ };
283
+ } else if (ModuleReferenceConfigHelp.isInlineModuleReference(moduleReferenceConfig)) {
284
+ const modulePath = path.join(configDir, moduleReferenceConfig.path);
285
+ moduleReference = {
286
+ path: modulePath,
287
+ name: ModuleConfigUtil.readModuleNameSync(modulePath)
288
+ };
289
+ } else throw new Error("unknown type of module reference config: " + JSON.stringify(moduleReferenceConfig));
290
+ moduleReferenceList.push(moduleReference);
291
+ }
292
+ return moduleReferenceList;
293
+ }
294
+ static readModuleReferenceFromScan(baseDir, options) {
295
+ const ref = [];
296
+ const realOptions = Object.assign({}, DEFAULT_READ_MODULE_REF_OPTS, options);
297
+ const packagePaths = globbySync([
298
+ "**/package.json",
299
+ "!**/node_modules",
300
+ "!**/+(.*)/**",
301
+ "!**/coverage",
302
+ ...realOptions.extraFilePattern || []
303
+ ], {
304
+ cwd: baseDir,
305
+ deep: realOptions.deep
306
+ });
307
+ const moduleDirSet = /* @__PURE__ */ new Set();
308
+ for (const packagePath of packagePaths) {
309
+ const absolutePkgPath = path.join(baseDir, packagePath);
310
+ let realPkgPath;
311
+ try {
312
+ realPkgPath = fs$1.realpathSync(absolutePkgPath);
313
+ } catch {
314
+ continue;
315
+ }
316
+ const moduleDir = path.dirname(realPkgPath);
317
+ if (moduleDirSet.has(moduleDir)) continue;
318
+ moduleDirSet.add(moduleDir);
319
+ let name;
320
+ try {
321
+ name = this.readModuleNameSync(moduleDir);
322
+ } catch {
323
+ continue;
324
+ }
325
+ ref.push({
326
+ path: moduleDir,
327
+ name
328
+ });
329
+ }
330
+ const moduleReferences = this.readModuleFromNodeModules(baseDir);
331
+ for (const moduleReference of moduleReferences) {
332
+ const moduleBasePath = path.basename(moduleReference.path);
333
+ moduleDirSet.forEach((modulePath) => {
334
+ if (path.basename(modulePath) === moduleBasePath) throw new Error("duplicate import of module reference: " + moduleBasePath);
335
+ });
336
+ ref.push({
337
+ path: moduleReference.path,
338
+ name: moduleReference.name
339
+ });
340
+ }
341
+ return ref;
342
+ }
343
+ static readModuleFromNodeModules(baseDir) {
344
+ const ref = [];
345
+ let pkgContent;
346
+ try {
347
+ pkgContent = fs$1.readFileSync(path.join(baseDir, "package.json"), "utf8");
348
+ } catch {
349
+ return [];
350
+ }
351
+ const pkg = JSON.parse(pkgContent);
352
+ for (const dependencyKey of Object.keys(pkg.dependencies || {})) {
353
+ let packageJsonPath;
354
+ try {
355
+ packageJsonPath = importResolve(`${dependencyKey}/package.json`, { paths: [baseDir] });
356
+ } catch {
357
+ continue;
358
+ }
359
+ const absolutePkgPath = path.dirname(packageJsonPath);
360
+ const realPkgPath = fs$1.realpathSync(absolutePkgPath);
361
+ try {
362
+ const name = this.readModuleNameSync(realPkgPath);
363
+ ref.push({
364
+ path: realPkgPath,
365
+ name
366
+ });
367
+ } catch {
368
+ continue;
369
+ }
370
+ }
371
+ return ref;
372
+ }
373
+ static resolveModuleDir(moduleDir, baseDir) {
374
+ if (path.isAbsolute(moduleDir)) return moduleDir;
375
+ assert(baseDir, "baseDir is required");
376
+ return path.join(baseDir, "config", moduleDir);
377
+ }
378
+ static getModuleName(pkg) {
379
+ assert(pkg.eggModule && pkg.eggModule.name, "eggModule.name not found in package.json");
380
+ return pkg.eggModule.name;
381
+ }
382
+ static async readModuleName(baseDir, moduleDir) {
383
+ moduleDir = ModuleConfigUtil.resolveModuleDir(moduleDir, baseDir);
384
+ const pkgContent = await promises.readFile(path.join(moduleDir, "package.json"), "utf8");
385
+ const pkg = JSON.parse(pkgContent);
386
+ return ModuleConfigUtil.getModuleName(pkg);
387
+ }
388
+ static readModuleNameSync(moduleDir, baseDir) {
389
+ moduleDir = ModuleConfigUtil.resolveModuleDir(moduleDir, baseDir);
390
+ const pkgContent = fs$1.readFileSync(path.join(moduleDir, "package.json"), "utf8");
391
+ const pkg = JSON.parse(pkgContent);
392
+ return ModuleConfigUtil.getModuleName(pkg);
393
+ }
394
+ static async loadModuleConfig(moduleDir, baseDir, env) {
395
+ const modulePath = ModuleConfigUtil.resolveModuleDir(moduleDir, baseDir);
396
+ let configNames;
397
+ if (env) configNames = ["module", `module.${env}`];
398
+ else configNames = ModuleConfigUtil.configNames || ["module"];
399
+ const target = {};
400
+ for (const configName of configNames) {
401
+ let config = await ModuleConfigUtil.#loadOne(modulePath, configName);
402
+ if (configName === "module.default" && !config) config = await ModuleConfigUtil.#loadOne(modulePath, "module");
403
+ if (config) extend(true, target, config);
404
+ }
405
+ return target;
406
+ }
407
+ static async #loadOne(moduleDir, configName) {
408
+ const yamlConfigPath = path.join(moduleDir, `${configName}.yml`);
409
+ let config = await ModuleConfigUtil.#loadYaml(yamlConfigPath);
410
+ if (!config) {
411
+ const jsonConfigPath = path.join(moduleDir, `${configName}.json`);
412
+ config = await ModuleConfigUtil.#loadJson(jsonConfigPath);
413
+ }
414
+ return config;
415
+ }
416
+ static async #loadJson(moduleJsonPath) {
417
+ if (!await FSUtil.fileExists(moduleJsonPath)) return;
418
+ const moduleJsonContent = await promises.readFile(moduleJsonPath, "utf8");
419
+ return JSON.parse(moduleJsonContent).config;
420
+ }
421
+ static async #loadYaml(moduleYamlPath) {
422
+ if (!await FSUtil.fileExists(moduleYamlPath)) return;
423
+ const moduleYamlContent = await promises.readFile(moduleYamlPath, "utf8");
424
+ return load(moduleYamlContent);
425
+ }
426
+ static loadModuleConfigSync(moduleDir, baseDir, env) {
427
+ const modulePath = ModuleConfigUtil.resolveModuleDir(moduleDir, baseDir);
428
+ let configNames;
429
+ if (env) configNames = ["module", `module.${env}`];
430
+ else configNames = ModuleConfigUtil.configNames || ["module"];
431
+ const target = {};
432
+ for (const configName of configNames) {
433
+ let config = ModuleConfigUtil.#loadOneSync(modulePath, configName);
434
+ if (configName === "module.default" && !config) config = ModuleConfigUtil.#loadOneSync(modulePath, "module");
435
+ if (config) extend(true, target, config);
436
+ }
437
+ return target;
438
+ }
439
+ static #loadOneSync(moduleDir, configName) {
440
+ const yamlConfigPath = path.join(moduleDir, `${configName}.yml`);
441
+ let config = ModuleConfigUtil.#loadYamlSync(yamlConfigPath);
442
+ if (!config) {
443
+ const jsonConfigPath = path.join(moduleDir, `${configName}.json`);
444
+ config = ModuleConfigUtil.#loadJsonSync(jsonConfigPath);
445
+ }
446
+ return config;
447
+ }
448
+ static #loadJsonSync(moduleJsonPath) {
449
+ if (!fs$1.existsSync(moduleJsonPath)) return;
450
+ const moduleJsonContent = fs$1.readFileSync(moduleJsonPath, "utf8");
451
+ return JSON.parse(moduleJsonContent).config;
452
+ }
453
+ static #loadYamlSync(moduleYamlPath) {
454
+ if (!fs$1.existsSync(moduleYamlPath)) return;
455
+ const moduleYamlContent = fs$1.readFileSync(moduleYamlPath, "utf8");
456
+ return load(moduleYamlContent);
457
+ }
458
+ };
459
+
460
+ //#endregion
461
+ //#region src/ModuleConfigs.ts
462
+ var ModuleConfigs = class {
463
+ inner;
464
+ constructor(inner) {
465
+ this.inner = inner;
466
+ }
467
+ get(moduleName) {
468
+ return this.inner[moduleName]?.config;
469
+ }
470
+ };
471
+
472
+ //#endregion
473
+ //#region src/TimerUtil.ts
474
+ var TimerUtil = class {
475
+ static async sleep(ms) {
476
+ await new Promise((resolve) => setTimeout(resolve, ms));
477
+ }
478
+ };
479
+
480
+ //#endregion
15
481
  export { FSUtil, Graph, GraphNode, GraphPath, MapUtil, ModuleConfigUtil, ModuleConfigs, ModuleReferenceConfigHelp, NameUtil, ObjectUtils, ProxyUtil, StackUtil, TimerUtil };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@eggjs/tegg-common-util",
3
3
  "description": "common util for tegg",
4
- "version": "4.0.0-beta.6",
4
+ "version": "4.0.0-beta.8",
5
5
  "keywords": [
6
6
  "egg",
7
7
  "typescript",
@@ -36,7 +36,7 @@
36
36
  "extend2": "^4.0.0",
37
37
  "globby": "^14.1.0",
38
38
  "js-yaml": "4.1.0",
39
- "@eggjs/tegg-types": "4.0.0-beta.6"
39
+ "@eggjs/tegg-types": "4.0.0-beta.8"
40
40
  },
41
41
  "publishConfig": {
42
42
  "access": "public"
package/dist/FSUtil.d.ts DELETED
@@ -1,6 +0,0 @@
1
- //#region src/FSUtil.d.ts
2
- declare class FSUtil {
3
- static fileExists(filePath: string): Promise<boolean>;
4
- }
5
- //#endregion
6
- export { FSUtil };
package/dist/FSUtil.js DELETED
@@ -1,16 +0,0 @@
1
- import fs from "node:fs/promises";
2
-
3
- //#region src/FSUtil.ts
4
- var FSUtil = class {
5
- static async fileExists(filePath) {
6
- try {
7
- await fs.access(filePath);
8
- } catch {
9
- return false;
10
- }
11
- return true;
12
- }
13
- };
14
-
15
- //#endregion
16
- export { FSUtil };
package/dist/Graph.d.ts DELETED
@@ -1,69 +0,0 @@
1
- import { GraphNodeObj } from "@eggjs/tegg-types";
2
-
3
- //#region src/Graph.d.ts
4
- declare const inspect: unique symbol;
5
- interface EdgeMeta {
6
- equal(meta: EdgeMeta): boolean;
7
- toString(): string;
8
- }
9
- declare class GraphNode<T extends GraphNodeObj, M extends EdgeMeta = EdgeMeta> {
10
- val: T;
11
- toNodeMap: Map<string, {
12
- node: GraphNode<T, M>;
13
- meta?: M;
14
- }>;
15
- fromNodeMap: Map<string, {
16
- node: GraphNode<T, M>;
17
- meta?: M;
18
- }>;
19
- constructor(val: T);
20
- get id(): string;
21
- addToVertex(node: GraphNode<T, M>, meta?: M): boolean;
22
- addFromVertex(node: GraphNode<T, M>, meta?: M): boolean;
23
- [inspect](): {
24
- val: T;
25
- toNodes: {
26
- node: GraphNode<T, M>;
27
- meta?: M;
28
- }[];
29
- fromNodes: {
30
- node: GraphNode<T, M>;
31
- meta?: M;
32
- }[];
33
- };
34
- toJSON(): {
35
- val: T;
36
- toNodes: {
37
- node: GraphNode<T, M>;
38
- meta?: M;
39
- }[];
40
- fromNodes: {
41
- node: GraphNode<T, M>;
42
- meta?: M;
43
- }[];
44
- };
45
- toString(): string;
46
- }
47
- declare class GraphPath<T extends GraphNodeObj, M extends EdgeMeta = EdgeMeta> {
48
- nodeIdMap: Map<string, number>;
49
- nodes: Array<{
50
- node: GraphNode<T, M>;
51
- meta?: M;
52
- }>;
53
- pushVertex(node: GraphNode<T, M>, meta?: M): boolean;
54
- popVertex(): void;
55
- toString(): string;
56
- [inspect](): string;
57
- }
58
- declare class Graph<T extends GraphNodeObj, M extends EdgeMeta = EdgeMeta> {
59
- nodes: Map<string, GraphNode<T, M>>;
60
- addVertex(node: GraphNode<T, M>): boolean;
61
- addEdge(from: GraphNode<T, M>, to: GraphNode<T, M>, meta?: M): boolean;
62
- findToNode(id: string, meta: M): GraphNode<T, M> | undefined;
63
- appendVertexToPath(node: GraphNode<T, M>, accessPath: GraphPath<T, M>, meta?: M): boolean;
64
- loopPath(): GraphPath<T, M> | undefined;
65
- accessNode(node: GraphNode<T, M>, nodes: Array<GraphNode<T, M>>, accessed: boolean[], res: Array<GraphNode<T, M>>): void;
66
- sort(): Array<GraphNode<T, M>>;
67
- }
68
- //#endregion
69
- export { EdgeMeta, Graph, GraphNode, GraphPath };
package/dist/Graph.js DELETED
@@ -1,129 +0,0 @@
1
- //#region src/Graph.ts
2
- const inspect = Symbol.for("nodejs.util.inspect.custom");
3
- var GraphNode = class {
4
- val;
5
- toNodeMap = /* @__PURE__ */ new Map();
6
- fromNodeMap = /* @__PURE__ */ new Map();
7
- constructor(val) {
8
- this.val = val;
9
- }
10
- get id() {
11
- return this.val.id;
12
- }
13
- addToVertex(node, meta) {
14
- if (this.toNodeMap.has(node.id)) return false;
15
- this.toNodeMap.set(node.id, {
16
- node,
17
- meta
18
- });
19
- return true;
20
- }
21
- addFromVertex(node, meta) {
22
- if (this.fromNodeMap.has(node.id)) return false;
23
- this.fromNodeMap.set(node.id, {
24
- node,
25
- meta
26
- });
27
- return true;
28
- }
29
- [inspect]() {
30
- return this.toJSON();
31
- }
32
- toJSON() {
33
- return {
34
- val: this.val,
35
- toNodes: Array.from(this.toNodeMap.values()),
36
- fromNodes: Array.from(this.fromNodeMap.values())
37
- };
38
- }
39
- toString() {
40
- return this.val.toString();
41
- }
42
- };
43
- var GraphPath = class {
44
- nodeIdMap = /* @__PURE__ */ new Map();
45
- nodes = [];
46
- pushVertex(node, meta) {
47
- const val = this.nodeIdMap.get(node.id) || 0;
48
- this.nodeIdMap.set(node.id, val + 1);
49
- this.nodes.push({
50
- node,
51
- meta
52
- });
53
- return val === 0;
54
- }
55
- popVertex() {
56
- const nodeHandler = this.nodes.pop();
57
- if (nodeHandler) {
58
- const val = this.nodeIdMap.get(nodeHandler.node.id);
59
- this.nodeIdMap.set(nodeHandler.node.id, val - 1);
60
- }
61
- }
62
- toString() {
63
- return this.nodes.reduce((p, c) => {
64
- let msg = "";
65
- if (c.meta) msg += ` ${c.meta.toString()} -> `;
66
- else if (p.length) msg += " -> ";
67
- msg += c.node.val.toString();
68
- p.push(msg);
69
- return p;
70
- }, new Array()).join("");
71
- }
72
- [inspect]() {
73
- return this.toString();
74
- }
75
- };
76
- var Graph = class {
77
- nodes = /* @__PURE__ */ new Map();
78
- addVertex(node) {
79
- if (this.nodes.has(node.id)) return false;
80
- this.nodes.set(node.id, node);
81
- return true;
82
- }
83
- addEdge(from, to, meta) {
84
- to.addFromVertex(from, meta);
85
- return from.addToVertex(to, meta);
86
- }
87
- findToNode(id, meta) {
88
- const node = this.nodes.get(id);
89
- if (!node) return void 0;
90
- for (const { node: toNode, meta: edgeMeta } of node.toNodeMap.values()) if (edgeMeta && meta.equal(edgeMeta)) return toNode;
91
- }
92
- appendVertexToPath(node, accessPath, meta) {
93
- if (!accessPath.pushVertex(node, meta)) return false;
94
- for (const toNode of node.toNodeMap.values()) if (!this.appendVertexToPath(toNode.node, accessPath, toNode.meta)) return false;
95
- accessPath.popVertex();
96
- return true;
97
- }
98
- loopPath() {
99
- const accessPath = new GraphPath();
100
- const nodes = Array.from(this.nodes.values());
101
- for (const node of nodes) if (!this.appendVertexToPath(node, accessPath)) return accessPath;
102
- }
103
- accessNode(node, nodes, accessed, res) {
104
- const index = nodes.indexOf(node);
105
- if (accessed[index]) return;
106
- if (!node.toNodeMap.size) {
107
- accessed[nodes.indexOf(node)] = true;
108
- res.push(node);
109
- return;
110
- }
111
- for (const toNode of node.toNodeMap.values()) this.accessNode(toNode.node, nodes, accessed, res);
112
- accessed[nodes.indexOf(node)] = true;
113
- res.push(node);
114
- }
115
- sort() {
116
- const res = [];
117
- const nodes = Array.from(this.nodes.values());
118
- const accessed = [];
119
- for (let i = 0; i < nodes.length; ++i) accessed.push(false);
120
- for (let i = 0; i < nodes.length; ++i) {
121
- const node = nodes[i];
122
- this.accessNode(node, nodes, accessed, res);
123
- }
124
- return res;
125
- }
126
- };
127
-
128
- //#endregion
129
- export { Graph, GraphNode, GraphPath };
package/dist/MapUtil.d.ts DELETED
@@ -1,6 +0,0 @@
1
- //#region src/MapUtil.d.ts
2
- declare class MapUtil {
3
- static getOrStore<K, V>(map: Map<K, V>, key: K, value: V): V;
4
- }
5
- //#endregion
6
- export { MapUtil };
package/dist/MapUtil.js DELETED
@@ -1,10 +0,0 @@
1
- //#region src/MapUtil.ts
2
- var MapUtil = class {
3
- static getOrStore(map, key, value) {
4
- if (!map.has(key)) map.set(key, value);
5
- return map.get(key);
6
- }
7
- };
8
-
9
- //#endregion
10
- export { MapUtil };
@@ -1,24 +0,0 @@
1
- import { InlineModuleReferenceConfig, ModuleConfig, ModuleReference, ModuleReferenceConfig, NpmModuleReferenceConfig, ReadModuleReferenceOptions } from "@eggjs/tegg-types";
2
-
3
- //#region src/ModuleConfig.d.ts
4
- declare class ModuleReferenceConfigHelp {
5
- static isInlineModuleReference(moduleReference: ModuleReferenceConfig): moduleReference is InlineModuleReferenceConfig;
6
- static isNpmModuleReference(moduleReference: ModuleReferenceConfig): moduleReference is NpmModuleReferenceConfig;
7
- }
8
- declare class ModuleConfigUtil {
9
- #private;
10
- static configNames: string[] | undefined;
11
- static setConfigNames(configNames: string[] | undefined): void;
12
- static readModuleReference(baseDir: string, options?: ReadModuleReferenceOptions): readonly ModuleReference[];
13
- private static readModuleReferenceFromModuleJson;
14
- private static readModuleReferenceFromScan;
15
- static readModuleFromNodeModules(baseDir: string): ModuleReference[];
16
- static resolveModuleDir(moduleDir: string, baseDir?: string): string;
17
- private static getModuleName;
18
- static readModuleName(baseDir: string, moduleDir: string): Promise<string>;
19
- static readModuleNameSync(moduleDir: string, baseDir?: string): string;
20
- static loadModuleConfig(moduleDir: string, baseDir?: string, env?: string): Promise<ModuleConfig>;
21
- static loadModuleConfigSync(moduleDir: string, baseDir?: string, env?: string): ModuleConfig;
22
- }
23
- //#endregion
24
- export { ModuleConfigUtil, ModuleReferenceConfigHelp };
@@ -1,224 +0,0 @@
1
- import { FSUtil } from "./FSUtil.js";
2
- import assert from "node:assert";
3
- import fs, { promises } from "node:fs";
4
- import path from "node:path";
5
- import { extend } from "extend2";
6
- import { globbySync } from "globby";
7
- import { load } from "js-yaml";
8
- import { importResolve } from "@eggjs/utils";
9
-
10
- //#region src/ModuleConfig.ts
11
- var ModuleReferenceConfigHelp = class {
12
- static isInlineModuleReference(moduleReference) {
13
- return !!moduleReference.path;
14
- }
15
- static isNpmModuleReference(moduleReference) {
16
- return !!moduleReference.package;
17
- }
18
- };
19
- const DEFAULT_READ_MODULE_REF_OPTS = { deep: 10 };
20
- var ModuleConfigUtil = class ModuleConfigUtil {
21
- static configNames;
22
- static setConfigNames(configNames) {
23
- ModuleConfigUtil.configNames = configNames;
24
- }
25
- static readModuleReference(baseDir, options) {
26
- const configDir = path.join(baseDir, "config");
27
- const moduleJsonPath = path.join(configDir, "module.json");
28
- if (fs.existsSync(moduleJsonPath)) return this.readModuleReferenceFromModuleJson(configDir, moduleJsonPath, options?.cwd || baseDir);
29
- return this.readModuleReferenceFromScan(baseDir, options);
30
- }
31
- static readModuleReferenceFromModuleJson(configDir, moduleJsonPath, cwd) {
32
- const moduleJsonContent = fs.readFileSync(moduleJsonPath, "utf8");
33
- const moduleJson = JSON.parse(moduleJsonContent);
34
- const moduleReferenceList = [];
35
- for (const moduleReferenceConfig of moduleJson) {
36
- let moduleReference;
37
- if (ModuleReferenceConfigHelp.isNpmModuleReference(moduleReferenceConfig)) {
38
- const options = cwd ? { paths: [cwd] } : {};
39
- const pkgJson = path.posix.join(moduleReferenceConfig.package, "package.json");
40
- const file = importResolve(pkgJson, options);
41
- const modulePath = path.dirname(file);
42
- moduleReference = {
43
- path: modulePath,
44
- name: ModuleConfigUtil.readModuleNameSync(modulePath)
45
- };
46
- } else if (ModuleReferenceConfigHelp.isInlineModuleReference(moduleReferenceConfig)) {
47
- const modulePath = path.join(configDir, moduleReferenceConfig.path);
48
- moduleReference = {
49
- path: modulePath,
50
- name: ModuleConfigUtil.readModuleNameSync(modulePath)
51
- };
52
- } else throw new Error("unknown type of module reference config: " + JSON.stringify(moduleReferenceConfig));
53
- moduleReferenceList.push(moduleReference);
54
- }
55
- return moduleReferenceList;
56
- }
57
- static readModuleReferenceFromScan(baseDir, options) {
58
- const ref = [];
59
- const realOptions = Object.assign({}, DEFAULT_READ_MODULE_REF_OPTS, options);
60
- const packagePaths = globbySync([
61
- "**/package.json",
62
- "!**/node_modules",
63
- "!**/+(.*)/**",
64
- "!**/coverage",
65
- ...realOptions.extraFilePattern || []
66
- ], {
67
- cwd: baseDir,
68
- deep: realOptions.deep
69
- });
70
- const moduleDirSet = /* @__PURE__ */ new Set();
71
- for (const packagePath of packagePaths) {
72
- const absolutePkgPath = path.join(baseDir, packagePath);
73
- let realPkgPath;
74
- try {
75
- realPkgPath = fs.realpathSync(absolutePkgPath);
76
- } catch {
77
- continue;
78
- }
79
- const moduleDir = path.dirname(realPkgPath);
80
- if (moduleDirSet.has(moduleDir)) continue;
81
- moduleDirSet.add(moduleDir);
82
- let name;
83
- try {
84
- name = this.readModuleNameSync(moduleDir);
85
- } catch {
86
- continue;
87
- }
88
- ref.push({
89
- path: moduleDir,
90
- name
91
- });
92
- }
93
- const moduleReferences = this.readModuleFromNodeModules(baseDir);
94
- for (const moduleReference of moduleReferences) {
95
- const moduleBasePath = path.basename(moduleReference.path);
96
- moduleDirSet.forEach((modulePath) => {
97
- if (path.basename(modulePath) === moduleBasePath) throw new Error("duplicate import of module reference: " + moduleBasePath);
98
- });
99
- ref.push({
100
- path: moduleReference.path,
101
- name: moduleReference.name
102
- });
103
- }
104
- return ref;
105
- }
106
- static readModuleFromNodeModules(baseDir) {
107
- const ref = [];
108
- let pkgContent;
109
- try {
110
- pkgContent = fs.readFileSync(path.join(baseDir, "package.json"), "utf8");
111
- } catch {
112
- return [];
113
- }
114
- const pkg = JSON.parse(pkgContent);
115
- for (const dependencyKey of Object.keys(pkg.dependencies || {})) {
116
- let packageJsonPath;
117
- try {
118
- packageJsonPath = importResolve(`${dependencyKey}/package.json`, { paths: [baseDir] });
119
- } catch {
120
- continue;
121
- }
122
- const absolutePkgPath = path.dirname(packageJsonPath);
123
- const realPkgPath = fs.realpathSync(absolutePkgPath);
124
- try {
125
- const name = this.readModuleNameSync(realPkgPath);
126
- ref.push({
127
- path: realPkgPath,
128
- name
129
- });
130
- } catch {
131
- continue;
132
- }
133
- }
134
- return ref;
135
- }
136
- static resolveModuleDir(moduleDir, baseDir) {
137
- if (path.isAbsolute(moduleDir)) return moduleDir;
138
- assert(baseDir, "baseDir is required");
139
- return path.join(baseDir, "config", moduleDir);
140
- }
141
- static getModuleName(pkg) {
142
- assert(pkg.eggModule && pkg.eggModule.name, "eggModule.name not found in package.json");
143
- return pkg.eggModule.name;
144
- }
145
- static async readModuleName(baseDir, moduleDir) {
146
- moduleDir = ModuleConfigUtil.resolveModuleDir(moduleDir, baseDir);
147
- const pkgContent = await promises.readFile(path.join(moduleDir, "package.json"), "utf8");
148
- const pkg = JSON.parse(pkgContent);
149
- return ModuleConfigUtil.getModuleName(pkg);
150
- }
151
- static readModuleNameSync(moduleDir, baseDir) {
152
- moduleDir = ModuleConfigUtil.resolveModuleDir(moduleDir, baseDir);
153
- const pkgContent = fs.readFileSync(path.join(moduleDir, "package.json"), "utf8");
154
- const pkg = JSON.parse(pkgContent);
155
- return ModuleConfigUtil.getModuleName(pkg);
156
- }
157
- static async loadModuleConfig(moduleDir, baseDir, env) {
158
- const modulePath = ModuleConfigUtil.resolveModuleDir(moduleDir, baseDir);
159
- let configNames;
160
- if (env) configNames = ["module", `module.${env}`];
161
- else configNames = ModuleConfigUtil.configNames || ["module"];
162
- const target = {};
163
- for (const configName of configNames) {
164
- let config = await ModuleConfigUtil.#loadOne(modulePath, configName);
165
- if (configName === "module.default" && !config) config = await ModuleConfigUtil.#loadOne(modulePath, "module");
166
- if (config) extend(true, target, config);
167
- }
168
- return target;
169
- }
170
- static async #loadOne(moduleDir, configName) {
171
- const yamlConfigPath = path.join(moduleDir, `${configName}.yml`);
172
- let config = await ModuleConfigUtil.#loadYaml(yamlConfigPath);
173
- if (!config) {
174
- const jsonConfigPath = path.join(moduleDir, `${configName}.json`);
175
- config = await ModuleConfigUtil.#loadJson(jsonConfigPath);
176
- }
177
- return config;
178
- }
179
- static async #loadJson(moduleJsonPath) {
180
- if (!await FSUtil.fileExists(moduleJsonPath)) return;
181
- const moduleJsonContent = await promises.readFile(moduleJsonPath, "utf8");
182
- return JSON.parse(moduleJsonContent).config;
183
- }
184
- static async #loadYaml(moduleYamlPath) {
185
- if (!await FSUtil.fileExists(moduleYamlPath)) return;
186
- const moduleYamlContent = await promises.readFile(moduleYamlPath, "utf8");
187
- return load(moduleYamlContent);
188
- }
189
- static loadModuleConfigSync(moduleDir, baseDir, env) {
190
- const modulePath = ModuleConfigUtil.resolveModuleDir(moduleDir, baseDir);
191
- let configNames;
192
- if (env) configNames = ["module", `module.${env}`];
193
- else configNames = ModuleConfigUtil.configNames || ["module"];
194
- const target = {};
195
- for (const configName of configNames) {
196
- let config = ModuleConfigUtil.#loadOneSync(modulePath, configName);
197
- if (configName === "module.default" && !config) config = ModuleConfigUtil.#loadOneSync(modulePath, "module");
198
- if (config) extend(true, target, config);
199
- }
200
- return target;
201
- }
202
- static #loadOneSync(moduleDir, configName) {
203
- const yamlConfigPath = path.join(moduleDir, `${configName}.yml`);
204
- let config = ModuleConfigUtil.#loadYamlSync(yamlConfigPath);
205
- if (!config) {
206
- const jsonConfigPath = path.join(moduleDir, `${configName}.json`);
207
- config = ModuleConfigUtil.#loadJsonSync(jsonConfigPath);
208
- }
209
- return config;
210
- }
211
- static #loadJsonSync(moduleJsonPath) {
212
- if (!fs.existsSync(moduleJsonPath)) return;
213
- const moduleJsonContent = fs.readFileSync(moduleJsonPath, "utf8");
214
- return JSON.parse(moduleJsonContent).config;
215
- }
216
- static #loadYamlSync(moduleYamlPath) {
217
- if (!fs.existsSync(moduleYamlPath)) return;
218
- const moduleYamlContent = fs.readFileSync(moduleYamlPath, "utf8");
219
- return load(moduleYamlContent);
220
- }
221
- };
222
-
223
- //#endregion
224
- export { ModuleConfigUtil, ModuleReferenceConfigHelp };
@@ -1,10 +0,0 @@
1
- import { ModuleConfig as ModuleConfig$1, ModuleConfigHolder } from "@eggjs/tegg-types";
2
-
3
- //#region src/ModuleConfigs.d.ts
4
- declare class ModuleConfigs {
5
- readonly inner: Record<string, ModuleConfigHolder>;
6
- constructor(inner: Record<string, ModuleConfigHolder>);
7
- get(moduleName: string): ModuleConfig$1 | undefined;
8
- }
9
- //#endregion
10
- export { type ModuleConfig$1 as ModuleConfig, ModuleConfigs };
@@ -1,13 +0,0 @@
1
- //#region src/ModuleConfigs.ts
2
- var ModuleConfigs = class {
3
- inner;
4
- constructor(inner) {
5
- this.inner = inner;
6
- }
7
- get(moduleName) {
8
- return this.inner[moduleName]?.config;
9
- }
10
- };
11
-
12
- //#endregion
13
- export { ModuleConfigs };
@@ -1,6 +0,0 @@
1
- //#region src/NameUtil.d.ts
2
- declare class NameUtil {
3
- static getClassName(constructor: Function): string;
4
- }
5
- //#endregion
6
- export { NameUtil };
package/dist/NameUtil.js DELETED
@@ -1,9 +0,0 @@
1
- //#region src/NameUtil.ts
2
- var NameUtil = class {
3
- static getClassName(constructor) {
4
- return constructor.name[0].toLowerCase() + constructor.name.substring(1);
5
- }
6
- };
7
-
8
- //#endregion
9
- export { NameUtil };
@@ -1,10 +0,0 @@
1
- import { EggProtoImplClass } from "@eggjs/tegg-types";
2
-
3
- //#region src/ObjectUtils.d.ts
4
- declare class ObjectUtils {
5
- static getProperties(obj: object): string[];
6
- static getFunctionArgNameList(func: Function): string[];
7
- static getConstructorArgNameList(clazz: EggProtoImplClass): string[];
8
- }
9
- //#endregion
10
- export { ObjectUtils };
@@ -1,29 +0,0 @@
1
- //#region src/ObjectUtils.ts
2
- var ObjectUtils = class {
3
- static getProperties(obj) {
4
- const properties = [];
5
- do
6
- for (const property of Object.getOwnPropertyNames(obj)) properties.push(property);
7
- while ((obj = Object.getPrototypeOf(obj)) && obj !== Object.prototype);
8
- return properties;
9
- }
10
- static getFunctionArgNameList(func) {
11
- if (func.length === 0) return [];
12
- let sourcecode = func.toString();
13
- sourcecode = sourcecode.replace(/\/\*[\s\S]*?\*\//g, "").replace(/\/\/(.)*/g, "").replace(/{[\s\S]*}/, "").replace(/=>/g, "").trim();
14
- let argsString = sourcecode.substring(sourcecode.indexOf("(") + 1, sourcecode.length - 1);
15
- argsString = argsString.replace(/=\([\s\S]*\)/g, "");
16
- return argsString.split(",").map((arg) => {
17
- return arg.replace(/=[\s\S]*/g, "").trim();
18
- }).filter((arg) => arg.length);
19
- }
20
- static getConstructorArgNameList(clazz) {
21
- if (clazz.length === 0) return [];
22
- const constructorMatch = clazz.toString().match(/constructor\s*\(([^)]+)\)/);
23
- if (!constructorMatch) return [];
24
- return constructorMatch[1].split(",").map((param) => param.trim()).map((param) => param.match(/(\w+)\s*(?=\s*(?:=|\/\/|\s*$))/)).filter(Boolean).map((match) => match[0].trim());
25
- }
26
- };
27
-
28
- //#endregion
29
- export { ObjectUtils };
@@ -1,6 +0,0 @@
1
- //#region src/ProxyUtil.d.ts
2
- declare class ProxyUtil {
3
- static safeProxy<T extends object>(obj: T, getter: (obj: T, p: PropertyKey) => any): T;
4
- }
5
- //#endregion
6
- export { ProxyUtil };
package/dist/ProxyUtil.js DELETED
@@ -1,12 +0,0 @@
1
- //#region src/ProxyUtil.ts
2
- var ProxyUtil = class {
3
- static safeProxy(obj, getter) {
4
- return new Proxy(obj, { get(target, p) {
5
- if (Object.prototype[p]) return Reflect.get(target, p);
6
- return getter(target, p);
7
- } });
8
- }
9
- };
10
-
11
- //#endregion
12
- export { ProxyUtil };
@@ -1,6 +0,0 @@
1
- //#region src/StackUtil.d.ts
2
- declare class StackUtil {
3
- static getCalleeFromStack(withLine: boolean, stackIndex?: number): string;
4
- }
5
- //#endregion
6
- export { StackUtil };
package/dist/StackUtil.js DELETED
@@ -1,39 +0,0 @@
1
- import { fileURLToPath } from "node:url";
2
- import { debuglog } from "node:util";
3
-
4
- //#region src/StackUtil.ts
5
- const debug = debuglog("@eggjs/tegg-common-util/StackUtil");
6
- /**
7
- * Capture call site stack from v8.
8
- * https://github.com/v8/v8/wiki/Stack-Trace-API
9
- */
10
- function prepareObjectStackTrace(_, stack) {
11
- return stack;
12
- }
13
- var StackUtil = class {
14
- static getCalleeFromStack(withLine, stackIndex) {
15
- stackIndex = stackIndex === void 0 ? 2 : stackIndex;
16
- const limit = Error.stackTraceLimit;
17
- const prep = Error.prepareStackTrace;
18
- Error.prepareStackTrace = prepareObjectStackTrace;
19
- Error.stackTraceLimit = 10;
20
- const obj = { stack: [] };
21
- Error.captureStackTrace(obj);
22
- if (debug.enabled) debug("call stack: %o", obj.stack.map((callSite$1) => callSite$1.getFileName() ?? "<anonymous>").join("\n"));
23
- const callSite = obj.stack[stackIndex];
24
- let fileName;
25
- if (callSite) {
26
- fileName = callSite.getFileName();
27
- if (fileName?.startsWith("file://")) fileName = fileURLToPath(fileName);
28
- }
29
- Error.prepareStackTrace = prep;
30
- Error.stackTraceLimit = limit;
31
- /* istanbul ignore if */
32
- if (!callSite || !fileName) return "<anonymous>";
33
- if (!withLine) return fileName;
34
- return `${fileName}:${callSite.getLineNumber()}:${callSite.getColumnNumber()}`;
35
- }
36
- };
37
-
38
- //#endregion
39
- export { StackUtil };
@@ -1,6 +0,0 @@
1
- //#region src/TimerUtil.d.ts
2
- declare class TimerUtil {
3
- static sleep(ms: number): Promise<void>;
4
- }
5
- //#endregion
6
- export { TimerUtil };
package/dist/TimerUtil.js DELETED
@@ -1,9 +0,0 @@
1
- //#region src/TimerUtil.ts
2
- var TimerUtil = class {
3
- static async sleep(ms) {
4
- await new Promise((resolve) => setTimeout(resolve, ms));
5
- }
6
- };
7
-
8
- //#endregion
9
- export { TimerUtil };