Skip to content

Commit

Permalink
Add eslint to generate-code
Browse files Browse the repository at this point in the history
  • Loading branch information
razor-x committed Sep 22, 2023
1 parent 49b2106 commit c30c4ec
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
23 changes: 21 additions & 2 deletions generate-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { writeFile } from 'node:fs/promises'
import { resolve } from 'node:path'

import { camelCase, pascalCase, snakeCase } from 'change-case'
import { ESLint } from 'eslint'
import { format, resolveConfig } from 'prettier'

interface Route {
Expand Down Expand Up @@ -140,12 +141,30 @@ const exampleRoute: Route = {

const write = async (data: string, ...path: string[]): Promise<void> => {
const filepath = resolve(...path)
const eslint = new ESLint({ fix: true })

const prettierConfig = await resolveConfig(filepath)
if (prettierConfig == null) {
throw new Error('Failed to resolve Prettier config')
}
const output = await format(data, { ...prettierConfig, filepath })
await writeFile(filepath, output)

const [linted] = await eslint.lintText(data)

if (linted == null) {
throw new Error('ESLint returned empty results')
}

if (linted.fatalErrorCount > 0) {
// TODO: ESLint is failing to parse, seems it may not be loading the right config.
// throw new Error(
// `ESLint returned fatal errors: ${JSON.stringify(linted.messages)}`,
// )
}

const output = linted.output ?? linted.source ?? ''

const prettyData = await format(output, { ...prettierConfig, filepath })
await writeFile(filepath, prettyData)
}

const routeRootPath = resolve('src', 'lib', 'seam', 'connect', 'routes')
Expand Down
20 changes: 18 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
},
"devDependencies": {
"@seamapi/types": "^1.14.0",
"@types/eslint": "^8.44.2",
"@types/node": "^18.11.18",
"ava": "^5.0.1",
"c8": "^8.0.0",
Expand Down

0 comments on commit c30c4ec

Please sign in to comment.