@@ -5,11 +5,13 @@ import { join } from 'pathe'
5
5
import { CommonArgs } from '../../arguments/common.mjs'
6
6
import { ExtensionArg , assertExtension } from '../../arguments/extension.mjs'
7
7
import { getConfigOrFail } from '../../config/get-config.mjs'
8
+ import type { HasCWD } from '../../config/get-cwd.mjs'
9
+ import type {
10
+ DatabaseInterface ,
11
+ DatabaseInterfaceConfig ,
12
+ } from '../../config/kysely-ctl-config.mjs'
8
13
import { createSubcommand } from '../../utils/create-subcommand.mjs'
9
- import {
10
- getKyselyCodegenInstalledVersion ,
11
- getPrismaKyselyInstalledVersion ,
12
- } from '../../utils/version.mjs'
14
+ import { getKyselyCodegenInstalledVersion } from '../../utils/version.mjs'
13
15
14
16
const args = {
15
17
...CommonArgs ,
@@ -58,50 +60,86 @@ const BaseMakeCommand = {
58
60
59
61
consola . debug ( 'File path:' , destinationFilePath )
60
62
61
- const databaseInterfacePath =
62
- seeds . databaseInterfacePath ||
63
- ( ( await getKyselyCodegenInstalledVersion ( args ) )
64
- ? 'kysely-codegen'
65
- : undefined )
63
+ const databaseInterfaceConfig = await resolveDatabaseInterfaceConfig (
64
+ args ,
65
+ seeds . databaseInterface ,
66
+ )
67
+ consola . debug ( 'Database interface config:' , databaseInterfaceConfig )
66
68
67
- consola . debug ( 'Database interface path:' , databaseInterfacePath )
69
+ if ( ! databaseInterfaceConfig ) {
70
+ consola . debug ( 'using non-type-safe seed template' )
68
71
69
- if ( ! databaseInterfacePath ) {
70
72
await copyFile (
71
73
join ( __dirname , 'templates/seed-template.ts' ) ,
72
74
destinationFilePath ,
73
75
)
74
- } else {
75
- const templateFile = await readFile (
76
- join ( __dirname , 'templates/seed-type-safe-template.ts' ) ,
77
- { encoding : 'utf8' } ,
78
- )
79
76
80
- consola . debug ( 'templateFile' , templateFile )
77
+ return printSuccess ( destinationFilePath )
78
+ }
81
79
82
- const [
83
- databaseInterfaceFilePath ,
84
- databaseInterfaceName = databaseInterfaceFilePath ===
85
- 'kysely-codegen' || ( await getPrismaKyselyInstalledVersion ( args ) )
86
- ? 'DB'
87
- : 'Database' ,
88
- ] = databaseInterfacePath . split ( '#' )
80
+ consola . debug ( 'using type-safe seed template' )
89
81
90
- consola . debug ( 'Database interface file path: ' , databaseInterfaceFilePath )
91
- consola . debug ( 'Database interface name: ' , databaseInterfaceName )
82
+ const templateFile = await readFile (
83
+ join ( __dirname , 'templates/seed-type-safe-template.ts' ) ,
84
+ { encoding : 'utf8' } ,
85
+ )
86
+ consola . debug ( 'Template file:' , templateFile )
87
+
88
+ const databaseInterfaceName = databaseInterfaceConfig . name || 'DB'
89
+
90
+ const populatedTemplateFile = templateFile
91
+ . replace (
92
+ / < i m p o r t > / ,
93
+ `import type ${
94
+ databaseInterfaceConfig . isDefaultExport
95
+ ? databaseInterfaceName
96
+ : `{ ${ databaseInterfaceName } }`
97
+ } from '${ databaseInterfaceConfig . path } '`,
98
+ )
99
+ . replace ( / < n a m e > / , databaseInterfaceName )
100
+ consola . debug ( 'Populated template file: ' , populatedTemplateFile )
92
101
93
- const populatedTemplateFile = templateFile
94
- . replace ( / < t y p e n a m e > / g, databaseInterfaceName )
95
- . replace ( / < p a t h > / g, databaseInterfaceFilePath )
102
+ await writeFile ( destinationFilePath , populatedTemplateFile )
96
103
97
- consola . debug ( 'Populated template file: ' , populatedTemplateFile )
104
+ printSuccess ( destinationFilePath )
105
+ } ,
106
+ } satisfies CommandDef < typeof args >
98
107
99
- await writeFile ( destinationFilePath , populatedTemplateFile )
108
+ function printSuccess ( destinationFilePath : string ) : void {
109
+ consola . success ( `Created seed file at ${ destinationFilePath } ` )
110
+ }
111
+
112
+ async function resolveDatabaseInterfaceConfig (
113
+ args : HasCWD ,
114
+ databaseInterface : DatabaseInterface | undefined ,
115
+ ) : Promise < DatabaseInterfaceConfig | null > {
116
+ if ( databaseInterface === 'off' ) {
117
+ return null
118
+ }
119
+
120
+ if ( typeof databaseInterface === 'object' ) {
121
+ return databaseInterface
122
+ }
123
+
124
+ if ( await getKyselyCodegenInstalledVersion ( args ) ) {
125
+ return {
126
+ isDefaultExport : false ,
127
+ name : 'DB' ,
128
+ path : 'kysely-codegen' ,
100
129
}
130
+ }
101
131
102
- consola . success ( `Created seed file at ${ destinationFilePath } ` )
103
- } ,
104
- } satisfies CommandDef < typeof args >
132
+ // if (await getPrismaKyselyInstalledVersion(config)) {
133
+ // TODO: generates by default to ./prisma/generated/types.ts#DB
134
+ // but configurable at the kysely generator config level located in ./prisma/schema.prisma
135
+ // }
136
+
137
+ // if (await getKanelKyselyInstalledVersion(config)) {
138
+ // TODO: generates by default to
139
+ // }
140
+
141
+ return null
142
+ }
105
143
106
144
export const MakeCommand = createSubcommand ( 'make' , BaseMakeCommand )
107
145
export const LegacyMakeCommand = createSubcommand ( 'seed:make' , BaseMakeCommand )
0 commit comments