bootstrap.mjs 2.0 KB

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