|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { spawnSync } from "child_process" |
| 4 | +import { existsSync, writeFileSync } from "fs" |
| 5 | + |
| 6 | +if (process.env.BOOTSTRAP_IN_PROGRESS) { |
| 7 | + console.log("⏭️ Bootstrap already in progress, continuing with normal installation...") |
| 8 | + process.exit(0) |
| 9 | +} |
| 10 | + |
| 11 | +// If we're already using pnpm, just exit normally. |
| 12 | +if (process.env.npm_execpath && process.env.npm_execpath.includes("pnpm")) { |
| 13 | + process.exit(0) |
| 14 | +} |
| 15 | + |
| 16 | +console.log("🚀 Bootstrapping to pnpm...") |
| 17 | + |
| 18 | +/** |
| 19 | + * Run pnpm install with bootstrap environment variable. |
| 20 | + */ |
| 21 | +function runPnpmInstall(pnpmCommand) { |
| 22 | + return spawnSync(pnpmCommand, ["install"], { |
| 23 | + stdio: "inherit", |
| 24 | + shell: true, |
| 25 | + env: { |
| 26 | + ...process.env, |
| 27 | + BOOTSTRAP_IN_PROGRESS: "1", // Set environment variable to indicate bootstrapping |
| 28 | + }, |
| 29 | + }) |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Create a temporary package.json if it doesn't exist. |
| 34 | + */ |
| 35 | +function ensurePackageJson() { |
| 36 | + if (!existsSync("package.json")) { |
| 37 | + console.log("📦 Creating temporary package.json...") |
| 38 | + writeFileSync("package.json", JSON.stringify({ name: "temp", private: true }, null, 2)) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +try { |
| 43 | + // Check if pnpm is installed globally. |
| 44 | + const pnpmCheck = spawnSync("command", ["-v", "pnpm"], { shell: true }) |
| 45 | + |
| 46 | + let pnpmInstall |
| 47 | + |
| 48 | + if (pnpmCheck.status === 0) { |
| 49 | + console.log("✨ Found pnpm") |
| 50 | + pnpmInstall = runPnpmInstall("pnpm") |
| 51 | + } else { |
| 52 | + console.log("⚠️ Unable to find pnpm, installing it temporarily...") |
| 53 | + ensurePackageJson() |
| 54 | + |
| 55 | + console.log("📥 Installing pnpm locally...") |
| 56 | + |
| 57 | + const npmInstall = spawnSync("npm", ["install", "--no-save", "pnpm"], { |
| 58 | + stdio: "inherit", |
| 59 | + shell: true, |
| 60 | + }) |
| 61 | + |
| 62 | + if (npmInstall.status !== 0) { |
| 63 | + console.error("❌ Failed to install pnpm locally") |
| 64 | + process.exit(1) |
| 65 | + } |
| 66 | + |
| 67 | + console.log("🔧 Running pnpm install with local installation...") |
| 68 | + pnpmInstall = runPnpmInstall("node_modules/.bin/pnpm") |
| 69 | + } |
| 70 | + |
| 71 | + if (pnpmInstall.status !== 0) { |
| 72 | + console.error("❌ pnpm install failed") |
| 73 | + process.exit(pnpmInstall.status) |
| 74 | + } |
| 75 | + |
| 76 | + console.log("🎉 Bootstrap completed successfully!") |
| 77 | + process.exit(0) |
| 78 | +} catch (error) { |
| 79 | + console.error("💥 Bootstrap failed:", error.message) |
| 80 | + process.exit(1) |
| 81 | +} |
0 commit comments