|
2 | 2 | import { $ } from "bun"; |
3 | 3 | import { join } from "path"; |
4 | 4 | import { homedir } from "os"; |
5 | | -import { existsSync, readdirSync } from "fs"; |
| 5 | +import { existsSync, readdirSync, mkdirSync, writeFileSync, chmodSync, rmSync } from "fs"; |
6 | 6 |
|
7 | 7 | const HOME = homedir(); |
8 | 8 | const BVM_DIR = join(HOME, ".bvm"); |
9 | 9 | const SHIMS_DIR = join(BVM_DIR, "shims"); |
10 | 10 | const BIN_DIR = join(BVM_DIR, "bin"); |
11 | 11 |
|
12 | | -console.log("\n🚀 Starting E2E NPM Verification Protocol...\n"); |
13 | | - |
14 | | -try { |
15 | | - // 1. Cleanup |
16 | | - console.log(`🧹 Cleaning up environment (${BVM_DIR})...`); |
17 | | - await $`rm -rf ${BVM_DIR}`; |
18 | | - await $`rm -f bvm-core-*.tgz`; |
19 | | - |
20 | | - // 2. Build & Pack |
21 | | - console.log("📦 Building and Packing..."); |
22 | | - // Using simple shell command to ensure npm run build executes correctly |
23 | | - await $`npm run build`.quiet(); |
24 | | - await $`npm pack`.quiet(); |
25 | | - |
26 | | - // Find the tarball |
27 | | - const files = readdirSync(process.cwd()); |
28 | | - const tarball = files.find(f => f.startsWith("bvm-core-") && f.endsWith(".tgz")); |
29 | | - if (!tarball) throw new Error("Tarball not found after packing!"); |
30 | | - console.log(`📝 Found tarball: ${tarball}`); |
31 | | - |
32 | | - // 3. Install |
33 | | - console.log("💿 Installing globally via NPM (this may take a moment)..."); |
34 | | - // We capture stdout/stderr to show it only on failure, or stream it? |
35 | | - // Let's stream it to be transparent as requested. |
36 | | - await $`npm install -g ./${tarball} --foreground-scripts --force`; |
37 | | - |
38 | | - // 4. Verify Physical Structure |
39 | | - console.log("🔍 Verifying filesystem structure..."); |
40 | | - |
41 | | - if (!existsSync(SHIMS_DIR)) throw new Error(`❌ Shims directory missing: ${SHIMS_DIR}`); |
42 | | - if (!existsSync(join(SHIMS_DIR, "bun"))) throw new Error(`❌ Bun shim missing`); |
43 | | - if (!existsSync(join(BIN_DIR, "bvm"))) throw new Error(`❌ BVM binary missing`); |
44 | | - |
45 | | - console.log(" ✅ Shims directory exists."); |
46 | | - console.log(" ✅ Bun shim exists."); |
47 | | - |
48 | | - // 5. Functional Verification |
49 | | - console.log("🧪 Verifying BVM functionality..."); |
50 | | - const bvmExec = join(BIN_DIR, "bvm"); |
51 | | - const output = await $`${bvmExec} ls`.text(); |
52 | | - console.log(output.trim()); |
53 | | - |
54 | | - if (!output.includes("Locally installed Bun versions")) { |
55 | | - throw new Error("❌ 'bvm ls' output is unexpected."); |
| 12 | +export class E2ESandbox { |
| 13 | + public bvmDir: string; |
| 14 | + public shimsDir: string; |
| 15 | + public binDir: string; |
| 16 | + private tempHome: string; |
| 17 | + |
| 18 | + constructor() { |
| 19 | + this.tempHome = join(process.cwd(), `.tmp-npm-verify-${Date.now()}`); |
| 20 | + this.bvmDir = join(this.tempHome, ".bvm"); |
| 21 | + this.shimsDir = join(this.bvmDir, "shims"); |
| 22 | + this.binDir = join(this.bvmDir, "bin"); |
| 23 | + if (!existsSync(this.tempHome)) mkdirSync(this.tempHome, { recursive: true }); |
| 24 | + } |
| 25 | + |
| 26 | + async installLocal() { |
| 27 | + // Mock implementation for testing safety/upgrade logic without full NPM network hit |
| 28 | + if (!existsSync(this.bvmDir)) mkdirSync(this.bvmDir, { recursive: true }); |
| 29 | + if (!existsSync(this.shimsDir)) mkdirSync(this.shimsDir, { recursive: true }); |
| 30 | + if (!existsSync(this.binDir)) mkdirSync(this.binDir, { recursive: true }); |
| 31 | + |
| 32 | + const bvmBin = join(this.binDir, "bvm"); |
| 33 | + const bvmSrcDir = join(this.bvmDir, "src"); |
| 34 | + if (!existsSync(bvmSrcDir)) mkdirSync(bvmSrcDir, { recursive: true }); |
| 35 | + |
| 36 | + // Marker for NPM install |
| 37 | + const marker = join(this.bvmDir, ".npm-install"); |
| 38 | + writeFileSync(marker, "true"); |
| 39 | + |
| 40 | + // Dummy wrapper |
| 41 | + writeFileSync(bvmBin, `#!/bin/bash\nexport BVM_INSTALL_SOURCE="npm"\nexec ${process.execPath} ${join(this.bvmDir, "src", "index.js")} "$@"\n`); |
| 42 | + chmodSync(bvmBin, 0o755); |
| 43 | + |
| 44 | + // Dummy source |
| 45 | + writeFileSync(join(bvmSrcDir, "index.js"), "import '...'; console.log('bvm')"); |
56 | 46 | } |
57 | 47 |
|
58 | | - // 6. Global Package Isolation Verification (NEW) |
59 | | - console.log("📦 Verifying Global Package Isolation (Option B)..."); |
60 | | - // Ensure we are using a version |
61 | | - await $`${bvmExec} use default`.quiet(); |
62 | | - // Simulate bun install -g |
63 | | - console.log(" Installing dummy global package..."); |
64 | | - await $`${join(SHIMS_DIR, "bun")} install -g fake-pkg-test-bvm`.quiet().catch(() => {}); |
65 | | - |
66 | | - const currentBin = join(BVM_DIR, "current", "bin"); |
67 | | - console.log(` Checking if current bin path is correctly set up: ${currentBin}`); |
68 | | - |
69 | | - // We check if current/bin is in the PATH reported by setup |
70 | | - const zshrc = await $`cat ${join(HOME, ".zshrc")}`.text(); |
71 | | - if (!zshrc.includes("current/bin")) { |
72 | | - throw new Error("❌ .zshrc does not contain 'current/bin' in PATH"); |
| 48 | + cleanup() { |
| 49 | + if (existsSync(this.tempHome)) rmSync(this.tempHome, { recursive: true, force: true }); |
73 | 50 | } |
| 51 | +} |
| 52 | + |
| 53 | +async function runProtocol() { |
| 54 | + console.log("\n🚀 Starting E2E NPM Verification Protocol...\n"); |
| 55 | + |
| 56 | + try { |
| 57 | + // 1. Cleanup |
| 58 | + console.log(`🧹 Cleaning up environment (${BVM_DIR})...`); |
| 59 | + await $`rm -rf ${BVM_DIR}`; |
| 60 | + await $`rm -f bvm-core-*.tgz`.nothrow(); // Use nothrow to avoid error if no tgz exists |
| 61 | + |
| 62 | + // 2. Build & Pack |
| 63 | + console.log("📦 Building and Packing..."); |
| 64 | + // Using simple shell command to ensure npm run build executes correctly |
| 65 | + await $`npm run build`.quiet(); |
| 66 | + await $`npm pack`.quiet(); |
| 67 | + |
| 68 | + // Find the tarball |
| 69 | + const files = readdirSync(process.cwd()); |
| 70 | + const tarball = files.find(f => f.startsWith("bvm-core-") && f.endsWith(".tgz")); |
| 71 | + if (!tarball) throw new Error("Tarball not found after packing!"); |
| 72 | + console.log(`📝 Found tarball: ${tarball}`); |
| 73 | + |
| 74 | + // 3. Install |
| 75 | + console.log("💿 Installing globally via NPM (this may take a moment)..."); |
| 76 | + // We capture stdout/stderr to show it only on failure, or stream it? |
| 77 | + // Let's stream it to be transparent as requested. |
| 78 | + await $`npm install -g ./${tarball} --foreground-scripts --force`; |
74 | 79 |
|
75 | | - console.log("\n✅ \x1b[32mE2E VERIFICATION PASSED!\x1b[0m"); |
76 | | - console.log(" BVM is installed, shims exist, and CLI works."); |
77 | | - console.log(` Run 'source ~/.zshrc' (or your shell config) to start using it.`); |
| 80 | + // 4. Verify Physical Structure |
| 81 | + console.log("🔍 Verifying filesystem structure..."); |
| 82 | + |
| 83 | + if (!existsSync(SHIMS_DIR)) throw new Error(`❌ Shims directory missing: ${SHIMS_DIR}`); |
| 84 | + if (!existsSync(join(SHIMS_DIR, "bun"))) throw new Error(`❌ Bun shim missing`); |
| 85 | + if (!existsSync(join(BIN_DIR, "bvm"))) throw new Error(`❌ BVM binary missing`); |
| 86 | + |
| 87 | + console.log(" ✅ Shims directory exists."); |
| 88 | + console.log(" ✅ Bun shim exists."); |
| 89 | + |
| 90 | + // 5. Functional Verification |
| 91 | + console.log("🧪 Verifying BVM functionality..."); |
| 92 | + const bvmExec = join(BIN_DIR, "bvm"); |
| 93 | + const output = await $`${bvmExec} ls`.text(); |
| 94 | + console.log(output.trim()); |
| 95 | + |
| 96 | + if (!output.includes("Locally installed Bun versions")) { |
| 97 | + throw new Error("❌ 'bvm ls' output is unexpected."); |
| 98 | + } |
| 99 | + |
| 100 | + // 6. Global Package Isolation Verification (NEW) |
| 101 | + console.log("📦 Verifying Global Package Isolation (Option B)..."); |
| 102 | + // Ensure we are using a version |
| 103 | + await $`${bvmExec} use default`.quiet(); |
| 104 | + // Simulate bun install -g |
| 105 | + console.log(" Installing dummy global package..."); |
| 106 | + await $`${join(SHIMS_DIR, "bun")} install -g fake-pkg-test-bvm`.quiet().catch(() => {}); |
| 107 | + |
| 108 | + const currentBin = join(BVM_DIR, "current", "bin"); |
| 109 | + console.log(` Checking if current bin path is correctly set up: ${currentBin}`); |
| 110 | + |
| 111 | + // We check if current/bin is in the PATH reported by setup |
| 112 | + const zshrc = await $`cat ${join(HOME, ".zshrc")}`.text(); |
| 113 | + if (!zshrc.includes("current/bin")) { |
| 114 | + throw new Error("❌ .zshrc does not contain 'current/bin' in PATH"); |
| 115 | + } |
| 116 | + |
| 117 | + console.log("\n✅ \x1b[32mE2E VERIFICATION PASSED!\x1b[0m"); |
| 118 | + console.log(" BVM is installed, shims exist, and CLI works."); |
| 119 | + console.log(` Run 'source ~/.zshrc' (or your shell config) to start using it.`); |
| 120 | + |
| 121 | + } catch (e) { |
| 122 | + console.error("\n💥 \x1b[31mVERIFICATION FAILED:\x1b[0m"); |
| 123 | + console.error(e); |
| 124 | + process.exit(1); |
| 125 | + } |
| 126 | +} |
78 | 127 |
|
79 | | -} catch (e) { |
80 | | - console.error("\n💥 \x1b[31mVERIFICATION FAILED:\x1b[0m"); |
81 | | - console.error(e); |
82 | | - process.exit(1); |
| 128 | +// Check if this script is being run directly |
| 129 | +if (import.meta.main) { |
| 130 | + runProtocol(); |
83 | 131 | } |
0 commit comments