Skip to content

Commit

Permalink
feat(nuxt): update Pergel version and add builder watch support for D…
Browse files Browse the repository at this point in the history
…rizzle module
  • Loading branch information
productdevbook committed Dec 22, 2023
1 parent c410e4d commit 0bed4ae
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 3 deletions.
4 changes: 4 additions & 0 deletions packages/nuxt/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"eslint.experimental.useFlatConfig": true,
"references.preferredLocation": "peek"
}
2 changes: 1 addition & 1 deletion packages/nuxt/playground/pergel/README.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
24 changes: 24 additions & 0 deletions packages/nuxt/src/runtime/core/utils/globs.ts
Original file line number Diff line number Diff line change
@@ -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\/(?<projectName>[^\/]+)\/(?<moduleName>[^\/]+)/)?.groups
const { projectName, moduleName } = groups || {}

const match = matchGlobs(absolutePath, [join('**', folder, '**', `*${ext}`)])

return {
match,
projectName,
moduleName: moduleName as ModuleName,
}
}
38 changes: 36 additions & 2 deletions packages/nuxt/src/runtime/modules/drizzle/index.ts
Original file line number Diff line number Diff line change
@@ -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<ResolvedDrizzleConfig>({
meta: {
name: 'drizzle',
Expand Down Expand Up @@ -154,6 +158,36 @@ export default definePergelModule<ResolvedDrizzleConfig>({
},
`

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,
Expand Down
16 changes: 16 additions & 0 deletions packages/nuxt/src/runtime/modules/drizzle/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 0bed4ae

Please sign in to comment.