Skip to content

Commit aa1770c

Browse files
committed
Refactor project-management module and core package loading
Updated the project-management module to export package metadata and lifecycle handlers, enabled JSON module resolution in tsconfig, and marked the package as private. Refactored ObjectQL core to prefer loading packages by name with fallback to directory, improving flexibility in package loading.
1 parent 78cb074 commit aa1770c

4 files changed

Lines changed: 42 additions & 16 deletions

File tree

examples/modules/project-management/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "@example/project-management",
33
"version": "0.1.0",
4+
"private": true,
45
"main": "dist/index.js",
56
"types": "dist/index.d.ts",
67
"files": [
Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,27 @@
1-
import * as path from 'path';
1+
// 从 package.json 导出当前软件包的基本定义
22

3-
// 导出包的根目录路径,供 ObjectQL 加载器使用
4-
export const packageDir = path.resolve(__dirname);
3+
import pkg from '../package.json';
4+
5+
export default {
6+
7+
name: pkg.name,
8+
version: pkg.version,
9+
10+
/**
11+
* Service created lifecycle event handler
12+
*/
13+
created() {
14+
},
15+
16+
/**
17+
* Service started lifecycle event handler
18+
*/
19+
async started() {
20+
},
21+
22+
/**
23+
* Service stopped lifecycle event handler
24+
*/
25+
async stopped() {
26+
}
27+
}

examples/modules/project-management/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"extends": "../../../tsconfig.base.json",
33
"compilerOptions": {
44
"outDir": "./dist",
5-
"rootDir": "./src"
5+
"rootDir": "./src",
6+
"resolveJsonModule": true
67
},
78
"include": ["src/**/*"]
89
}

packages/core/src/index.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,24 @@ export class ObjectQL implements IObjectQL {
2424
}
2525
}
2626
if (config.packages) {
27-
for (const dir of config.packages) {
28-
this.loadFromDirectory(dir);
27+
for (const name of config.packages) {
28+
try {
29+
this.loadFromPackage(name);
30+
} catch (e) {
31+
this.loadFromDirectory(name);
32+
}
2933
}
3034
}
3135
}
3236

33-
loadFromDirectory(dir: string) {
34-
let packageDir = dir;
35-
try {
36-
// Try to resolve as a module
37-
const entryPath = require.resolve(dir, { paths: [process.cwd()] });
38-
packageDir = path.dirname(entryPath);
39-
} catch (e) {
40-
// Ignore error, assume it is a path
41-
}
37+
loadFromPackage(name: string) {
38+
const entryPath = require.resolve(name, { paths: [process.cwd()] });
39+
const packageDir = path.dirname(entryPath);
40+
this.loadFromDirectory(packageDir);
41+
}
4242

43-
const objects = loadObjectConfigs(packageDir);
43+
loadFromDirectory(dir: string) {
44+
const objects = loadObjectConfigs(dir);
4445
for (const obj of Object.values(objects)) {
4546
this.registerObject(obj);
4647
}

0 commit comments

Comments
 (0)