|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const { spawnSync } = require("child_process") |
| 4 | + |
| 5 | +// Check if we're already bootstrapping |
| 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 | +// Check if we're running under pnpm |
| 12 | +const isPnpm = process.env.npm_execpath && process.env.npm_execpath.includes("pnpm") |
| 13 | + |
| 14 | +// If we're already using pnpm, just exit normally |
| 15 | +if (isPnpm) { |
| 16 | + console.log("Already using pnpm, continuing with normal installation...") |
| 17 | + process.exit(0) |
| 18 | +} |
| 19 | + |
| 20 | +console.log("Bootstrapping to pnpm...") |
| 21 | + |
| 22 | +try { |
| 23 | + // Check if pnpm is installed |
| 24 | + const pnpmCheck = spawnSync("command", ["-v", "pnpm"], { shell: true }) |
| 25 | + |
| 26 | + let pnpmInstall |
| 27 | + |
| 28 | + if (pnpmCheck.status === 0) { |
| 29 | + // If pnpm is available, use it directly |
| 30 | + console.log("pnpm found, using it directly...") |
| 31 | + pnpmInstall = spawnSync("pnpm", ["install"], { |
| 32 | + stdio: "inherit", |
| 33 | + shell: true, |
| 34 | + env: { |
| 35 | + ...process.env, |
| 36 | + BOOTSTRAP_IN_PROGRESS: "1", // Set environment variable to indicate bootstrapping |
| 37 | + }, |
| 38 | + }) |
| 39 | + } else { |
| 40 | + // If pnpm is not available, install it temporarily in the project |
| 41 | + console.log("pnpm not found, installing it temporarily...") |
| 42 | + |
| 43 | + // Create a temporary package.json if it doesn't exist |
| 44 | + const tempPkgJson = spawnSync( |
| 45 | + "node", |
| 46 | + [ |
| 47 | + "-e", |
| 48 | + 'if(!require("fs").existsSync("package.json")){require("fs").writeFileSync("package.json", JSON.stringify({name:"temp",private:true}))}', |
| 49 | + ], |
| 50 | + { shell: true }, |
| 51 | + ) |
| 52 | + |
| 53 | + // Install pnpm locally without saving it as a dependency |
| 54 | + const npmInstall = spawnSync("npm", ["install", "--no-save", "pnpm"], { |
| 55 | + stdio: "inherit", |
| 56 | + shell: true, |
| 57 | + }) |
| 58 | + |
| 59 | + if (npmInstall.status !== 0) { |
| 60 | + console.error("Failed to install pnpm locally") |
| 61 | + process.exit(1) |
| 62 | + } |
| 63 | + |
| 64 | + // Use the locally installed pnpm |
| 65 | + console.log("Running pnpm install...") |
| 66 | + pnpmInstall = spawnSync("node_modules/.bin/pnpm", ["install"], { |
| 67 | + stdio: "inherit", |
| 68 | + shell: true, |
| 69 | + env: { |
| 70 | + ...process.env, |
| 71 | + BOOTSTRAP_IN_PROGRESS: "1", // Set environment variable to indicate bootstrapping |
| 72 | + }, |
| 73 | + }) |
| 74 | + } |
| 75 | + |
| 76 | + if (pnpmInstall.status !== 0) { |
| 77 | + console.error("pnpm install failed") |
| 78 | + process.exit(pnpmInstall.status) |
| 79 | + } |
| 80 | + |
| 81 | + console.log("Bootstrap completed successfully") |
| 82 | + process.exit(0) |
| 83 | +} catch (error) { |
| 84 | + console.error("Bootstrap failed:", error) |
| 85 | + process.exit(1) |
| 86 | +} |
0 commit comments