@eggjs/tegg-common-util 4.0.0-beta.8 → 4.0.1-beta.0
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/FSUtil.d.ts +6 -0
- package/dist/FSUtil.js +16 -0
- package/dist/Graph.d.ts +46 -0
- package/dist/Graph.js +121 -0
- package/dist/MapUtil.d.ts +6 -0
- package/dist/MapUtil.js +10 -0
- package/dist/ModuleConfig.d.ts +30 -0
- package/dist/ModuleConfig.js +259 -0
- package/dist/ModuleConfigs.d.ts +10 -0
- package/dist/ModuleConfigs.js +13 -0
- package/dist/NameUtil.d.ts +6 -0
- package/dist/NameUtil.js +9 -0
- package/dist/ObjectUtils.d.ts +10 -0
- package/dist/ObjectUtils.js +29 -0
- package/dist/ProxyUtil.d.ts +6 -0
- package/dist/ProxyUtil.js +12 -0
- package/dist/StackUtil.d.ts +12 -0
- package/dist/StackUtil.js +68 -0
- package/dist/StreamUtil.d.ts +6 -0
- package/dist/StreamUtil.js +11 -0
- package/dist/TimerUtil.d.ts +11 -0
- package/dist/TimerUtil.js +26 -0
- package/dist/index.d.ts +12 -134
- package/dist/index.js +12 -477
- package/package.json +34 -38
package/dist/FSUtil.d.ts
ADDED
package/dist/FSUtil.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
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, fs.constants.F_OK);
|
|
8
|
+
} catch {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
//#endregion
|
|
16
|
+
export { FSUtil };
|
package/dist/Graph.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { GraphNodeObj } from "@eggjs/tegg-types";
|
|
2
|
+
|
|
3
|
+
//#region src/Graph.d.ts
|
|
4
|
+
interface EdgeMeta {
|
|
5
|
+
equal(meta: EdgeMeta): boolean;
|
|
6
|
+
toString(): string;
|
|
7
|
+
}
|
|
8
|
+
declare class GraphNode<T extends GraphNodeObj, M extends EdgeMeta = EdgeMeta> {
|
|
9
|
+
val: T;
|
|
10
|
+
toNodeMap: Map<string, {
|
|
11
|
+
node: GraphNode<T, M>;
|
|
12
|
+
meta?: M;
|
|
13
|
+
}>;
|
|
14
|
+
fromNodeMap: Map<string, {
|
|
15
|
+
node: GraphNode<T, M>;
|
|
16
|
+
meta?: M;
|
|
17
|
+
}>;
|
|
18
|
+
constructor(val: T);
|
|
19
|
+
get id(): string;
|
|
20
|
+
addToVertex(node: GraphNode<T, M>, meta?: M): boolean;
|
|
21
|
+
addFromVertex(node: GraphNode<T, M>, meta?: M): boolean;
|
|
22
|
+
toJSON(): object;
|
|
23
|
+
toString(): string;
|
|
24
|
+
}
|
|
25
|
+
declare class GraphPath<T extends GraphNodeObj, M extends EdgeMeta = EdgeMeta> {
|
|
26
|
+
nodeIdMap: Map<string, number>;
|
|
27
|
+
nodes: Array<{
|
|
28
|
+
node: GraphNode<T, M>;
|
|
29
|
+
meta?: M;
|
|
30
|
+
}>;
|
|
31
|
+
pushVertex(node: GraphNode<T, M>, meta?: M): boolean;
|
|
32
|
+
popVertex(): void;
|
|
33
|
+
toString(): string;
|
|
34
|
+
}
|
|
35
|
+
declare class Graph<T extends GraphNodeObj, M extends EdgeMeta = EdgeMeta> {
|
|
36
|
+
nodes: Map<string, GraphNode<T, M>>;
|
|
37
|
+
addVertex(node: GraphNode<T, M>): boolean;
|
|
38
|
+
addEdge(from: GraphNode<T, M>, to: GraphNode<T, M>, meta?: M): boolean;
|
|
39
|
+
findToNode(id: string, meta: M): GraphNode<T, M> | undefined;
|
|
40
|
+
appendVertexToPath(node: GraphNode<T, M>, accessPath: GraphPath<T, M>, meta?: M): boolean;
|
|
41
|
+
loopPath(): GraphPath<T, M> | undefined;
|
|
42
|
+
accessNode(node: GraphNode<T, M>, nodes: Array<GraphNode<T, M>>, accessed: boolean[], res: Array<GraphNode<T, M>>): void;
|
|
43
|
+
sort(): Array<GraphNode<T, M>>;
|
|
44
|
+
}
|
|
45
|
+
//#endregion
|
|
46
|
+
export { EdgeMeta, Graph, GraphNode, GraphPath };
|
package/dist/Graph.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
//#region src/Graph.ts
|
|
2
|
+
var GraphNode = class {
|
|
3
|
+
val;
|
|
4
|
+
toNodeMap = /* @__PURE__ */ new Map();
|
|
5
|
+
fromNodeMap = /* @__PURE__ */ new Map();
|
|
6
|
+
constructor(val) {
|
|
7
|
+
this.val = val;
|
|
8
|
+
}
|
|
9
|
+
get id() {
|
|
10
|
+
return this.val.id;
|
|
11
|
+
}
|
|
12
|
+
addToVertex(node, meta) {
|
|
13
|
+
if (this.toNodeMap.has(node.id)) return false;
|
|
14
|
+
this.toNodeMap.set(node.id, {
|
|
15
|
+
node,
|
|
16
|
+
meta
|
|
17
|
+
});
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
addFromVertex(node, meta) {
|
|
21
|
+
if (this.fromNodeMap.has(node.id)) return false;
|
|
22
|
+
this.fromNodeMap.set(node.id, {
|
|
23
|
+
node,
|
|
24
|
+
meta
|
|
25
|
+
});
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
toJSON() {
|
|
29
|
+
return {
|
|
30
|
+
val: this.val,
|
|
31
|
+
toNodes: Array.from(this.toNodeMap.values()),
|
|
32
|
+
fromNodes: Array.from(this.fromNodeMap.values())
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
toString() {
|
|
36
|
+
return this.val.toString();
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
var GraphPath = class {
|
|
40
|
+
nodeIdMap = /* @__PURE__ */ new Map();
|
|
41
|
+
nodes = [];
|
|
42
|
+
pushVertex(node, meta) {
|
|
43
|
+
const val = this.nodeIdMap.get(node.id) || 0;
|
|
44
|
+
this.nodeIdMap.set(node.id, val + 1);
|
|
45
|
+
this.nodes.push({
|
|
46
|
+
node,
|
|
47
|
+
meta
|
|
48
|
+
});
|
|
49
|
+
return val === 0;
|
|
50
|
+
}
|
|
51
|
+
popVertex() {
|
|
52
|
+
const nodeHandler = this.nodes.pop();
|
|
53
|
+
if (nodeHandler) {
|
|
54
|
+
const val = this.nodeIdMap.get(nodeHandler.node.id);
|
|
55
|
+
this.nodeIdMap.set(nodeHandler.node.id, val - 1);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
toString() {
|
|
59
|
+
return this.nodes.reduce((p, c) => {
|
|
60
|
+
let msg = "";
|
|
61
|
+
if (c.meta) msg += ` ${c.meta.toString()} -> `;
|
|
62
|
+
else if (p.length) msg += " -> ";
|
|
63
|
+
msg += c.node.val.toString();
|
|
64
|
+
p.push(msg);
|
|
65
|
+
return p;
|
|
66
|
+
}, new Array()).join("");
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
var Graph = class {
|
|
70
|
+
nodes = /* @__PURE__ */ new Map();
|
|
71
|
+
addVertex(node) {
|
|
72
|
+
if (this.nodes.has(node.id)) return false;
|
|
73
|
+
this.nodes.set(node.id, node);
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
addEdge(from, to, meta) {
|
|
77
|
+
to.addFromVertex(from, meta);
|
|
78
|
+
return from.addToVertex(to, meta);
|
|
79
|
+
}
|
|
80
|
+
findToNode(id, meta) {
|
|
81
|
+
const node = this.nodes.get(id);
|
|
82
|
+
if (!node) return void 0;
|
|
83
|
+
for (const { node: toNode, meta: edgeMeta } of node.toNodeMap.values()) if (edgeMeta && meta.equal(edgeMeta)) return toNode;
|
|
84
|
+
}
|
|
85
|
+
appendVertexToPath(node, accessPath, meta) {
|
|
86
|
+
if (!accessPath.pushVertex(node, meta)) return false;
|
|
87
|
+
for (const toNode of node.toNodeMap.values()) if (!this.appendVertexToPath(toNode.node, accessPath, toNode.meta)) return false;
|
|
88
|
+
accessPath.popVertex();
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
loopPath() {
|
|
92
|
+
const accessPath = new GraphPath();
|
|
93
|
+
const nodes = Array.from(this.nodes.values());
|
|
94
|
+
for (const node of nodes) if (!this.appendVertexToPath(node, accessPath)) return accessPath;
|
|
95
|
+
}
|
|
96
|
+
accessNode(node, nodes, accessed, res) {
|
|
97
|
+
if (accessed[nodes.indexOf(node)]) return;
|
|
98
|
+
if (!node.toNodeMap.size) {
|
|
99
|
+
accessed[nodes.indexOf(node)] = true;
|
|
100
|
+
res.push(node);
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
for (const toNode of node.toNodeMap.values()) this.accessNode(toNode.node, nodes, accessed, res);
|
|
104
|
+
accessed[nodes.indexOf(node)] = true;
|
|
105
|
+
res.push(node);
|
|
106
|
+
}
|
|
107
|
+
sort() {
|
|
108
|
+
const res = [];
|
|
109
|
+
const nodes = Array.from(this.nodes.values());
|
|
110
|
+
const accessed = [];
|
|
111
|
+
for (let i = 0; i < nodes.length; ++i) accessed.push(false);
|
|
112
|
+
for (let i = 0; i < nodes.length; ++i) {
|
|
113
|
+
const node = nodes[i];
|
|
114
|
+
this.accessNode(node, nodes, accessed, res);
|
|
115
|
+
}
|
|
116
|
+
return res;
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
//#endregion
|
|
121
|
+
export { Graph, GraphNode, GraphPath };
|
package/dist/MapUtil.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
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
|
+
* Deduplicate module references to avoid adding the same module multiple times.
|
|
24
|
+
* @param moduleReferences array of module references
|
|
25
|
+
* @return deduplicated module references
|
|
26
|
+
*/
|
|
27
|
+
static deduplicateModules(moduleReferences: readonly ModuleReference[]): readonly ModuleReference[];
|
|
28
|
+
}
|
|
29
|
+
//#endregion
|
|
30
|
+
export { ModuleConfigUtil, ModuleReferenceConfigHelp };
|
|
@@ -0,0 +1,259 @@
|
|
|
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 { importResolve } from "@eggjs/utils";
|
|
6
|
+
import { extend } from "extend2";
|
|
7
|
+
import globby from "globby";
|
|
8
|
+
import { load } from "js-yaml";
|
|
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 file = importResolve(path.posix.join(moduleReferenceConfig.package, "package.json"), options);
|
|
40
|
+
const modulePath = path.dirname(file);
|
|
41
|
+
moduleReference = {
|
|
42
|
+
path: modulePath,
|
|
43
|
+
name: ModuleConfigUtil.readModuleNameSync(modulePath)
|
|
44
|
+
};
|
|
45
|
+
} else if (ModuleReferenceConfigHelp.isInlineModuleReference(moduleReferenceConfig)) {
|
|
46
|
+
const modulePath = path.join(configDir, moduleReferenceConfig.path);
|
|
47
|
+
moduleReference = {
|
|
48
|
+
path: modulePath,
|
|
49
|
+
name: ModuleConfigUtil.readModuleNameSync(modulePath)
|
|
50
|
+
};
|
|
51
|
+
} else throw new Error("unknown type of module reference config: " + JSON.stringify(moduleReferenceConfig));
|
|
52
|
+
moduleReferenceList.push(moduleReference);
|
|
53
|
+
}
|
|
54
|
+
return moduleReferenceList;
|
|
55
|
+
}
|
|
56
|
+
static readModuleReferenceFromScan(baseDir, options) {
|
|
57
|
+
const ref = [];
|
|
58
|
+
const realOptions = Object.assign({}, DEFAULT_READ_MODULE_REF_OPTS, options);
|
|
59
|
+
const packagePaths = globby.sync([
|
|
60
|
+
"**/package.json",
|
|
61
|
+
"!**/node_modules",
|
|
62
|
+
"!**/+(.*)/**",
|
|
63
|
+
"!**/coverage",
|
|
64
|
+
...realOptions.extraFilePattern || []
|
|
65
|
+
], {
|
|
66
|
+
cwd: baseDir,
|
|
67
|
+
deep: realOptions.deep
|
|
68
|
+
});
|
|
69
|
+
const moduleDirSet = /* @__PURE__ */ new Set();
|
|
70
|
+
for (const packagePath of packagePaths) {
|
|
71
|
+
const absolutePkgPath = path.join(baseDir, packagePath);
|
|
72
|
+
let realPkgPath;
|
|
73
|
+
try {
|
|
74
|
+
realPkgPath = fs.realpathSync(absolutePkgPath);
|
|
75
|
+
} catch {
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
const moduleDir = path.dirname(realPkgPath);
|
|
79
|
+
if (moduleDirSet.has(moduleDir)) continue;
|
|
80
|
+
moduleDirSet.add(moduleDir);
|
|
81
|
+
let name;
|
|
82
|
+
try {
|
|
83
|
+
name = this.readModuleNameSync(moduleDir);
|
|
84
|
+
} catch {
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
ref.push({
|
|
88
|
+
path: moduleDir,
|
|
89
|
+
name
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
const moduleReferences = this.readModuleFromNodeModules(baseDir);
|
|
93
|
+
for (const moduleReference of moduleReferences) {
|
|
94
|
+
const moduleBasePath = path.basename(moduleReference.path);
|
|
95
|
+
moduleDirSet.forEach((modulePath) => {
|
|
96
|
+
if (path.basename(modulePath) === moduleBasePath) throw new Error("duplicate import of module reference: " + moduleBasePath);
|
|
97
|
+
});
|
|
98
|
+
ref.push({
|
|
99
|
+
path: moduleReference.path,
|
|
100
|
+
name: moduleReference.name
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
return ref;
|
|
104
|
+
}
|
|
105
|
+
static readModuleFromNodeModules(baseDir) {
|
|
106
|
+
const ref = [];
|
|
107
|
+
let pkgContent;
|
|
108
|
+
try {
|
|
109
|
+
pkgContent = fs.readFileSync(path.join(baseDir, "package.json"), "utf8");
|
|
110
|
+
} catch {
|
|
111
|
+
return [];
|
|
112
|
+
}
|
|
113
|
+
const pkg = JSON.parse(pkgContent);
|
|
114
|
+
for (const dependencyKey of Object.keys(pkg.dependencies || {})) {
|
|
115
|
+
let packageJsonPath;
|
|
116
|
+
try {
|
|
117
|
+
packageJsonPath = importResolve(`${dependencyKey}/package.json`, { paths: [baseDir] });
|
|
118
|
+
} catch {
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
const absolutePkgPath = path.dirname(packageJsonPath);
|
|
122
|
+
const realPkgPath = fs.realpathSync(absolutePkgPath);
|
|
123
|
+
try {
|
|
124
|
+
const name = this.readModuleNameSync(realPkgPath);
|
|
125
|
+
ref.push({
|
|
126
|
+
path: realPkgPath,
|
|
127
|
+
name
|
|
128
|
+
});
|
|
129
|
+
} catch {
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return ref;
|
|
134
|
+
}
|
|
135
|
+
static resolveModuleDir(moduleDir, baseDir) {
|
|
136
|
+
if (path.isAbsolute(moduleDir)) return moduleDir;
|
|
137
|
+
assert(baseDir, "baseDir is required");
|
|
138
|
+
return path.join(baseDir, "config", moduleDir);
|
|
139
|
+
}
|
|
140
|
+
static getModuleName(pkg) {
|
|
141
|
+
assert(pkg.eggModule && pkg.eggModule.name, "eggModule.name not found in package.json");
|
|
142
|
+
return pkg.eggModule.name;
|
|
143
|
+
}
|
|
144
|
+
static async readModuleName(baseDir, moduleDir) {
|
|
145
|
+
moduleDir = ModuleConfigUtil.resolveModuleDir(moduleDir, baseDir);
|
|
146
|
+
const pkgContent = await promises.readFile(path.join(moduleDir, "package.json"), "utf8");
|
|
147
|
+
const pkg = JSON.parse(pkgContent);
|
|
148
|
+
return ModuleConfigUtil.getModuleName(pkg);
|
|
149
|
+
}
|
|
150
|
+
static readModuleNameSync(moduleDir, baseDir) {
|
|
151
|
+
moduleDir = ModuleConfigUtil.resolveModuleDir(moduleDir, baseDir);
|
|
152
|
+
const pkgContent = fs.readFileSync(path.join(moduleDir, "package.json"), "utf8");
|
|
153
|
+
const pkg = JSON.parse(pkgContent);
|
|
154
|
+
return ModuleConfigUtil.getModuleName(pkg);
|
|
155
|
+
}
|
|
156
|
+
static async loadModuleConfig(moduleDir, baseDir, env) {
|
|
157
|
+
const modulePath = ModuleConfigUtil.resolveModuleDir(moduleDir, baseDir);
|
|
158
|
+
let configNames;
|
|
159
|
+
if (env) configNames = ["module", `module.${env}`];
|
|
160
|
+
else configNames = ModuleConfigUtil.configNames || ["module"];
|
|
161
|
+
const target = {};
|
|
162
|
+
for (const configName of configNames) {
|
|
163
|
+
let config = await ModuleConfigUtil.#loadOne(modulePath, configName);
|
|
164
|
+
if (configName === "module.default" && !config) config = await ModuleConfigUtil.#loadOne(modulePath, "module");
|
|
165
|
+
if (config) extend(true, target, config);
|
|
166
|
+
}
|
|
167
|
+
return target;
|
|
168
|
+
}
|
|
169
|
+
static async #loadOne(moduleDir, configName) {
|
|
170
|
+
const yamlConfigPath = path.join(moduleDir, `${configName}.yml`);
|
|
171
|
+
let config = await ModuleConfigUtil.#loadYaml(yamlConfigPath);
|
|
172
|
+
if (!config) {
|
|
173
|
+
const jsonConfigPath = path.join(moduleDir, `${configName}.json`);
|
|
174
|
+
config = await ModuleConfigUtil.#loadJson(jsonConfigPath);
|
|
175
|
+
}
|
|
176
|
+
return config;
|
|
177
|
+
}
|
|
178
|
+
static async #loadJson(moduleJsonPath) {
|
|
179
|
+
if (!await FSUtil.fileExists(moduleJsonPath)) return;
|
|
180
|
+
const moduleJsonContent = await promises.readFile(moduleJsonPath, "utf8");
|
|
181
|
+
return JSON.parse(moduleJsonContent).config;
|
|
182
|
+
}
|
|
183
|
+
static async #loadYaml(moduleYamlPath) {
|
|
184
|
+
if (!await FSUtil.fileExists(moduleYamlPath)) return;
|
|
185
|
+
return load(await promises.readFile(moduleYamlPath, "utf8"));
|
|
186
|
+
}
|
|
187
|
+
static loadModuleConfigSync(moduleDir, baseDir, env) {
|
|
188
|
+
const modulePath = ModuleConfigUtil.resolveModuleDir(moduleDir, baseDir);
|
|
189
|
+
let configNames;
|
|
190
|
+
if (env) configNames = ["module", `module.${env}`];
|
|
191
|
+
else configNames = ModuleConfigUtil.configNames || ["module"];
|
|
192
|
+
const target = {};
|
|
193
|
+
for (const configName of configNames) {
|
|
194
|
+
let config = ModuleConfigUtil.#loadOneSync(modulePath, configName);
|
|
195
|
+
if (configName === "module.default" && !config) config = ModuleConfigUtil.#loadOneSync(modulePath, "module");
|
|
196
|
+
if (config) extend(true, target, config);
|
|
197
|
+
}
|
|
198
|
+
return target;
|
|
199
|
+
}
|
|
200
|
+
static #loadOneSync(moduleDir, configName) {
|
|
201
|
+
const yamlConfigPath = path.join(moduleDir, `${configName}.yml`);
|
|
202
|
+
let config = ModuleConfigUtil.#loadYamlSync(yamlConfigPath);
|
|
203
|
+
if (!config) {
|
|
204
|
+
const jsonConfigPath = path.join(moduleDir, `${configName}.json`);
|
|
205
|
+
config = ModuleConfigUtil.#loadJsonSync(jsonConfigPath);
|
|
206
|
+
}
|
|
207
|
+
return config;
|
|
208
|
+
}
|
|
209
|
+
static #loadJsonSync(moduleJsonPath) {
|
|
210
|
+
if (!fs.existsSync(moduleJsonPath)) return;
|
|
211
|
+
const moduleJsonContent = fs.readFileSync(moduleJsonPath, "utf8");
|
|
212
|
+
return JSON.parse(moduleJsonContent).config;
|
|
213
|
+
}
|
|
214
|
+
static #loadYamlSync(moduleYamlPath) {
|
|
215
|
+
if (!fs.existsSync(moduleYamlPath)) return;
|
|
216
|
+
return load(fs.readFileSync(moduleYamlPath, "utf8"));
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Deduplicate module references to avoid adding the same module multiple times.
|
|
220
|
+
* @param moduleReferences array of module references
|
|
221
|
+
* @return deduplicated module references
|
|
222
|
+
*/
|
|
223
|
+
static deduplicateModules(moduleReferences) {
|
|
224
|
+
const moduleMap = /* @__PURE__ */ new Map();
|
|
225
|
+
const nameMap = /* @__PURE__ */ new Map();
|
|
226
|
+
for (const moduleRef of moduleReferences) {
|
|
227
|
+
const key = moduleRef.path;
|
|
228
|
+
const existingRef = moduleMap.get(key);
|
|
229
|
+
if (existingRef) {
|
|
230
|
+
const existingOptional = existingRef.optional ?? false;
|
|
231
|
+
const currentOptional = moduleRef.optional ?? false;
|
|
232
|
+
if (!existingOptional && currentOptional) continue;
|
|
233
|
+
else if (existingOptional && !currentOptional) {
|
|
234
|
+
if (existingRef.name !== moduleRef.name) {
|
|
235
|
+
const existingByName$1 = nameMap.get(moduleRef.name);
|
|
236
|
+
if (existingByName$1) throw new Error(`Duplicate module name "${moduleRef.name}" found: existing at ${existingByName$1.path}, duplicate at ${moduleRef.path}`);
|
|
237
|
+
}
|
|
238
|
+
const newModuleRef = {
|
|
239
|
+
...moduleRef,
|
|
240
|
+
optional: false
|
|
241
|
+
};
|
|
242
|
+
moduleMap.set(key, newModuleRef);
|
|
243
|
+
if (nameMap.get(existingRef.name) === existingRef) nameMap.delete(existingRef.name);
|
|
244
|
+
nameMap.set(newModuleRef.name, newModuleRef);
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
const existingByName = nameMap.get(moduleRef.name);
|
|
250
|
+
if (existingByName) throw new Error(`Duplicate module name "${moduleRef.name}" found: existing at ${existingByName.path}, duplicate at ${moduleRef.path}`);
|
|
251
|
+
moduleMap.set(key, moduleRef);
|
|
252
|
+
nameMap.set(moduleRef.name, moduleRef);
|
|
253
|
+
}
|
|
254
|
+
return Array.from(moduleMap.values());
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
//#endregion
|
|
259
|
+
export { ModuleConfigUtil, ModuleReferenceConfigHelp };
|
|
@@ -0,0 +1,10 @@
|
|
|
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 };
|
package/dist/NameUtil.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
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 };
|
|
@@ -0,0 +1,29 @@
|
|
|
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 };
|
|
@@ -0,0 +1,12 @@
|
|
|
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 };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
//#region src/StackUtil.d.ts
|
|
2
|
+
declare class StackUtil {
|
|
3
|
+
/**
|
|
4
|
+
* Get the callee file path from the stack.
|
|
5
|
+
* @param withLine - Whether to include the line number and column number in the result. Default is false.
|
|
6
|
+
* @param stackIndex - The index of the stack frame to get. Default is 2.
|
|
7
|
+
* @returns The callee file path from the stack.
|
|
8
|
+
*/
|
|
9
|
+
static getCalleeFromStack(withLine?: boolean, stackIndex?: number): string;
|
|
10
|
+
}
|
|
11
|
+
//#endregion
|
|
12
|
+
export { StackUtil };
|