|
1 | | - |
2 | 1 | import type * as BunTest from 'bun:test' |
3 | 2 | import { config as loadDotenv } from 'dotenv' |
4 | | -import { join } from 'path' |
| 3 | +import { join, dirname, basename } from 'path' |
| 4 | +import { mkdir } from 'fs/promises' |
5 | 5 |
|
6 | 6 | // Global cache for loaded env files (this is fine as a cache) |
7 | 7 | const loadedEnvFiles = new Set<string>() |
@@ -67,6 +67,35 @@ export async function capsule({ |
67 | 67 | return process.env[envVarName] |
68 | 68 | } |
69 | 69 | }, |
| 70 | + workbenchDir: { |
| 71 | + type: CapsulePropertyTypes.GetterFunction, |
| 72 | + value: function (this: any): string { |
| 73 | + |
| 74 | + const moduleFilepath = this['#@stream44.studio/encapsulate/structs/Capsule'].rootCapsule.moduleFilepath |
| 75 | + const dir = join(dirname(moduleFilepath), '.~o/workspace.foundation/workbenches', basename(moduleFilepath).replace(/\.[^\.]+$/, '')) |
| 76 | + |
| 77 | + return dir |
| 78 | + } |
| 79 | + }, |
| 80 | + emptyWorkbenchDir: { |
| 81 | + type: CapsulePropertyTypes.Function, |
| 82 | + value: async function (this: any): Promise<void> { |
| 83 | + const dir = this.workbenchDir |
| 84 | + |
| 85 | + // Ensure the directory exists first |
| 86 | + await mkdir(dir, { recursive: true }) |
| 87 | + |
| 88 | + // Remove directory contents (not the directory itself) including dotfiles |
| 89 | + // Use shell with proper globbing to handle both regular files and dotfiles |
| 90 | + await Bun.$`sh -c 'rm -rf ${dir}/* ${dir}/.[!.]* ${dir}/..?* 2>/dev/null || true'`.quiet() |
| 91 | + } |
| 92 | + }, |
| 93 | + EnsureEmptyWorkbenchDir: { |
| 94 | + type: CapsulePropertyTypes.StructInit, |
| 95 | + value: async function (this: any) { |
| 96 | + await this.emptyWorkbenchDir() |
| 97 | + } |
| 98 | + }, |
70 | 99 | describe: { |
71 | 100 | type: CapsulePropertyTypes.GetterFunction, |
72 | 101 | value: function (this: any) { |
@@ -161,7 +190,7 @@ export async function capsule({ |
161 | 190 | }, { |
162 | 191 | importMeta: import.meta, |
163 | 192 | importStack: makeImportStack(), |
164 | | - capsuleName: capsule['#'], |
| 193 | + capsuleName: capsule['#'] |
165 | 194 | }) |
166 | 195 | } |
167 | 196 | capsule['#'] = 't44/caps/WorkspaceTest' |
0 commit comments