The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as Exit from "effect/Exit"; |
| 3 | import * as FileSystem from "effect/FileSystem"; |
| 4 | import * as Path from "effect/Path"; |
| 5 | import * as YAML from "yaml"; |
| 6 | |
| 7 | /** |
| 8 | * Resolve `catalog:` dependency specifiers in a cloned fixture's |
| 9 | * `package.json` to the concrete versions pinned in the workspace's |
| 10 | * `pnpm-workspace.yaml`. Fixtures keep `catalog:` refs so versions stay |
| 11 | * single-sourced, but a clone that installs OUTSIDE the workspace (e.g. |
| 12 | * the Nextjs fixture, cloned to the OS temp dir so OpenNext's output |
| 13 | * tracing sees a plain node_modules tree) has no workspace context — |
| 14 | * `bun install` fails on every `catalog:` ref without this rewrite. |
| 15 | */ |
| 16 | const resolveCatalogSpecifiers = Effect.fn(function* (dir: string) { |
| 17 | const fs = yield* FileSystem.FileSystem; |
| 18 | const path = yield* Path.Path; |
| 19 | const manifestPath = path.join(dir, "package.json"); |
| 20 | if (!(yield* fs.exists(manifestPath))) { |
| 21 | return; |
| 22 | } |
| 23 | // Fixture.ts lives at packages/alchemy/test/Cloudflare/Utils/ — the |
| 24 | // workspace root (and its pnpm-workspace.yaml) is five levels up. |
| 25 | const workspaceYaml = path.join(import.meta.dirname, "../../../../..", "pnpm-workspace.yaml"); |
| 26 | const workspace = YAML.parse(yield* fs.readFileString(workspaceYaml)) as { |
| 27 | catalog?: Record<string, string>; |
| 28 | catalogs?: Record<string, Record<string, string>>; |
| 29 | }; |
| 30 | const manifest = JSON.parse(yield* fs.readFileString(manifestPath)) as { |
| 31 | [section: string]: unknown; |
| 32 | }; |
| 33 | let changed = false; |
| 34 | for (const section of [ |
| 35 | "dependencies", |
| 36 | "devDependencies", |
| 37 | "peerDependencies", |
| 38 | "optionalDependencies", |
| 39 | ]) { |
| 40 | const deps = manifest[section] as Record<string, string> | undefined; |
| 41 | if (!deps) continue; |
| 42 | for (const [name, spec] of Object.entries(deps)) { |
| 43 | if (!spec.startsWith("catalog:")) continue; |
| 44 | const catalogName = spec.slice("catalog:".length); |
| 45 | const version = |
| 46 | catalogName === "" ? workspace.catalog?.[name] : workspace.catalogs?.[catalogName]?.[name]; |
| 47 | if (version === undefined) { |
| 48 | return yield* Effect.fail( |
| 49 | new Error( |
| 50 | `cloneFixture: ${name} uses "${spec}" but pnpm-workspace.yaml has no such catalog entry`, |
| 51 | ), |
| 52 | ); |
| 53 | } |
| 54 | deps[name] = version; |
| 55 | changed = true; |
| 56 | } |
| 57 | } |
| 58 | if (changed) { |
| 59 | yield* fs.writeFileString(manifestPath, `${JSON.stringify(manifest, null, 2)}\n`); |
| 60 | } |
| 61 | }); |
| 62 | |
| 63 | /** |
| 64 | * Recursively copy `sourceDir` into a fresh `fs.makeTempDirectory` |
| 65 | * (with `prefix`), preserving file modes. Returns the absolute path of |
| 66 | * the new temp directory. |
| 67 | * |
| 68 | * Used by tests that want to mutate a fixture's `src/` files without |
| 69 | * polluting the source-controlled fixture or racing with other tests |
| 70 | * sharing the same fixture path. |
| 71 | * |
| 72 | * - `tempRoot`, when provided, places the temp dir under a specific |
| 73 | * parent directory instead of the OS temp root. This matters for |
| 74 | * Vite tests because Vite's `vite:build-html` plugin can't express |
| 75 | * project roots that sit outside the current working directory; an |
| 76 | * under-workspace temp dir keeps the relative path representable. |
| 77 | * - `entries`, when provided, restricts the copy to a specific subset |
| 78 | * of top-level entries. Defaults to copying everything in |
| 79 | * `sourceDir`. |
| 80 | */ |
| 81 | export const cloneFixture = Effect.fn(function* ( |
| 82 | sourceDir: string, |
| 83 | options: { |
| 84 | prefix: string; |
| 85 | tempRoot?: string; |
| 86 | entries?: string[]; |
| 87 | }, |
| 88 | ) { |
| 89 | const fs = yield* FileSystem.FileSystem; |
| 90 | const path = yield* Path.Path; |
| 91 | |
| 92 | if (options.tempRoot) { |
| 93 | yield* fs.makeDirectory(options.tempRoot, { recursive: true }); |
| 94 | } |
| 95 | // Realpath the temp dir: macOS temp roots are symlinks (/tmp, /var/folders |
| 96 | // → /private/...), and file watchers (Turbopack HMR) report events under |
| 97 | // the real path — a symlinked project root never sees invalidations. |
| 98 | const dir = yield* fs.realPath( |
| 99 | yield* fs.makeTempDirectory({ |
| 100 | prefix: options.prefix, |
| 101 | directory: options.tempRoot, |
| 102 | }), |
| 103 | ); |
| 104 | |
| 105 | const entries = options.entries ?? (yield* fs.readDirectory(sourceDir)); |
| 106 | |
| 107 | const copyTree = (relativePath: string): Effect.Effect<void, any, any> => |
| 108 | Effect.gen(function* () { |
| 109 | const from = path.join(sourceDir, relativePath); |
| 110 | const to = path.join(dir, relativePath); |
| 111 | const stat = yield* fs.stat(from); |
| 112 | if (stat.type === "Directory") { |
| 113 | yield* fs.makeDirectory(to, { recursive: true }); |
| 114 | const children = yield* fs.readDirectory(from); |
| 115 | for (const child of children) { |
| 116 | yield* copyTree(path.join(relativePath, child)); |
| 117 | } |
| 118 | } else { |
| 119 | const contents = yield* fs.readFile(from); |
| 120 | yield* fs.writeFile(to, contents); |
| 121 | // Preserve executable bit so copied build scripts still run. |
| 122 | const mode = Number(stat.mode); |
| 123 | if (mode & 0o111) { |
| 124 | yield* fs.chmod(to, mode); |
| 125 | } |
| 126 | } |
| 127 | }); |
| 128 | |
| 129 | for (const entry of entries) { |
| 130 | yield* copyTree(entry); |
| 131 | } |
| 132 | |
| 133 | yield* resolveCatalogSpecifiers(dir); |
| 134 | |
| 135 | yield* Effect.addFinalizer( |
| 136 | Exit.match({ |
| 137 | onSuccess: () => Effect.ignore(fs.remove(dir, { recursive: true })), |
| 138 | onFailure: () => Effect.void, |
| 139 | }), |
| 140 | ); |
| 141 | |
| 142 | return dir; |
| 143 | }); |