Skip to content

Commit 61e4e22

Browse files
authored
Delete any stale generated files (#77)
* Add del * Define main function in generate-routes * Clean any non-generated files from routes * Use posix.join
1 parent de218fa commit 61e4e22

File tree

3 files changed

+36
-14
lines changed

3 files changed

+36
-14
lines changed

generate-routes.ts

+34-14
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,35 @@
11
import { readFile, writeFile } from 'node:fs/promises'
2-
import { resolve } from 'node:path'
2+
import { dirname, posix, resolve } from 'node:path'
3+
import { fileURLToPath } from 'node:url'
34

45
import { openapi } from '@seamapi/types/connect'
56
import { camelCase, kebabCase, pascalCase, snakeCase } from 'change-case'
7+
import { deleteAsync } from 'del'
68
import { ESLint } from 'eslint'
79
import { format, resolveConfig } from 'prettier'
810

9-
const rootClassPath = resolve('src', 'lib', 'seam', 'connect', 'seam-http.ts')
10-
const routeOutputPath = resolve('src', 'lib', 'seam', 'connect', 'routes')
11+
const rootPathParts = ['src', 'lib', 'seam', 'connect']
12+
13+
const routeOutputPathParts = ['routes']
14+
15+
const rootPath = resolve(
16+
dirname(fileURLToPath(import.meta.url)),
17+
...rootPathParts,
18+
)
19+
const rootClassPath = resolve(rootPath, 'seam-http.ts')
20+
const routeOutputPath = resolve(rootPath, ...routeOutputPathParts)
21+
22+
async function main(): Promise<void> {
23+
const routes = createRoutes()
24+
const routeNames = await Promise.all(routes.map(writeRoute))
25+
const routeIndexName = await writeRoutesIndex(routes)
26+
const outputPathParts = [...rootPathParts, ...routeOutputPathParts]
27+
await deleteAsync([
28+
posix.join(...outputPathParts, '*'),
29+
`!${posix.join(...outputPathParts, routeIndexName)}`,
30+
...routeNames.map((name) => `!${posix.join(...outputPathParts, name)}`),
31+
])
32+
}
1133

1234
const routePaths = [
1335
'/access_codes',
@@ -486,21 +508,21 @@ const getClassConstructors = (data: string): string => {
486508
return lines.slice(startIdx, endIdx).join('\n')
487509
}
488510

489-
const writeRoute = async (route: Route): Promise<void> => {
511+
const writeRoute = async (route: Route): Promise<string> => {
490512
const rootClass = await readFile(rootClassPath)
491513
const constructors = getClassConstructors(rootClass.toString())
492-
await write(
493-
renderRoute(route, { constructors }),
494-
routeOutputPath,
495-
`${kebabCase(route.namespace)}.ts`,
496-
)
514+
const name = `${kebabCase(route.namespace)}.ts`
515+
await write(renderRoute(route, { constructors }), routeOutputPath, name)
516+
return name
497517
}
498518

499-
const writeRoutesIndex = async (routes: Route[]): Promise<void> => {
519+
const writeRoutesIndex = async (routes: Route[]): Promise<string> => {
500520
const exports = routes.map(
501521
(route) => `export * from './${kebabCase(route.namespace)}.js'`,
502522
)
503-
await write(exports.join('\n'), routeOutputPath, `index.ts`)
523+
const name = 'index.ts'
524+
await write(exports.join('\n'), routeOutputPath, name)
525+
return name
504526
}
505527

506528
const write = async (data: string, ...path: string[]): Promise<void> => {
@@ -550,6 +572,4 @@ const eslintFixOutput = async (
550572
return linted.output ?? linted.source ?? data
551573
}
552574

553-
const routes = createRoutes()
554-
await Promise.all(routes.map(writeRoute))
555-
await writeRoutesIndex(routes)
575+
await main()

package-lock.json

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
"c8": "^9.0.0",
111111
"change-case": "^5.0.2",
112112
"concurrently": "^8.2.1",
113+
"del": "^7.1.0",
113114
"del-cli": "^5.0.0",
114115
"eslint": "^8.9.0",
115116
"eslint-config-prettier": "^9.0.0",

0 commit comments

Comments
 (0)