|
| 1 | +import fs from 'node:fs'; |
| 2 | +import path from 'node:path'; |
1 | 3 | import { common, exports, functions, imports, object, variables } from '@sveltejs/cli-core/js';
|
2 | 4 | import { defineAddon, defineAddonOptions, dedent, type OptionValues } from '@sveltejs/cli-core';
|
3 | 5 | import { parseJson, parseScript } from '@sveltejs/cli-core/parsers';
|
@@ -69,8 +71,21 @@ export default defineAddon({
|
69 | 71 | shortDescription: 'database orm',
|
70 | 72 | homepage: 'https://orm.drizzle.team',
|
71 | 73 | options,
|
72 |
| - setup: ({ kit, unsupported }) => { |
| 74 | + setup: ({ kit, unsupported, cwd, typescript }) => { |
| 75 | + const ext = typescript ? 'ts' : 'js'; |
73 | 76 | if (!kit) unsupported('Requires SvelteKit');
|
| 77 | + |
| 78 | + const baseDBPath = path.resolve(kit!.libDirectory, 'server', 'db'); |
| 79 | + const paths = { |
| 80 | + 'drizzle config': path.relative(cwd, path.resolve(cwd, `drizzle.config.${ext}`)), |
| 81 | + 'database schema': path.relative(cwd, path.resolve(baseDBPath, `schema.${ext}`)), |
| 82 | + database: path.relative(cwd, path.resolve(baseDBPath, `index.${ext}`)) |
| 83 | + }; |
| 84 | + for (const [fileType, filePath] of Object.entries(paths)) { |
| 85 | + if (fs.existsSync(filePath)) { |
| 86 | + unsupported(`Preexisting ${fileType} file at '${filePath}'`); |
| 87 | + } |
| 88 | + } |
74 | 89 | },
|
75 | 90 | run: ({ sv, typescript, options, kit }) => {
|
76 | 91 | const ext = typescript ? 'ts' : 'js';
|
@@ -178,41 +193,27 @@ export default defineAddon({
|
178 | 193 |
|
179 | 194 | imports.addNamed(ast, 'drizzle-kit', { defineConfig: 'defineConfig' });
|
180 | 195 |
|
181 |
| - const envCheckStatement = common.statementFromString( |
182 |
| - "if (!process.env.DATABASE_URL) throw new Error('DATABASE_URL is not set');" |
| 196 | + common.addStatement( |
| 197 | + ast, |
| 198 | + common.statementFromString( |
| 199 | + "if (!process.env.DATABASE_URL) throw new Error('DATABASE_URL is not set');" |
| 200 | + ) |
183 | 201 | );
|
184 |
| - common.addStatement(ast, envCheckStatement); |
185 |
| - |
186 |
| - const fallback = common.expressionFromString('defineConfig({})'); |
187 |
| - const { value: exportDefault } = exports.defaultExport(ast, fallback); |
188 |
| - if (exportDefault.type !== 'CallExpression') return content; |
189 |
| - |
190 |
| - const objExpression = exportDefault.arguments?.[0]; |
191 |
| - if (!objExpression || objExpression.type !== 'ObjectExpression') return content; |
192 |
| - |
193 |
| - const authToken = |
194 |
| - options.sqlite === 'turso' |
195 |
| - ? common.expressionFromString('process.env.DATABASE_AUTH_TOKEN') |
196 |
| - : undefined; |
197 |
| - |
198 |
| - object.properties(objExpression, { |
199 |
| - schema: common.createLiteral(`./src/lib/server/db/schema.${typescript ? 'ts' : 'js'}`), |
200 |
| - dbCredentials: object.create({ |
201 |
| - url: common.expressionFromString('process.env.DATABASE_URL'), |
202 |
| - authToken |
203 |
| - }), |
204 |
| - verbose: { type: 'Literal', value: true }, |
205 |
| - strict: { type: 'Literal', value: true } |
206 |
| - }); |
207 |
| - |
208 |
| - const dialect = options.sqlite === 'turso' ? 'turso' : options.database; |
209 |
| - object.overrideProperties(objExpression, { |
210 |
| - dialect: common.createLiteral(dialect) |
211 |
| - }); |
212 | 202 |
|
213 |
| - // The `driver` property is only required for _some_ sqlite DBs. |
214 |
| - // We'll need to remove it if it's anything but sqlite |
215 |
| - if (options.database !== 'sqlite') object.removeProperty(objExpression, 'driver'); |
| 203 | + exports.defaultExport( |
| 204 | + ast, |
| 205 | + common.expressionFromString(` |
| 206 | + defineConfig({ |
| 207 | + schema: "./src/lib/server/db/schema.${typescript ? 'ts' : 'js'}", |
| 208 | + dialect: "${options.sqlite === 'turso' ? 'turso' : options.database}", |
| 209 | + dbCredentials: { |
| 210 | + ${options.sqlite === 'turso' ? 'authToken: process.env.DATABASE_AUTH_TOKEN,' : ''} |
| 211 | + url: process.env.DATABASE_URL |
| 212 | + }, |
| 213 | + verbose: true, |
| 214 | + strict: true |
| 215 | + })`) |
| 216 | + ); |
216 | 217 |
|
217 | 218 | return generateCode();
|
218 | 219 | });
|
|
0 commit comments