@sixb/app 0.1.3 → 0.1.5
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/README.md +42 -12
- package/dist/agents.d.ts +5 -5
- package/dist/agents.d.ts.map +1 -1
- package/dist/agents.js +5 -4
- package/dist/agents.js.map +3 -3
- package/dist/codegen.d.ts +4 -3
- package/dist/codegen.d.ts.map +1 -1
- package/dist/createCustomApp.d.ts.map +1 -1
- package/dist/index.js +323 -102
- package/dist/index.js.map +6 -6
- package/dist/scanner.d.ts +24 -6
- package/dist/scanner.d.ts.map +1 -1
- package/package.json +7 -3
- package/src/agents.ts +12 -8
- package/src/build.ts +6 -4
- package/src/codegen.ts +233 -25
- package/src/createCustomApp.ts +10 -10
- package/src/scanner.ts +191 -46
package/src/scanner.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { readdir } from "node:fs/promises"
|
|
2
2
|
import { join, relative } from "node:path"
|
|
3
3
|
|
|
4
|
+
const dynamicSegmentPattern = /^\[([\w-]+)\]$/
|
|
5
|
+
const reservedRoutePrefixes = ["/api", "/auth", "/ws", "/docs", "/__sixb"] as const
|
|
6
|
+
const reservedRoutePaths = new Set(["/app.webmanifest"])
|
|
7
|
+
|
|
4
8
|
export interface PageRoute {
|
|
5
9
|
/** React Router path, e.g. "/" or "/remote/:id" */
|
|
6
10
|
path: string
|
|
@@ -10,72 +14,213 @@ export interface PageRoute {
|
|
|
10
14
|
relativePath: string
|
|
11
15
|
}
|
|
12
16
|
|
|
17
|
+
export interface AppRouteLayout {
|
|
18
|
+
/** React Router prefix owned by this layout, e.g. "/remote/:id". */
|
|
19
|
+
path: string
|
|
20
|
+
/** Absolute file path. */
|
|
21
|
+
filePath: string
|
|
22
|
+
/** Relative path from the app directory, e.g. "remote/[id]/layout.tsx". */
|
|
23
|
+
relativePath: string
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface AppRouteDiscovery {
|
|
27
|
+
readonly pages: readonly PageRoute[]
|
|
28
|
+
/** Descendant layouts only. The root layout remains the global app wrapper. */
|
|
29
|
+
readonly layouts: readonly AppRouteLayout[]
|
|
30
|
+
}
|
|
31
|
+
|
|
13
32
|
/**
|
|
14
|
-
* Recursively scans an `app/` directory for
|
|
15
|
-
* converts them to React Router route paths.
|
|
33
|
+
* Recursively scans an `app/` directory for page modules and descendant layouts.
|
|
16
34
|
*
|
|
17
|
-
* - `app/page.tsx`
|
|
18
|
-
* - `app/about/page.tsx`
|
|
19
|
-
* - `app/remote/[id]/page.tsx`
|
|
20
|
-
* -
|
|
35
|
+
* - `app/page.tsx` -> page `/`
|
|
36
|
+
* - `app/about/page.tsx` -> page `/about`
|
|
37
|
+
* - `app/remote/[id]/page.tsx` -> page `/remote/:id`
|
|
38
|
+
* - `app/remote/[id]/layout.tsx` -> layout `/remote/:id/**`
|
|
39
|
+
* - `app/layout.tsx` remains the global wrapper and is not returned as a route layout.
|
|
40
|
+
* - Files and folders starting with `_` are skipped.
|
|
21
41
|
*/
|
|
42
|
+
export async function scanAppRoutes(appDir: string): Promise<AppRouteDiscovery> {
|
|
43
|
+
const pages: PageRoute[] = []
|
|
44
|
+
const layouts: AppRouteLayout[] = []
|
|
45
|
+
await walkDir(appDir, appDir, pages, layouts)
|
|
46
|
+
pages.sort(compareRouteModules)
|
|
47
|
+
layouts.sort(compareRouteModules)
|
|
48
|
+
return { pages, layouts }
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Preserve the existing flat inspection contract while route generation uses the richer scan. */
|
|
22
52
|
export async function scanPages(appDir: string): Promise<PageRoute[]> {
|
|
23
|
-
|
|
24
|
-
await walkDir(appDir, appDir, routes)
|
|
25
|
-
routes.sort((a, b) => a.path.localeCompare(b.path))
|
|
26
|
-
return routes
|
|
53
|
+
return [...(await scanAppRoutes(appDir)).pages]
|
|
27
54
|
}
|
|
28
55
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
56
|
+
/** Treat parameter names as labels when comparing route ownership and overrides. */
|
|
57
|
+
export function routePatternKey(path: string): string {
|
|
58
|
+
if (path === "/") return "/"
|
|
59
|
+
const segments = routeSegments(path).map((segment) =>
|
|
60
|
+
segment.startsWith(":") ? ":" : segment.toLowerCase()
|
|
61
|
+
)
|
|
62
|
+
return `/${segments.join("/")}`
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async function walkDir(
|
|
66
|
+
dir: string,
|
|
67
|
+
appDir: string,
|
|
68
|
+
pages: PageRoute[],
|
|
69
|
+
layouts: AppRouteLayout[]
|
|
70
|
+
): Promise<void> {
|
|
71
|
+
const entries = await readDirectory(dir)
|
|
72
|
+
if (!entries) return
|
|
73
|
+
|
|
74
|
+
const visibleEntries = entries
|
|
75
|
+
.filter((entry) => !String(entry.name).startsWith("_"))
|
|
76
|
+
.sort((a, b) => compareText(String(a.name), String(b.name)))
|
|
77
|
+
const relativeDir = normalizeRelativePath(relative(appDir, dir))
|
|
78
|
+
|
|
79
|
+
validateChildDirectories(visibleEntries, relativeDir)
|
|
80
|
+
|
|
81
|
+
const pageEntries = visibleEntries.filter(
|
|
82
|
+
(entry) => entry.isFile() && (entry.name === "page.tsx" || entry.name === "page.ts")
|
|
83
|
+
)
|
|
84
|
+
if (pageEntries.length > 1) {
|
|
85
|
+
const modules = pageEntries.map((entry) => displayAppPath(join(relativeDir, entry.name)))
|
|
86
|
+
throw new Error(
|
|
87
|
+
`[SixbCustomApp] Conflicting page modules for route '${directoryRoutePath(relativeDir)}': ${modules.join(" and ")}. Keep exactly one of page.tsx or page.ts.`
|
|
88
|
+
)
|
|
35
89
|
}
|
|
36
90
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
91
|
+
const pageEntry = pageEntries[0]
|
|
92
|
+
if (pageEntry) {
|
|
93
|
+
const relativePath = normalizeRelativePath(join(relativeDir, pageEntry.name))
|
|
94
|
+
const path = directoryRoutePath(relativeDir)
|
|
95
|
+
assertRouteIsAvailable(path, relativePath)
|
|
96
|
+
pages.push({ path, filePath: join(dir, pageEntry.name), relativePath })
|
|
97
|
+
}
|
|
40
98
|
|
|
41
|
-
|
|
99
|
+
if (relativeDir) {
|
|
100
|
+
const layoutEntry = visibleEntries.find(
|
|
101
|
+
(entry) => entry.isFile() && entry.name === "layout.tsx"
|
|
102
|
+
)
|
|
103
|
+
if (layoutEntry) {
|
|
104
|
+
layouts.push({
|
|
105
|
+
path: directoryRoutePath(relativeDir),
|
|
106
|
+
filePath: join(dir, layoutEntry.name),
|
|
107
|
+
relativePath: normalizeRelativePath(join(relativeDir, layoutEntry.name)),
|
|
108
|
+
})
|
|
109
|
+
}
|
|
110
|
+
}
|
|
42
111
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
112
|
+
for (const entry of visibleEntries) {
|
|
113
|
+
if (!entry.isDirectory()) continue
|
|
114
|
+
// `app/public/` is the static asset root, never part of the file router.
|
|
115
|
+
if (!relativeDir && entry.name === "public") continue
|
|
116
|
+
await walkDir(join(dir, entry.name), appDir, pages, layouts)
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async function readDirectory(dir: string): Promise<import("node:fs").Dirent[] | null> {
|
|
121
|
+
try {
|
|
122
|
+
return (await readdir(dir, { withFileTypes: true })) as import("node:fs").Dirent[]
|
|
123
|
+
} catch (error) {
|
|
124
|
+
if (isNotFoundError(error)) return null
|
|
125
|
+
throw error
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function validateChildDirectories(
|
|
130
|
+
entries: readonly import("node:fs").Dirent[],
|
|
131
|
+
relativeDir: string
|
|
132
|
+
): void {
|
|
133
|
+
const claimedSegments = new Map<string, string>()
|
|
134
|
+
|
|
135
|
+
for (const entry of entries) {
|
|
136
|
+
if (!entry.isDirectory()) continue
|
|
137
|
+
const segment = routeSegment(entry.name, join(relativeDir, entry.name))
|
|
138
|
+
const key = segment.startsWith(":") ? ":" : segment.toLowerCase()
|
|
139
|
+
const existing = claimedSegments.get(key)
|
|
140
|
+
if (existing && existing !== entry.name) {
|
|
141
|
+
const parent = relativeDir ? displayAppPath(relativeDir) : "app/"
|
|
142
|
+
throw new Error(
|
|
143
|
+
`[SixbCustomApp] Ambiguous route directories '${existing}' and '${entry.name}' under ${parent}. They match the same URL segment.`
|
|
144
|
+
)
|
|
46
145
|
}
|
|
146
|
+
claimedSegments.set(key, entry.name)
|
|
147
|
+
}
|
|
148
|
+
}
|
|
47
149
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
150
|
+
function directoryRoutePath(relativeDir: string): string {
|
|
151
|
+
if (!relativeDir) return "/"
|
|
152
|
+
return `/${relativeDir
|
|
153
|
+
.split("/")
|
|
154
|
+
.map((segment) => routeSegment(segment, relativeDir))
|
|
155
|
+
.join("/")}`
|
|
156
|
+
}
|
|
51
157
|
|
|
52
|
-
|
|
53
|
-
|
|
158
|
+
function routeSegment(segment: string, relativePath: string): string {
|
|
159
|
+
const dynamic = dynamicSegmentPattern.exec(segment)
|
|
160
|
+
if (dynamic) return `:${dynamic[1]}`
|
|
54
161
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
162
|
+
if (segment.includes("[") || segment.includes("]")) {
|
|
163
|
+
throw new Error(
|
|
164
|
+
`[SixbCustomApp] Invalid dynamic route directory ${displayAppPath(relativePath)}. Use one name such as '[id]'; catch-all and partial dynamic segments are not supported.`
|
|
165
|
+
)
|
|
166
|
+
}
|
|
167
|
+
if (segment.includes("(") || segment.includes(")")) {
|
|
168
|
+
throw new Error(
|
|
169
|
+
`[SixbCustomApp] Invalid route directory ${displayAppPath(relativePath)}. Route groups are not supported yet; use a normal directory name.`
|
|
170
|
+
)
|
|
60
171
|
}
|
|
172
|
+
if (/[:*?]/.test(segment)) {
|
|
173
|
+
throw new Error(
|
|
174
|
+
`[SixbCustomApp] Invalid route directory ${displayAppPath(relativePath)}. Use '[name]' for a dynamic segment; ':', '*' and '?' are reserved by React Router.`
|
|
175
|
+
)
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return segment
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function assertRouteIsAvailable(path: string, relativePath: string): void {
|
|
182
|
+
const normalizedPath = path.toLowerCase()
|
|
183
|
+
const reserved =
|
|
184
|
+
reservedRoutePaths.has(normalizedPath) ||
|
|
185
|
+
reservedRoutePrefixes.some(
|
|
186
|
+
(prefix) => normalizedPath === prefix || normalizedPath.startsWith(`${prefix}/`)
|
|
187
|
+
)
|
|
188
|
+
if (!reserved) return
|
|
189
|
+
|
|
190
|
+
throw new Error(
|
|
191
|
+
`[SixbCustomApp] Page ${displayAppPath(relativePath)} resolves to reserved framework path '${path}'. Choose a route outside /api, /auth, /ws, /docs, /__sixb, and /app.webmanifest.`
|
|
192
|
+
)
|
|
61
193
|
}
|
|
62
194
|
|
|
63
|
-
function
|
|
64
|
-
|
|
65
|
-
|
|
195
|
+
function routeSegments(path: string): string[] {
|
|
196
|
+
return path === "/" ? [] : path.replace(/^\/+|\/+$/g, "").split("/")
|
|
197
|
+
}
|
|
66
198
|
|
|
67
|
-
|
|
68
|
-
|
|
199
|
+
function normalizeRelativePath(path: string): string {
|
|
200
|
+
return path
|
|
201
|
+
.split("\\")
|
|
202
|
+
.join("/")
|
|
203
|
+
.replace(/^\.\/$/, "")
|
|
204
|
+
}
|
|
69
205
|
|
|
70
|
-
|
|
71
|
-
|
|
206
|
+
function displayAppPath(path: string): string {
|
|
207
|
+
const normalized = normalizeRelativePath(path)
|
|
208
|
+
return normalized ? `app/${normalized}` : "app/"
|
|
209
|
+
}
|
|
72
210
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
211
|
+
function compareRouteModules(
|
|
212
|
+
a: Pick<PageRoute, "path" | "relativePath">,
|
|
213
|
+
b: Pick<PageRoute, "path" | "relativePath">
|
|
214
|
+
): number {
|
|
215
|
+
return compareText(a.path, b.path) || compareText(a.relativePath, b.relativePath)
|
|
216
|
+
}
|
|
76
217
|
|
|
77
|
-
|
|
78
|
-
|
|
218
|
+
function compareText(a: string, b: string): number {
|
|
219
|
+
return a < b ? -1 : a > b ? 1 : 0
|
|
220
|
+
}
|
|
79
221
|
|
|
80
|
-
|
|
222
|
+
function isNotFoundError(error: unknown): boolean {
|
|
223
|
+
if (!(error instanceof Error)) return false
|
|
224
|
+
const code = (error as NodeJS.ErrnoException).code
|
|
225
|
+
return code === "ENOENT" || code === "ENOTDIR"
|
|
81
226
|
}
|