Skip to content

Commit

Permalink
test: codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm committed Oct 28, 2023
1 parent dab50f0 commit ffe4191
Show file tree
Hide file tree
Showing 13 changed files with 470 additions and 252 deletions.
34 changes: 6 additions & 28 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,8 @@
},
"peerDependencies": {
"kysely": ">=0.26.3",
"kysely-codegen": ">=0.11.0",
"typescript": ">=5"
},
"peerDependenciesMeta": {
"kysely-codegen": {
"optional": true
}
},
"dependencies": {
"@clack/prompts": "^0.7.0",
"bundle-require": "^4.0.2",
Expand All @@ -73,7 +67,8 @@
"find-up": "^6.3.0",
"human-id": "^4.1.0",
"is-unicode-supported": "^1.3.0",
"picocolors": "^1.0.0"
"picocolors": "^1.0.0",
"std-env": "^3.4.3"
},
"devDependencies": {
"@biomejs/biome": "1.1.2",
Expand All @@ -89,39 +84,22 @@
"glob": "^10.3.10",
"knip": "^2.29.0",
"kysely": "^0.26.3",
"kysely-codegen": "^0.11.0",
"mysql2": "^3.6.2",
"publint": "^0.2.2",
"rimraf": "^4.4.1",
"simple-git-hooks": "^2.9.0",
"typescript": "5.2.2",
"vitest": "^0.34.5"
},
"contributors": [
"[email protected]"
],
"contributors": ["[email protected]"],
"funding": "https://github.com/sponsors/tmm",
"keywords": [
"kysely",
"cli",
"migrate",
"migrations",
"codegen"
],
"keywords": ["kysely", "cli", "migrate", "migrations", "codegen"],
"packageManager": "[email protected]",
"simple-git-hooks": {
"pre-commit": "pnpm format && pnpm lint:fix"
},
"knip": {
"entry": [
"src/**/*.ts!",
"src/exports/index.ts!"
],
"ignoreDependencies": [
"kysely-codegen"
],
"project": [
".scripts/**/*.ts"
]
"entry": ["src/**/*.ts!", "src/exports/index.ts!"],
"project": [".scripts/**/*.ts"]
}
}
84 changes: 4 additions & 80 deletions pnpm-lock.yaml

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

41 changes: 8 additions & 33 deletions src/commands/codegen.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { setTimeout as sleep } from 'node:timers/promises'
import { dirname, relative } from 'path'
import { relative } from 'path'
import { capitalCase } from 'change-case'
import { writeFile } from 'fs/promises'
import pc from 'picocolors'

import { spinner } from '@clack/prompts'
import { S_BAR, S_SUCCESS, message } from '../utils/clack.js'
import { S_BAR, S_SUCCESS, message, spinner } from '../utils/clack.js'
import { getTypes } from '../utils/codegen/getTypes.js'
import { findConfig } from '../utils/findConfig.js'
import { loadConfig } from '../utils/loadConfig.js'
Expand All @@ -27,19 +25,21 @@ export async function codegen(options: CodegenOptions) {
if (!config.codegen)
throw new Error('`codegen` config required to generate types.')

const s = spinner()
s.start('Generating types')
// so spinner has a chance :)
if (config._spinnerMs) await sleep(config._spinnerMs)
const s = spinner(config._spinnerMs)
await s.start('Generating types')

const tables = await db.introspection.getTables()

// TODO: Add support for enums + schemas
// - mysql https://github.com/RobinBlomberg/kysely-codegen/blob/b749a677e6bfd7370559767e57e4c69746898f94/src/dialects/mysql/mysql-introspector.ts#L28-L46
// - postgres https://github.com/RobinBlomberg/kysely-codegen/blob/b749a677e6bfd7370559767e57e4c69746898f94/src/dialects/postgres/postgres-introspector.ts#L22-L36
const content = getTypes(
tables,
config.codegen.dialect,
config.codegen.definitions,
)
await writeFile(config.codegen.out, content)

s.stop('Generated types')

if (tables.length) process.stdout.write(`${pc.gray(S_BAR)}\n`)
Expand All @@ -57,31 +57,6 @@ export async function codegen(options: CodegenOptions) {
)
}

const kyselyCodegenOptions = config.codegen['kysely-codegen']
if (kyselyCodegenOptions) {
const { Cli } = await import('kysely-codegen').catch(() => ({ Cli: null }))
if (!Cli) throw new Error('`kysely-codegen` not installed.')
const defaultOptions = {
camelCase: false,
dialectName: config.codegen.dialect,
envFile: undefined,
excludePattern: undefined,
includePattern: undefined,
logLevel: 0,
outFile: `${dirname(config.codegen.out)}/types-kc.ts`,
print: false,
schema: undefined,
typeOnlyImports: true,
verify: false,
}
const cliOptions =
typeof kyselyCodegenOptions === 'object'
? { ...defaultOptions, ...kyselyCodegenOptions }
: { ...defaultOptions, url: kyselyCodegenOptions }
const cli = new Cli()
await cli.generate(cliOptions)
}

const codegenRelativeFilePath = relative(process.cwd(), config.codegen.out)
return `Created ${pc.green(codegenRelativeFilePath)}`
}
12 changes: 5 additions & 7 deletions src/commands/down.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { existsSync } from 'node:fs'
import { mkdir } from 'node:fs/promises'
import { setTimeout as sleep } from 'node:timers/promises'
import { cancel, confirm, isCancel, spinner } from '@clack/prompts'
import { cancel, confirm, isCancel } from '@clack/prompts'
import { type MigrationResultSet } from 'kysely'

import { spinner } from '../utils/clack.js'
import { findConfig } from '../utils/findConfig.js'
import { getAppliedMigrationsCount } from '../utils/getAppliedMigrationsCount.js'
import { getMigrator } from '../utils/getMigrator.js'
Expand Down Expand Up @@ -42,14 +42,12 @@ export async function down(options: DownOptions) {
if (!shouldContinue) return 'Applied 0 migrations.'
}

const s = spinner()
s.start('Running migrations')
// so spinner has a chance :)
if (config._spinnerMs) await sleep(config._spinnerMs)
const s = spinner(config._spinnerMs)
await s.start('Running migrations')

let resultSet: MigrationResultSet
if (options.reset) {
// TODO: migrator.migrateTo(NO_MIGRATIONS) throwing when run with linked package
// migrator.migrateTo(NO_MIGRATIONS) throwing when run with linked package so handling manually
const migration = migrations[0]!
resultSet = await migrator.migrateTo(migration.name)
if (!resultSet.error) {
Expand Down
10 changes: 4 additions & 6 deletions src/commands/to.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { existsSync } from 'node:fs'
import { mkdir } from 'node:fs/promises'
import { setTimeout as sleep } from 'node:timers/promises'
import { cancel, isCancel, select, spinner } from '@clack/prompts'
import { cancel, isCancel, select } from '@clack/prompts'
import pc from 'picocolors'

import { spinner } from '../utils/clack.js'
import { findConfig } from '../utils/findConfig.js'
import { getAppliedMigrationsCount } from '../utils/getAppliedMigrationsCount.js'
import { getMigrator } from '../utils/getMigrator.js'
Expand Down Expand Up @@ -68,10 +68,8 @@ export async function to(options: ToOptions) {
}
}

const s = spinner()
s.start('Running migrations')
// so spinner has a chance :)
if (config._spinnerMs) await sleep(config._spinnerMs)
const s = spinner(config._spinnerMs)
await s.start('Running migrations')

const resultSet = await migrator.migrateTo(migration as string)

Expand Down
9 changes: 3 additions & 6 deletions src/commands/up.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { existsSync } from 'node:fs'
import { mkdir } from 'node:fs/promises'
import { setTimeout as sleep } from 'node:timers/promises'
import { spinner } from '@clack/prompts'
import { type MigrationResultSet } from 'kysely'

import { spinner } from '../utils/clack.js'
import { findConfig } from '../utils/findConfig.js'
import { getAppliedMigrationsCount } from '../utils/getAppliedMigrationsCount.js'
import { getMigrator } from '../utils/getMigrator.js'
Expand Down Expand Up @@ -31,10 +30,8 @@ export async function up(options: UpOptions) {

if (pendingMigrations.length === 0) return 'No pending migrations.'

const s = spinner()
s.start('Running migrations')
// so spinner has a chance :)
if (config._spinnerMs) await sleep(config._spinnerMs)
const s = spinner(config._spinnerMs)
await s.start('Running migrations')

let resultSet: MigrationResultSet
if (options.latest) resultSet = await migrator.migrateToLatest()
Expand Down
Loading

0 comments on commit ffe4191

Please sign in to comment.