upgrading/extension/upgrades/0.11-to-0.12/migrate-contract-testing-imports.ts
upgrading/extension/upgrades/0.11-to-0.12/migrate-contract-testing-imports.tsBrowse 76 files
734 tokens
2,810 bytes
Token encoding: o200k_base
Snapshot fac8604
← Back to SKILL.md
1/**2 * Rewrites test-only imports from the removed `@internal/contract/testing`3 * subpath to `@repo/test-utils`.4 *5 * Background: starting at 0.12 the contract test factories (`createContract`,6 * `createSqlContract`, `DUMMY_HASH`, `applicationDomainOf`, …) live in7 * `@repo/test-utils`. The `@internal/contract/testing` export was8 * removed from `@internal/contract`.9 *10 * Behaviour:11 * - Walks the project root recursively, ignoring `node_modules`, `.git`,12 * `dist`, and `build`.13 * - Rewrites every `.ts` / `.tsx` file whose source contains14 * `@internal/contract/testing`.15 * - Idempotent: files already importing from `@repo/test-utils` are16 * left untouched.17 *18 * Flags:19 * --check dry-run; lists files that still need rewriting and exits 1 if20 * any remain.21 */22import { readdir, readFile, writeFile } from 'node:fs/promises';23import { join } from 'node:path';24 25const FROM = '@internal/contract/testing';26const TO = '@repo/test-utils';27 28const SKIP_DIRS = new Set(['node_modules', '.git', 'dist', 'build']);29 30const dryRun = process.argv.includes('--check');31const projectRoot = process.cwd();32 33async function findSourceFiles(root: string): Promise<string[]> {34 const out: string[] = [];35 36 async function walk(dir: string): Promise<void> {37 let entries: Awaited<ReturnType<typeof readdir>>;38 try {39 entries = await readdir(dir, { withFileTypes: true });40 } catch {41 return;42 }43 for (const entry of entries) {44 if (entry.isDirectory()) {45 if (SKIP_DIRS.has(entry.name)) continue;46 await walk(join(dir, entry.name));47 } else if (48 entry.isFile() &&49 (entry.name.endsWith('.ts') || entry.name.endsWith('.tsx')) &&50 entry.name !== 'migrate-contract-testing-imports.ts'51 ) {52 out.push(join(dir, entry.name));53 }54 }55 }56 57 await walk(root);58 return out.sort();59}60 61const files = await findSourceFiles(projectRoot);62const targets: string[] = [];63 64for (const path of files) {65 const raw = await readFile(path, 'utf-8');66 if (raw.includes(FROM)) targets.push(path);67}68 69if (targets.length === 0) {70 console.log('No @internal/contract/testing imports found.');71 process.exit(0);72}73 74let needsFix = 0;75let fixed = 0;76 77for (const path of targets) {78 const rel = path.slice(projectRoot.length + 1);79 const raw = await readFile(path, 'utf-8');80 const next = raw.replaceAll(FROM, TO);81 if (next === raw) continue;82 needsFix += 1;83 if (dryRun) {84 console.log(`WOULD REWRITE ${rel}`);85 continue;86 }87 await writeFile(path, next);88 fixed += 1;89 console.log(`REWRITE ${rel}`);90}91 92console.log();93console.log(94 `${targets.length} file(s) with legacy import: ${dryRun ? needsFix : fixed} ${dryRun ? 'needing rewrite' : 'rewritten'}.`,95);96 97if (dryRun && needsFix > 0) process.exit(1);98