Skip to content

Commit ffe4191

Browse files
committed
test: codegen
1 parent dab50f0 commit ffe4191

File tree

13 files changed

+470
-252
lines changed

13 files changed

+470
-252
lines changed

package.json

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,8 @@
5454
},
5555
"peerDependencies": {
5656
"kysely": ">=0.26.3",
57-
"kysely-codegen": ">=0.11.0",
5857
"typescript": ">=5"
5958
},
60-
"peerDependenciesMeta": {
61-
"kysely-codegen": {
62-
"optional": true
63-
}
64-
},
6559
"dependencies": {
6660
"@clack/prompts": "^0.7.0",
6761
"bundle-require": "^4.0.2",
@@ -73,7 +67,8 @@
7367
"find-up": "^6.3.0",
7468
"human-id": "^4.1.0",
7569
"is-unicode-supported": "^1.3.0",
76-
"picocolors": "^1.0.0"
70+
"picocolors": "^1.0.0",
71+
"std-env": "^3.4.3"
7772
},
7873
"devDependencies": {
7974
"@biomejs/biome": "1.1.2",
@@ -89,39 +84,22 @@
8984
"glob": "^10.3.10",
9085
"knip": "^2.29.0",
9186
"kysely": "^0.26.3",
92-
"kysely-codegen": "^0.11.0",
9387
"mysql2": "^3.6.2",
9488
"publint": "^0.2.2",
9589
"rimraf": "^4.4.1",
9690
"simple-git-hooks": "^2.9.0",
9791
"typescript": "5.2.2",
9892
"vitest": "^0.34.5"
9993
},
100-
"contributors": [
101-
102-
],
94+
"contributors": ["[email protected]"],
10395
"funding": "https://github.com/sponsors/tmm",
104-
"keywords": [
105-
"kysely",
106-
"cli",
107-
"migrate",
108-
"migrations",
109-
"codegen"
110-
],
96+
"keywords": ["kysely", "cli", "migrate", "migrations", "codegen"],
11197
"packageManager": "[email protected]",
11298
"simple-git-hooks": {
11399
"pre-commit": "pnpm format && pnpm lint:fix"
114100
},
115101
"knip": {
116-
"entry": [
117-
"src/**/*.ts!",
118-
"src/exports/index.ts!"
119-
],
120-
"ignoreDependencies": [
121-
"kysely-codegen"
122-
],
123-
"project": [
124-
".scripts/**/*.ts"
125-
]
102+
"entry": ["src/**/*.ts!", "src/exports/index.ts!"],
103+
"project": [".scripts/**/*.ts"]
126104
}
127105
}

pnpm-lock.yaml

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

src/commands/codegen.ts

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import { setTimeout as sleep } from 'node:timers/promises'
2-
import { dirname, relative } from 'path'
1+
import { relative } from 'path'
32
import { capitalCase } from 'change-case'
43
import { writeFile } from 'fs/promises'
54
import pc from 'picocolors'
65

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

30-
const s = spinner()
31-
s.start('Generating types')
32-
// so spinner has a chance :)
33-
if (config._spinnerMs) await sleep(config._spinnerMs)
28+
const s = spinner(config._spinnerMs)
29+
await s.start('Generating types')
3430

3531
const tables = await db.introspection.getTables()
3632

33+
// TODO: Add support for enums + schemas
34+
// - mysql https://github.com/RobinBlomberg/kysely-codegen/blob/b749a677e6bfd7370559767e57e4c69746898f94/src/dialects/mysql/mysql-introspector.ts#L28-L46
35+
// - postgres https://github.com/RobinBlomberg/kysely-codegen/blob/b749a677e6bfd7370559767e57e4c69746898f94/src/dialects/postgres/postgres-introspector.ts#L22-L36
3736
const content = getTypes(
3837
tables,
3938
config.codegen.dialect,
4039
config.codegen.definitions,
4140
)
4241
await writeFile(config.codegen.out, content)
42+
4343
s.stop('Generated types')
4444

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

60-
const kyselyCodegenOptions = config.codegen['kysely-codegen']
61-
if (kyselyCodegenOptions) {
62-
const { Cli } = await import('kysely-codegen').catch(() => ({ Cli: null }))
63-
if (!Cli) throw new Error('`kysely-codegen` not installed.')
64-
const defaultOptions = {
65-
camelCase: false,
66-
dialectName: config.codegen.dialect,
67-
envFile: undefined,
68-
excludePattern: undefined,
69-
includePattern: undefined,
70-
logLevel: 0,
71-
outFile: `${dirname(config.codegen.out)}/types-kc.ts`,
72-
print: false,
73-
schema: undefined,
74-
typeOnlyImports: true,
75-
verify: false,
76-
}
77-
const cliOptions =
78-
typeof kyselyCodegenOptions === 'object'
79-
? { ...defaultOptions, ...kyselyCodegenOptions }
80-
: { ...defaultOptions, url: kyselyCodegenOptions }
81-
const cli = new Cli()
82-
await cli.generate(cliOptions)
83-
}
84-
8560
const codegenRelativeFilePath = relative(process.cwd(), config.codegen.out)
8661
return `Created ${pc.green(codegenRelativeFilePath)}`
8762
}

src/commands/down.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { existsSync } from 'node:fs'
22
import { mkdir } from 'node:fs/promises'
3-
import { setTimeout as sleep } from 'node:timers/promises'
4-
import { cancel, confirm, isCancel, spinner } from '@clack/prompts'
3+
import { cancel, confirm, isCancel } from '@clack/prompts'
54
import { type MigrationResultSet } from 'kysely'
65

6+
import { spinner } from '../utils/clack.js'
77
import { findConfig } from '../utils/findConfig.js'
88
import { getAppliedMigrationsCount } from '../utils/getAppliedMigrationsCount.js'
99
import { getMigrator } from '../utils/getMigrator.js'
@@ -42,14 +42,12 @@ export async function down(options: DownOptions) {
4242
if (!shouldContinue) return 'Applied 0 migrations.'
4343
}
4444

45-
const s = spinner()
46-
s.start('Running migrations')
47-
// so spinner has a chance :)
48-
if (config._spinnerMs) await sleep(config._spinnerMs)
45+
const s = spinner(config._spinnerMs)
46+
await s.start('Running migrations')
4947

5048
let resultSet: MigrationResultSet
5149
if (options.reset) {
52-
// TODO: migrator.migrateTo(NO_MIGRATIONS) throwing when run with linked package
50+
// migrator.migrateTo(NO_MIGRATIONS) throwing when run with linked package so handling manually
5351
const migration = migrations[0]!
5452
resultSet = await migrator.migrateTo(migration.name)
5553
if (!resultSet.error) {

src/commands/to.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { existsSync } from 'node:fs'
22
import { mkdir } from 'node:fs/promises'
3-
import { setTimeout as sleep } from 'node:timers/promises'
4-
import { cancel, isCancel, select, spinner } from '@clack/prompts'
3+
import { cancel, isCancel, select } from '@clack/prompts'
54
import pc from 'picocolors'
65

6+
import { spinner } from '../utils/clack.js'
77
import { findConfig } from '../utils/findConfig.js'
88
import { getAppliedMigrationsCount } from '../utils/getAppliedMigrationsCount.js'
99
import { getMigrator } from '../utils/getMigrator.js'
@@ -68,10 +68,8 @@ export async function to(options: ToOptions) {
6868
}
6969
}
7070

71-
const s = spinner()
72-
s.start('Running migrations')
73-
// so spinner has a chance :)
74-
if (config._spinnerMs) await sleep(config._spinnerMs)
71+
const s = spinner(config._spinnerMs)
72+
await s.start('Running migrations')
7573

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

src/commands/up.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { existsSync } from 'node:fs'
22
import { mkdir } from 'node:fs/promises'
3-
import { setTimeout as sleep } from 'node:timers/promises'
4-
import { spinner } from '@clack/prompts'
53
import { type MigrationResultSet } from 'kysely'
64

5+
import { spinner } from '../utils/clack.js'
76
import { findConfig } from '../utils/findConfig.js'
87
import { getAppliedMigrationsCount } from '../utils/getAppliedMigrationsCount.js'
98
import { getMigrator } from '../utils/getMigrator.js'
@@ -31,10 +30,8 @@ export async function up(options: UpOptions) {
3130

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

34-
const s = spinner()
35-
s.start('Running migrations')
36-
// so spinner has a chance :)
37-
if (config._spinnerMs) await sleep(config._spinnerMs)
33+
const s = spinner(config._spinnerMs)
34+
await s.start('Running migrations')
3835

3936
let resultSet: MigrationResultSet
4037
if (options.latest) resultSet = await migrator.migrateToLatest()

0 commit comments

Comments
 (0)