bootstrap.js 2.3 KB

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