diff --git a/packages/nuxt/.vscode/settings.json b/packages/nuxt/.vscode/settings.json new file mode 100644 index 00000000..6f28c8a4 --- /dev/null +++ b/packages/nuxt/.vscode/settings.json @@ -0,0 +1,4 @@ +{ + "eslint.experimental.useFlatConfig": true, + "references.preferredLocation": "peek" +} \ No newline at end of file diff --git a/packages/nuxt/playground/pergel/README.yaml b/packages/nuxt/playground/pergel/README.yaml index fb8d7497..cb38cf72 100644 --- a/packages/nuxt/playground/pergel/README.yaml +++ b/packages/nuxt/playground/pergel/README.yaml @@ -1,6 +1,6 @@ pergel: # This file is generated by pergel. Do not edit it manually. - # Version: 0.4.0-beta.0 + # Version: 0.4.0-beta.1 test: drizzle: # Script Commands diff --git a/packages/nuxt/src/runtime/core/utils/globs.ts b/packages/nuxt/src/runtime/core/utils/globs.ts new file mode 100644 index 00000000..7a52bcca --- /dev/null +++ b/packages/nuxt/src/runtime/core/utils/globs.ts @@ -0,0 +1,24 @@ +import { join, relative, resolve } from 'node:path' +import { matchGlobs } from '../../modules/graphqlYoga/utils' +import type { ModuleName, NuxtPergel } from '../types' + +export function globsBuilderWatch( + nuxt: NuxtPergel, + path: string, + folder: string, + ext: string, +) { + const absolutePath = resolve(nuxt.options.rootDir, path) + const relativePath = relative(nuxt.options.rootDir, path) + + const groups = relativePath.match(/pergel\/(?[^\/]+)\/(?[^\/]+)/)?.groups + const { projectName, moduleName } = groups || {} + + const match = matchGlobs(absolutePath, [join('**', folder, '**', `*${ext}`)]) + + return { + match, + projectName, + moduleName: moduleName as ModuleName, + } +} diff --git a/packages/nuxt/src/runtime/modules/drizzle/index.ts b/packages/nuxt/src/runtime/modules/drizzle/index.ts index 9a4b1a67..74d0f154 100644 --- a/packages/nuxt/src/runtime/modules/drizzle/index.ts +++ b/packages/nuxt/src/runtime/modules/drizzle/index.ts @@ -1,13 +1,17 @@ import { existsSync, mkdirSync } from 'node:fs' import { resolve } from 'node:path' -import { addImportsDir, addServerImportsDir, createResolver } from '@nuxt/kit' +import { execSync } from 'node:child_process' +import { addImportsDir, addServerImportsDir, createResolver, useLogger } from '@nuxt/kit' import { camelCase, pascalCase } from 'scule' import { definePergelModule } from '../../core/definePergel' import { useNitroImports } from '../../core/utils/useImports' -import type { ResolvedDrizzleConfig } from './types' +import { globsBuilderWatch } from '../../core/utils/globs' +import type { DrizzleConfig, ResolvedDrizzleConfig } from './types' import { setupPostgres } from './drivers/postgres' import { copyMigrationFolder } from './core' +const _logger = useLogger('pergel:drizzle') + export default definePergelModule({ meta: { name: 'drizzle', @@ -154,6 +158,36 @@ export default definePergelModule({ }, ` + nuxt.hook('builder:watch', async (event, path) => { + // TODO: add support module name + const { match, projectName, moduleName } = globsBuilderWatch( + nuxt, + path, + 'drizzle', + '.ts', + ) + + if (projectName) { + const activeProject = nuxt._pergel.rootOptions.projects[projectName][moduleName] as DrizzleConfig + + if (!activeProject) + return + + if (match) { + if (activeProject.dev?.cli !== false) { + execSync( + activeProject.dev?.cli ?? `pergel orm -s=push -p=${projectName}`, + { + stdio: 'inherit', + cwd: nuxt.options.rootDir, + }, + ) + _logger.info(`Pushed ${projectName} schema`) + } + } + } + }) + nuxt._pergel.contents.push({ moduleName: 'drizzle', projectName, diff --git a/packages/nuxt/src/runtime/modules/drizzle/types.ts b/packages/nuxt/src/runtime/modules/drizzle/types.ts index 6c58474b..5b3bbfa7 100644 --- a/packages/nuxt/src/runtime/modules/drizzle/types.ts +++ b/packages/nuxt/src/runtime/modules/drizzle/types.ts @@ -36,6 +36,14 @@ export interface DrizzleConfig { * @default true */ studio?: false + + dev?: { + /** + * Database seeds + * @default 'pergel orm -s=push -p={projectName}'' + */ + cli?: string | false + } } export interface ResolvedDrizzleConfig { @@ -84,6 +92,14 @@ export interface ResolvedDrizzleConfig { } studio: boolean + + dev?: { + /** + * Database seeds + * @default 'pergel orm -s=push -p={projectName}'' + */ + cli: string | boolean + } } export interface PostgresJSOptions {