Skip to content

Commit a3b1c81

Browse files
committed
perf(hooks): cache DDEV project detection
- Add _ddevProjectCache to avoid repeated filesystem/which() checks - Add resetDdevCache() function for test isolation - Call resetDdevCache() in test beforeEach hooks
1 parent 123147b commit a3b1c81

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

booster/.husky/shared/core.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,27 +123,49 @@ export function isSkipped(name: string): boolean {
123123
return process.env[skipEnvVar] === '1' || process.env[skipEnvVar] === 'true'
124124
}
125125

126+
// Cache for DDEV detection to avoid repeated filesystem checks
127+
let _ddevProjectCache: boolean | null = null
128+
126129
/**
127130
* Check if the current project is a DDEV project
131+
* Result is cached after first call for performance.
128132
* @returns True if .ddev/config.yaml exists and ddev binary is available
129133
*/
130134
export async function isDdevProject(): Promise<boolean> {
135+
// Return cached result if available
136+
if (_ddevProjectCache !== null) {
137+
return _ddevProjectCache
138+
}
139+
131140
// Allow explicit disable via env var
132141
if (process.env.DDEV_PHP === 'false' || process.env.DDEV_PHP === '0') {
142+
_ddevProjectCache = false
133143
return false
134144
}
135145

136146
const hasConfig = await fs.pathExists('.ddev/config.yaml')
137-
if (!hasConfig) return false
147+
if (!hasConfig) {
148+
_ddevProjectCache = false
149+
return false
150+
}
138151

139152
try {
140153
await which('ddev')
154+
_ddevProjectCache = true
141155
return true
142156
} catch {
157+
_ddevProjectCache = false
143158
return false
144159
}
145160
}
146161

162+
/**
163+
* Reset DDEV detection cache (useful for testing)
164+
*/
165+
export function resetDdevCache(): void {
166+
_ddevProjectCache = null
167+
}
168+
147169
/**
148170
* Get the DDEV project name from .ddev/config.yaml
149171
* @returns The project name or null if not found

booster/.husky/tests/shared/core.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
formatDuration,
66
isSkipped,
77
isDdevProject,
8+
resetDdevCache,
89
exec,
910
ensureMutagenSync
1011
} from '../../shared/core'
@@ -42,6 +43,7 @@ describe('core.ts', () => {
4243
beforeEach(() => {
4344
vi.clearAllMocks()
4445
vi.resetModules()
46+
resetDdevCache() // Reset DDEV cache between tests
4547
process.env = { ...process.env } // Clone env
4648
delete process.env.DDEV_PHP // Ensure this doesn't interfere
4749
})

0 commit comments

Comments
 (0)