feat: detect rust handler file

This commit is contained in:
Valerio Ageno
2024-05-11 15:27:51 +02:00
parent 73c94e5fbb
commit 4a8a24c708
6 changed files with 75 additions and 18 deletions
@@ -34,8 +34,11 @@ const defaultConfig: Config = {
let isFirst = false
let skipMessage = false
async function getRouteNodes(config = defaultConfig): Promise<RouteNode[]> {
async function getRouteNodes(
config = defaultConfig,
): Promise<{ routeNodes: RouteNode[]; rustHandlersNodes: string[] }> {
const routeNodes: RouteNode[] = []
const rustHandlersNodes: string[] = []
async function recurse(dir: string): Promise<void> {
const fullDir = path.resolve(config.folderName, dir)
@@ -74,6 +77,19 @@ async function getRouteNodes(config = defaultConfig): Promise<RouteNode[]> {
isLoader,
variableName,
})
} else if (fullPath.match(/\.(rs)$/)) {
const filePath = replaceBackslash(path.join(dir, dirent.name))
const filePathNoExt = removeExt(filePath)
let routePath =
cleanPath(`/${filePathNoExt.split('.').join('/')}`) || ''
if (routePath === 'index') {
routePath = '/'
}
routePath = routePath.replace(/\/index$/, '/') || '/'
rustHandlersNodes.push(routePath)
}
}),
)
@@ -81,7 +97,7 @@ async function getRouteNodes(config = defaultConfig): Promise<RouteNode[]> {
await recurse('./')
return routeNodes
return { routeNodes, rustHandlersNodes }
}
export function hasParentRoute(
@@ -139,7 +155,8 @@ export async function routeGenerator(config = defaultConfig): Promise<void> {
latestTask = taskId
const start = Date.now()
const beforeRouteNodes = await getRouteNodes(config)
const { routeNodes: beforeRouteNodes, rustHandlersNodes } =
await getRouteNodes(config)
const preRouteNodes = multiSortBy(beforeRouteNodes, [
(d): number => (d.routePath === '/' ? -1 : 1),
@@ -259,21 +276,7 @@ export async function routeGenerator(config = defaultConfig): Promise<void> {
}),
].join('\n')
const routeImports = [
'// This file is auto-generated by Tuono',
"import { createRoute } from 'tuono'",
[
`import RootImport from './${replaceBackslash(
path.relative(
path.dirname(config.generatedRouteTree),
path.resolve(config.folderName, ROOT_PATH_ID),
),
)}'`,
].join('\n'),
imports,
'const rootRoute = createRoute({ isRoot: true, component: RootImport });',
createRoutes,
'// Create/Update Routes',
const createRouteUpdates = [
sortedRouteNodes
.map((node) => {
const loaderNode = routePiecesByPath[node.routePath]?.loader
@@ -284,6 +287,9 @@ export async function routeGenerator(config = defaultConfig): Promise<void> {
${[
`path: '${node.cleanedPath}'`,
`getParentRoute: () => ${node.parent?.variableName ?? 'root'}Route`,
rustHandlersNodes.includes(node.path || '')
? 'hasHandler: true'
: '',
]
.filter(Boolean)
.join(',')}
@@ -313,6 +319,24 @@ export async function routeGenerator(config = defaultConfig): Promise<void> {
].join('')
})
.join('\n\n'),
]
const routeImports = [
'// This file is auto-generated by Tuono',
"import { createRoute } from 'tuono'",
[
`import RootImport from './${replaceBackslash(
path.relative(
path.dirname(config.generatedRouteTree),
path.resolve(config.folderName, ROOT_PATH_ID),
),
)}'`,
].join('\n'),
imports,
'const rootRoute = createRoute({ isRoot: true, component: RootImport });',
createRoutes,
'// Create/Update Routes',
createRouteUpdates,
'// Create and export the route tree',
`export const routeTree = rootRoute.addChildren([${routeConfigChildrenText}])`,
]
@@ -0,0 +1,30 @@
// This file is auto-generated by Tuono
import { createRoute } from 'tuono'
import RootImport from './routes/__root'
import AboutImport from './routes/about'
import IndexImport from './routes/index'
const rootRoute = createRoute({ isRoot: true, component: RootImport })
const About = createRoute({ component: AboutImport })
const Index = createRoute({ component: IndexImport })
// Create/Update Routes
const AboutRoute = About.update({
path: '/about',
getParentRoute: () => rootRoute,
} as any)
const IndexRoute = Index.update({
path: '/',
getParentRoute: () => rootRoute,
hasHandler: true,
} as any)
// Create and export the route tree
export const routeTree = rootRoute.addChildren([IndexRoute, AboutRoute])