Skip to content

Commit

Permalink
Clean any non-generated files from routes
Browse files Browse the repository at this point in the history
  • Loading branch information
razor-x committed Apr 9, 2024
1 parent 1e9c65e commit d3ce81e
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions generate-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { fileURLToPath } from 'node:url'

import { openapi } from '@seamapi/types/connect'
import { camelCase, kebabCase, pascalCase, snakeCase } from 'change-case'
import { deleteAsync } from 'del'
import { ESLint } from 'eslint'
import { format, resolveConfig } from 'prettier'

Expand All @@ -19,8 +20,13 @@ const routeOutputPath = resolve(rootPath, 'routes')

async function main(): Promise<void> {
const routes = createRoutes()
await Promise.all(routes.map(writeRoute))
await writeRoutesIndex(routes)
const routeNames = await Promise.all(routes.map(writeRoute))
const routeIndexName = await writeRoutesIndex(routes)
await deleteAsync([
`${routeOutputPath}/**`,
`!${routeOutputPath}/${routeIndexName}`,
...routeNames.map((name) => `!${routeOutputPath}/${name}`),
])
}

const routePaths = [
Expand Down Expand Up @@ -500,21 +506,21 @@ const getClassConstructors = (data: string): string => {
return lines.slice(startIdx, endIdx).join('\n')
}

const writeRoute = async (route: Route): Promise<void> => {
const writeRoute = async (route: Route): Promise<string> => {
const rootClass = await readFile(rootClassPath)
const constructors = getClassConstructors(rootClass.toString())
await write(
renderRoute(route, { constructors }),
routeOutputPath,
`${kebabCase(route.namespace)}.ts`,
)
const name = `${kebabCase(route.namespace)}.ts`
await write(renderRoute(route, { constructors }), routeOutputPath, name)
return name
}

const writeRoutesIndex = async (routes: Route[]): Promise<void> => {
const writeRoutesIndex = async (routes: Route[]): Promise<string> => {
const exports = routes.map(
(route) => `export * from './${kebabCase(route.namespace)}.js'`,
)
await write(exports.join('\n'), routeOutputPath, `index.ts`)
const name = 'index.ts'
await write(exports.join('\n'), routeOutputPath, name)
return name
}

const write = async (data: string, ...path: string[]): Promise<void> => {
Expand Down

0 comments on commit d3ce81e

Please sign in to comment.