Skip to content

Commit

Permalink
fix(nuxt): drizzle watch mode
Browse files Browse the repository at this point in the history
  • Loading branch information
productdevbook committed Jan 19, 2024
1 parent 496481d commit 1b79806
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 17 deletions.
3 changes: 3 additions & 0 deletions packages/nuxt/playground/pergel/test.docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ services:
- type: volume
source: pergel-postgres
target: /var/lib/postgresql/data
ports:
- target: 5432
published: 5432
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
Expand Down
16 changes: 15 additions & 1 deletion packages/nuxt/src/module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { join } from 'node:path'
import { join, relative } from 'node:path'
import { writeFileSync } from 'node:fs'
import {
addTemplate,
Expand Down Expand Up @@ -155,6 +155,20 @@ export default defineNuxtModule<PergelOptions>({
nuxt._pergel.exitPergelFolder && writeFileSync(file, envTemplate, {
encoding: 'utf8',
})

nuxt._pergel.watchDirs = nuxt._pergel.projects
? Object.keys(nuxt._pergel.projects).map((projectName) => {
const project = nuxt._pergel.projects[projectName] as any

return Object.keys(project).map(moduleName => ({
projectName,
moduleName,
serverDir: relative(nuxt.options.rootDir, project[moduleName].serverDir),
rootModuleDir: relative(nuxt.options.rootDir, project[moduleName].rootModuleDir),
}))
},
).flat()
: []
},
})

Expand Down
7 changes: 7 additions & 0 deletions packages/nuxt/src/runtime/core/types/nuxtModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,13 @@ export interface ResolvedPergelOptions {
workspaceMode: boolean

serverDir: string

watchDirs: {
serverDir: string
rootModuleDir: string
projectName: string
moduleName: string
}[]
}

export interface NuxtPergel extends Nuxt {
Expand Down
32 changes: 22 additions & 10 deletions packages/nuxt/src/runtime/core/utils/globs.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
import { join, relative, resolve } from 'node:path'
import { join, resolve } from 'node:path'
import { matchGlobs } from '../../modules/graphqlYoga/utils'
import type { NuxtPergel, PergelModuleNames } from '../types/nuxtModule'
import type { NuxtPergel } from '../types/nuxtModule'

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 dirs = nuxt._pergel.watchDirs
const match = matchGlobs(
absolutePath,
[...dirs.map(({ serverDir }) => {
return join('**', serverDir, '**')
}), ...dirs.map(({ rootModuleDir }) => {
return join('**', rootModuleDir, '**')
})],
)

const groups = relativePath.match(/pergel\/(?<projectName>[^\/]+)\/(?<moduleName>[^\/]+)/)?.groups
const { projectName, moduleName } = groups || {}
if (!match) {
return {
match: false,
}
}

const match = matchGlobs(absolutePath, [join('**', folder, '**', `*${ext}`)])
const { serverDir, projectName, moduleName } = dirs.find(({ serverDir, rootModuleDir }) => {
return match.glob.includes(serverDir) || match.glob.includes(rootModuleDir)
})!

return {
match,
match: true,
serverDir,
projectName,
moduleName: moduleName as PergelModuleNames,
moduleName,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ export default {
target: '/var/lib/postgresql/data',
},
],
ports: [
{
target: 5432,
published: 5432,
},
],
environment: {
POSTGRES_USER: 'postgres',
POSTGRES_PASSWORD: 'postgres',
Expand Down
6 changes: 2 additions & 4 deletions packages/nuxt/src/runtime/modules/drizzle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,17 +175,15 @@ export default definePergelModule<DrizzleConfig, ResolvedDrizzleConfig>({
},
`

// Watch for changes
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
const activeProject = (nuxt._pergel.rootOptions.projects as any)[projectName][moduleName] as DrizzleConfig

if (!activeProject)
return
Expand Down
8 changes: 6 additions & 2 deletions packages/nuxt/src/runtime/modules/graphqlYoga/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ import { join } from 'pathe'

export function matchGlobs(filepath: string, globs: string[]) {
for (const glob of globs) {
if (minimatch(join(filepath), glob))
return true
if (minimatch(join(filepath), glob)) {
return {
glob,
status: true,
}
}
}
return false
}
Expand Down

0 comments on commit 1b79806

Please sign in to comment.