Skip to content

Commit

Permalink
Merge branch 'main' into dynamic-endpoint-resources
Browse files Browse the repository at this point in the history
  • Loading branch information
andrii-balitskyi authored Apr 12, 2024
2 parents d9b35f2 + 61e4e22 commit a11944b
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 157 deletions.
2 changes: 1 addition & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ updates:
prefix: feat
include: scope
allow:
- dependency-type: production
- dependency-name: '@seamapi/types'
ignore:
- dependency-name: '*'
update-types:
Expand Down
48 changes: 34 additions & 14 deletions generate-routes.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
import { readFile, writeFile } from 'node:fs/promises'
import { resolve } from 'node:path'
import { dirname, posix, resolve } from 'node:path'
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'

const rootClassPath = resolve('src', 'lib', 'seam', 'connect', 'seam-http.ts')
const routeOutputPath = resolve('src', 'lib', 'seam', 'connect', 'routes')
const rootPathParts = ['src', 'lib', 'seam', 'connect']

const routeOutputPathParts = ['routes']

const rootPath = resolve(
dirname(fileURLToPath(import.meta.url)),
...rootPathParts,
)
const rootClassPath = resolve(rootPath, 'seam-http.ts')
const routeOutputPath = resolve(rootPath, ...routeOutputPathParts)

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

const openapiResponseKeyProp = 'x-fern-sdk-return-value'

Expand Down Expand Up @@ -452,21 +474,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 Expand Up @@ -516,6 +538,4 @@ const eslintFixOutput = async (
return linted.output ?? linted.source ?? data
}

const routes = createRoutes()
await Promise.all(routes.map(writeRoute))
await writeRoutesIndex(routes)
await main()
Loading

0 comments on commit a11944b

Please sign in to comment.