@supacloud/compiler 0.1.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.
@@ -0,0 +1,110 @@
1
+ /**
2
+ * ApplicationGraph:编译器从源码 AST 构建出的静态应用图。
3
+ * 运行期不反射、无容器,所有信息都在该结构与生成代码中显式给出。
4
+ */
5
+ export type Scope = "application" | "request" | "job";
6
+ export type ProviderKind = "class" | "value" | "factory" | "existing";
7
+ export type TokenKind = "injection-token" | "class";
8
+ export interface Diagnostic {
9
+ severity: "error" | "warn";
10
+ code: string;
11
+ message: string;
12
+ file?: string;
13
+ line?: number;
14
+ }
15
+ export interface ProviderNode {
16
+ /** token 名(InjectionToken 变量名或类名)。 */
17
+ token: string;
18
+ tokenKind: TokenKind;
19
+ kind: ProviderKind;
20
+ useClass?: string;
21
+ useValueExpr?: string;
22
+ useFactoryName?: string;
23
+ useExisting?: string;
24
+ scope: Scope;
25
+ /** token 名,构造/工厂参数顺序。 */
26
+ deps: string[];
27
+ exported: boolean;
28
+ file: string;
29
+ line: number;
30
+ /** useClass/useFactory/useValue 符号的模块相对路径(供生成 import)。 */
31
+ importPath?: string;
32
+ }
33
+ export interface RouteNode {
34
+ method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
35
+ path: string;
36
+ handler: string;
37
+ body?: string;
38
+ params?: string;
39
+ query?: string;
40
+ response?: string;
41
+ }
42
+ export interface ControllerNode {
43
+ className: string;
44
+ path: string;
45
+ scope: Scope;
46
+ deps: string[];
47
+ routes: RouteNode[];
48
+ file: string;
49
+ importPath: string;
50
+ /** 路由 schema 符号名 → 模块相对路径(供生成 import)。 */
51
+ schemaImports?: Record<string, string>;
52
+ }
53
+ export interface CommandNode {
54
+ className: string;
55
+ name: string;
56
+ permission?: string;
57
+ transaction?: string;
58
+ audit?: string;
59
+ idempotency?: string;
60
+ }
61
+ export interface QueryNode {
62
+ className: string;
63
+ name: string;
64
+ }
65
+ export interface ModuleNode {
66
+ /** @Module({ name }) 或 defineModule 的 name。 */
67
+ name: string;
68
+ className: string;
69
+ file: string;
70
+ line: number;
71
+ /** 被 import 模块的 name。 */
72
+ imports: string[];
73
+ providers: ProviderNode[];
74
+ controllers: ControllerNode[];
75
+ commands: CommandNode[];
76
+ queries: QueryNode[];
77
+ /** 导出的 token 名。 */
78
+ exports: string[];
79
+ }
80
+ export interface ApplicationGraph {
81
+ modules: ModuleNode[];
82
+ /** 被依赖但无任何模块提供的 token 名(平台注入)。 */
83
+ externalTokens: string[];
84
+ /**
85
+ * 分析阶段产生的诊断(如 missing-deps),由 compileProject 合并进结果。
86
+ * 不写入 app.manifest.json。
87
+ */
88
+ diagnostics?: Diagnostic[];
89
+ /**
90
+ * InjectionToken 变量名 → 字符串 name(如 REQUEST_CONTEXT →
91
+ * "supacloud.request-context"),供代码生成识别内置上下文 token。
92
+ * 不写入 app.manifest.json。
93
+ */
94
+ tokenNames?: Record<string, string>;
95
+ }
96
+ export interface CompileOptions {
97
+ /** 项目根(含 tsconfig)。 */
98
+ rootDir: string;
99
+ /** glob,默认 ['**\/*.module.ts', '**\/*.ts']。 */
100
+ include?: string[];
101
+ /** 生成目录(如 <rootDir>/generated)。 */
102
+ outDir: string;
103
+ /** warn 级诊断升级为 error。 */
104
+ strict?: boolean;
105
+ }
106
+ export interface CompileResult {
107
+ diagnostics: Diagnostic[];
108
+ graph: ApplicationGraph;
109
+ written: string[];
110
+ }
package/dist/util.d.ts ADDED
@@ -0,0 +1,15 @@
1
+ /**
2
+ * token 名 → services 对象的 key。
3
+ * CASE_REPOSITORY → caseRepository、LOGGER → logger(常量命名转 camelCase),
4
+ * CaseService → caseService(PascalCase 首字母小写)。
5
+ */
6
+ export declare function camelName(token: string): string;
7
+ /** 计算 from 目录到 to 文件的相对 import 路径(去扩展名,保证 ./ 或 ../ 前缀)。 */
8
+ export declare function relativeImportPath(fromDir: string, toFile: string): string;
9
+ /** 内置上下文 token 的字符串 name(与 @supacloud/app 的 REQUEST_CONTEXT/JOB_CONTEXT 对齐)。 */
10
+ export declare const REQUEST_CONTEXT_TOKEN_NAME = "supacloud.request-context";
11
+ export declare const JOB_CONTEXT_TOKEN_NAME = "supacloud.job-context";
12
+ /** 判断 token 是否为内置 request 上下文(变量名 REQUEST_CONTEXT 或 token name 匹配)。 */
13
+ export declare function isRequestContextToken(token: string, tokenNames?: Record<string, string>): boolean;
14
+ /** 判断 token 是否为内置 job 上下文。 */
15
+ export declare function isJobContextToken(token: string, tokenNames?: Record<string, string>): boolean;
@@ -0,0 +1,6 @@
1
+ import type { ApplicationGraph, Diagnostic } from "./types";
2
+ /**
3
+ * 校验 ApplicationGraph,产出诊断列表。
4
+ * strict 时 warn 级诊断升级为 error。
5
+ */
6
+ export declare function validateGraph(graph: ApplicationGraph, strict?: boolean): Diagnostic[];
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@supacloud/compiler",
3
+ "version": "0.1.0",
4
+ "description": "Static compiler for @supacloud/app metadata: builds the application graph from AST, validates it, and generates reflection-free factory code",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js",
13
+ "default": "./dist/index.js"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist",
18
+ "README.md"
19
+ ],
20
+ "scripts": {
21
+ "build": "bun run clean && bun run build:js && bun run build:types",
22
+ "build:js": "bun build src/index.ts --outdir dist --target node --external ts-morph",
23
+ "build:types": "tsc -p tsconfig.json --emitDeclarationOnly",
24
+ "clean": "rm -rf dist",
25
+ "prepublishOnly": "bun run build",
26
+ "test": "bun test",
27
+ "typecheck": "tsc -p tsconfig.json --noEmit",
28
+ "typecheck:test": "tsc -p tsconfig.test.json --noEmit"
29
+ },
30
+ "keywords": [
31
+ "supacloud",
32
+ "compiler",
33
+ "di",
34
+ "codegen",
35
+ "elysia"
36
+ ],
37
+ "license": "MIT",
38
+ "repository": {
39
+ "type": "git",
40
+ "url": "https://github.com/vibeunion/supacloud.git",
41
+ "directory": "packages/compiler"
42
+ },
43
+ "dependencies": {
44
+ "ts-morph": "^26.0.0"
45
+ },
46
+ "devDependencies": {
47
+ "@types/bun": "^1.4.0",
48
+ "typescript": "^7.0.2"
49
+ }
50
+ }