@ubean/codegen 0.1.7 → 0.1.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 +2 -0
- package/dist/index.js +39 -25
- package/package.json +5 -5
package/dist/index.d.ts
CHANGED
|
@@ -2,6 +2,8 @@ import { AutoImportOptions } from "@ubean/auto-imports";
|
|
|
2
2
|
import { ScanResult } from "@ubean/routing";
|
|
3
3
|
//#region src/route-types.d.ts
|
|
4
4
|
interface RouteTypesOptions {
|
|
5
|
+
/** Project root directory (absolute). Used to compute portable relative `filePath` values. */
|
|
6
|
+
cwd: string;
|
|
5
7
|
outDir: string;
|
|
6
8
|
fileName?: string;
|
|
7
9
|
}
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,22 @@
|
|
|
1
1
|
import { mkdir, writeFile } from "node:fs/promises";
|
|
2
2
|
import { generateAutoImports } from "@ubean/auto-imports";
|
|
3
|
-
import { join } from "pathe";
|
|
3
|
+
import { isAbsolute, join, relative } from "pathe";
|
|
4
4
|
import openapiTS, { astToString } from "openapi-typescript";
|
|
5
5
|
//#region src/route-types.ts
|
|
6
|
+
/**
|
|
7
|
+
* Compute a portable file path for a scanned route.
|
|
8
|
+
*
|
|
9
|
+
* - If `fullPath` is under `cwd`, returns a POSIX-style project-relative path
|
|
10
|
+
* (e.g. `src/routes/api/users/[id].ts`). This keeps generated `.d.ts` files
|
|
11
|
+
* portable across machines and free of leaked absolute paths.
|
|
12
|
+
* - Otherwise, falls back to the raw `fullPath` (already non-sensitive for
|
|
13
|
+
* non-local sources).
|
|
14
|
+
*/
|
|
15
|
+
function toPortableFilePath(route, cwd) {
|
|
16
|
+
return (isAbsolute(route.fullPath) ? relative(cwd, route.fullPath) : route.fullPath).replace(/\\/g, "/");
|
|
17
|
+
}
|
|
6
18
|
async function generateRouteTypes(result, options) {
|
|
7
|
-
const { outDir, fileName = "routes.d.ts" } = options;
|
|
19
|
+
const { cwd, outDir, fileName = "routes.d.ts" } = options;
|
|
8
20
|
await mkdir(outDir, { recursive: true });
|
|
9
21
|
const apiRoutes = result.apiRoutes;
|
|
10
22
|
const lines = [
|
|
@@ -21,19 +33,29 @@ async function generateRouteTypes(result, options) {
|
|
|
21
33
|
" }",
|
|
22
34
|
""
|
|
23
35
|
];
|
|
24
|
-
|
|
25
|
-
|
|
36
|
+
if (apiRoutes.length > 0) {
|
|
37
|
+
const groups = /* @__PURE__ */ new Map();
|
|
38
|
+
for (const r of apiRoutes) {
|
|
39
|
+
const list = groups.get(r.route);
|
|
40
|
+
if (list) list.push(r);
|
|
41
|
+
else groups.set(r.route, [r]);
|
|
42
|
+
}
|
|
43
|
+
const entries = [];
|
|
44
|
+
for (const [path, routes] of groups) {
|
|
45
|
+
const methodEntries = routes.map((r) => {
|
|
46
|
+
const method = (r.method || "ALL").toUpperCase();
|
|
47
|
+
const filePath = toPortableFilePath(r, cwd);
|
|
48
|
+
return ` ${method}: { filePath: ${JSON.stringify(filePath)} }`;
|
|
49
|
+
}).join(";\n");
|
|
50
|
+
entries.push(` ${JSON.stringify(path)}: {\n${methodEntries}\n }`);
|
|
51
|
+
}
|
|
26
52
|
lines.push(" export type ApiRouteMap = {");
|
|
27
|
-
lines.push(
|
|
53
|
+
lines.push(`${entries.join(";\n")};`);
|
|
28
54
|
lines.push(" };");
|
|
29
55
|
} else lines.push(" export type ApiRouteMap = {};");
|
|
30
56
|
lines.push("");
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
lines.push(" export type ApiRoutePath =");
|
|
34
|
-
lines.push(routePaths.map((p) => ` | ${JSON.stringify(p)}`).join("\n"));
|
|
35
|
-
lines.push(" ;");
|
|
36
|
-
} else lines.push(" export type ApiRoutePath = string;");
|
|
57
|
+
if (apiRoutes.length > 0) lines.push(" export type ApiRoutePath = keyof ApiRouteMap;");
|
|
58
|
+
else lines.push(" export type ApiRoutePath = string;");
|
|
37
59
|
lines.push("");
|
|
38
60
|
const routeMethods = [...new Set(apiRoutes.map((r) => r.method?.toUpperCase()).filter(Boolean))].sort();
|
|
39
61
|
if (routeMethods.length > 0) {
|
|
@@ -90,19 +112,8 @@ async function generatePageTypes(result, options) {
|
|
|
90
112
|
lines.push(" ;");
|
|
91
113
|
} else lines.push(" export type LayoutName = string;");
|
|
92
114
|
lines.push("");
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
lines.push(" export const pages: {");
|
|
96
|
-
lines.push(pageEntries.join(",\n"));
|
|
97
|
-
lines.push(" };");
|
|
98
|
-
} else lines.push(" export const pages: Record<string, PageInfo>;");
|
|
99
|
-
lines.push("");
|
|
100
|
-
const layoutEntries = layouts.map((l) => ` ${JSON.stringify(l.name)}: { name: ${JSON.stringify(l.name)}, filePath: ${JSON.stringify(l.fullPath)}, isDefault: ${l.isDefault} }`);
|
|
101
|
-
if (layoutEntries.length > 0) {
|
|
102
|
-
lines.push(" export const layouts: {");
|
|
103
|
-
lines.push(layoutEntries.join(",\n"));
|
|
104
|
-
lines.push(" };");
|
|
105
|
-
} else lines.push(" export const layouts: Record<string, LayoutInfo>;");
|
|
115
|
+
lines.push(" export const pages: Record<RouteName, PageInfo>;");
|
|
116
|
+
lines.push(" export const layouts: Record<LayoutName, LayoutInfo>;");
|
|
106
117
|
lines.push("}");
|
|
107
118
|
const content = `${lines.join("\n")}\n`;
|
|
108
119
|
const filePath = join(outDir, fileName);
|
|
@@ -153,7 +164,10 @@ async function generateTypes(result, options) {
|
|
|
153
164
|
const outDir = join(cwd, buildDir);
|
|
154
165
|
await mkdir(outDir, { recursive: true });
|
|
155
166
|
const generated = [];
|
|
156
|
-
const routeTypesPath = await generateRouteTypes(result, {
|
|
167
|
+
const routeTypesPath = await generateRouteTypes(result, {
|
|
168
|
+
cwd,
|
|
169
|
+
outDir
|
|
170
|
+
});
|
|
157
171
|
generated.push(routeTypesPath);
|
|
158
172
|
const pageTypesPath = await generatePageTypes(result, { outDir });
|
|
159
173
|
generated.push(pageTypesPath);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ubean/codegen",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.8",
|
|
4
4
|
"description": "Type generation for ubean (generateTypes, routes.d.ts, pages.d.ts, openapi.d.ts)",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist"
|
|
@@ -18,12 +18,12 @@
|
|
|
18
18
|
"dependencies": {
|
|
19
19
|
"openapi-typescript": "^7.13.0",
|
|
20
20
|
"pathe": "^2.0.3",
|
|
21
|
-
"@ubean/auto-imports": "0.1.
|
|
22
|
-
"@ubean/routing": "0.1.
|
|
21
|
+
"@ubean/auto-imports": "0.1.8",
|
|
22
|
+
"@ubean/routing": "0.1.8"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
|
-
"@types/node": "^26.1.
|
|
26
|
-
"typescript": "
|
|
25
|
+
"@types/node": "^26.1.2",
|
|
26
|
+
"typescript": "7.0.2",
|
|
27
27
|
"vite-plus": "0.2.6"
|
|
28
28
|
},
|
|
29
29
|
"scripts": {
|