Skip to content

Commit 3b4f0c6

Browse files
committed
Use index file at each route
1 parent 374eca7 commit 3b4f0c6

1 file changed

Lines changed: 13 additions & 36 deletions

File tree

codegen/lib/connect.ts

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,30 @@ export const connect = (
2323
file.layout = 'route.hbs'
2424
setRouteLayoutContext(file, null, blueprint)
2525

26-
const routeIndexes: Record<string, Set<string>> = {}
27-
2826
const namespaces = blueprint.namespaces.filter(
2927
({ isUndocumented }) => !isUndocumented,
3028
)
3129
const routes = blueprint.routes.filter(
3230
({ isUndocumented }) => !isUndocumented,
3331
)
3432

35-
for (const namespace of namespaces) {
36-
const k = `${rootPath}/${toFilePath(namespace.path)}.ts`
33+
const routeIndexes: Record<string, Set<string>> = {}
34+
35+
for (const node of [...namespaces, ...routes]) {
36+
const path = toFilePath(node.path)
37+
const name = kebabCase(node.name)
3738

39+
const k = `${rootPath}/${path}/${name}.ts`
3840
files[k] = { contents: Buffer.from('\n') }
3941
const file = files[k] as unknown as File
4042
file.layout = 'route.hbs'
41-
setRouteLayoutContext(file, namespace, blueprint)
43+
setRouteLayoutContext(file, node, blueprint)
4244

43-
const parentPath = namespace.parentPath ?? ''
44-
routeIndexes[parentPath] ??= new Set()
45-
routeIndexes[parentPath]?.add(`${kebabCase(namespace.name)}.js`)
46-
routeIndexes[parentPath]?.add(`${kebabCase(namespace.name)}/index.js`)
45+
routeIndexes[path] ??= new Set()
46+
routeIndexes[path]?.add(`${name}.js`)
4747

48-
const pathParts = namespace.path.split('/')
49-
while (pathParts.length > 1) {
48+
const pathParts = path.split('/')
49+
while (pathParts.length > 0) {
5050
const name = pathParts.pop()
5151
if (name == null) throw new Error('Unexpected null')
5252
const path = toFilePath(`/${pathParts.join('/')}`)
@@ -55,32 +55,9 @@ export const connect = (
5555
}
5656
}
5757

58-
for (const route of routes) {
59-
const k = `${rootPath}/${toFilePath(route.path)}.ts`
60-
61-
files[k] = { contents: Buffer.from('\n') }
62-
const file = files[k] as unknown as File
63-
file.layout = 'route.hbs'
64-
setRouteLayoutContext(file, route, blueprint)
65-
66-
const parentPath = route.parentPath ?? ''
67-
routeIndexes[parentPath] ??= new Set()
68-
routeIndexes[parentPath]?.add(`${kebabCase(route.name)}.js`)
69-
70-
const hasChildren = routes.some(
71-
({ path }) => path.startsWith(route.path) && path !== route.path,
72-
)
73-
if (hasChildren) {
74-
routeIndexes[parentPath] ??= new Set()
75-
routeIndexes[parentPath]?.add(`${kebabCase(route.name)}/index.js`)
76-
}
77-
}
78-
79-
for (const [parentPath, routes] of Object.entries(routeIndexes)) {
58+
for (const [path, routes] of Object.entries(routeIndexes)) {
8059
const k =
81-
parentPath === ''
82-
? `${rootPath}/index.ts`
83-
: `${rootPath}/${toFilePath(parentPath)}/index.ts`
60+
path === '' ? `${rootPath}/index.ts` : `${rootPath}/${path}/index.ts`
8461
files[k] = { contents: Buffer.from('\n') }
8562
const file = files[k] as unknown as File
8663
file.layout = 'route-index.hbs'

0 commit comments

Comments
 (0)