Skip to content

Commit c30c4ec

Browse files
committed
Add eslint to generate-code
1 parent 49b2106 commit c30c4ec

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

generate-routes.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { writeFile } from 'node:fs/promises'
22
import { resolve } from 'node:path'
33

44
import { camelCase, pascalCase, snakeCase } from 'change-case'
5+
import { ESLint } from 'eslint'
56
import { format, resolveConfig } from 'prettier'
67

78
interface Route {
@@ -140,12 +141,30 @@ const exampleRoute: Route = {
140141

141142
const write = async (data: string, ...path: string[]): Promise<void> => {
142143
const filepath = resolve(...path)
144+
const eslint = new ESLint({ fix: true })
145+
143146
const prettierConfig = await resolveConfig(filepath)
144147
if (prettierConfig == null) {
145148
throw new Error('Failed to resolve Prettier config')
146149
}
147-
const output = await format(data, { ...prettierConfig, filepath })
148-
await writeFile(filepath, output)
150+
151+
const [linted] = await eslint.lintText(data)
152+
153+
if (linted == null) {
154+
throw new Error('ESLint returned empty results')
155+
}
156+
157+
if (linted.fatalErrorCount > 0) {
158+
// TODO: ESLint is failing to parse, seems it may not be loading the right config.
159+
// throw new Error(
160+
// `ESLint returned fatal errors: ${JSON.stringify(linted.messages)}`,
161+
// )
162+
}
163+
164+
const output = linted.output ?? linted.source ?? ''
165+
166+
const prettyData = await format(output, { ...prettierConfig, filepath })
167+
await writeFile(filepath, prettyData)
149168
}
150169

151170
const routeRootPath = resolve('src', 'lib', 'seam', 'connect', 'routes')

package-lock.json

Lines changed: 18 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
},
8989
"devDependencies": {
9090
"@seamapi/types": "^1.14.0",
91+
"@types/eslint": "^8.44.2",
9192
"@types/node": "^18.11.18",
9293
"ava": "^5.0.1",
9394
"c8": "^8.0.0",

0 commit comments

Comments
 (0)