Browse Source

Convert bootstrap script to ESM (#3904)

Chris Estreich 8 months ago
parent
commit
78bb7e0256
4 changed files with 89 additions and 89 deletions
  1. 5 0
      .changeset/ready-guests-hide.md
  2. 3 3
      package.json
  3. 0 86
      scripts/bootstrap.js
  4. 81 0
      scripts/bootstrap.mjs

+ 5 - 0
.changeset/ready-guests-hide.md

@@ -0,0 +1,5 @@
+---
+"roo-cline": patch
+---
+
+Convert bootstrap script to esm

+ 3 - 3
package.json

@@ -5,10 +5,10 @@
 		"node": "20.18.1"
 	},
 	"scripts": {
-		"preinstall": "node scripts/bootstrap.js",
+		"preinstall": "node scripts/bootstrap.mjs",
 		"prepare": "husky",
-		"install": "node scripts/bootstrap.js",
-		"install:all": "node scripts/bootstrap.js",
+		"install": "node scripts/bootstrap.mjs",
+		"install:all": "node scripts/bootstrap.mjs",
 		"lint": "turbo lint --log-order grouped --output-logs new-only",
 		"check-types": "turbo check-types --log-order grouped --output-logs new-only",
 		"test": "turbo test --log-order grouped --output-logs new-only",

+ 0 - 86
scripts/bootstrap.js

@@ -1,86 +0,0 @@
-#!/usr/bin/env node
-
-const { spawnSync } = require("child_process")
-
-// Check if we're already bootstrapping
-if (process.env.BOOTSTRAP_IN_PROGRESS) {
-	console.log("Bootstrap already in progress, continuing with normal installation...")
-	process.exit(0)
-}
-
-// Check if we're running under pnpm
-const isPnpm = process.env.npm_execpath && process.env.npm_execpath.includes("pnpm")
-
-// If we're already using pnpm, just exit normally
-if (isPnpm) {
-	console.log("Already using pnpm, continuing with normal installation...")
-	process.exit(0)
-}
-
-console.log("Bootstrapping to pnpm...")
-
-try {
-	// Check if pnpm is installed
-	const pnpmCheck = spawnSync("command", ["-v", "pnpm"], { shell: true })
-
-	let pnpmInstall
-
-	if (pnpmCheck.status === 0) {
-		// If pnpm is available, use it directly
-		console.log("pnpm found, using it directly...")
-		pnpmInstall = spawnSync("pnpm", ["install"], {
-			stdio: "inherit",
-			shell: true,
-			env: {
-				...process.env,
-				BOOTSTRAP_IN_PROGRESS: "1", // Set environment variable to indicate bootstrapping
-			},
-		})
-	} else {
-		// If pnpm is not available, install it temporarily in the project
-		console.log("pnpm not found, installing it temporarily...")
-
-		// Create a temporary package.json if it doesn't exist
-		const tempPkgJson = spawnSync(
-			"node",
-			[
-				"-e",
-				'if(!require("fs").existsSync("package.json")){require("fs").writeFileSync("package.json", JSON.stringify({name:"temp",private:true}))}',
-			],
-			{ shell: true },
-		)
-
-		// Install pnpm locally without saving it as a dependency
-		const npmInstall = spawnSync("npm", ["install", "--no-save", "pnpm"], {
-			stdio: "inherit",
-			shell: true,
-		})
-
-		if (npmInstall.status !== 0) {
-			console.error("Failed to install pnpm locally")
-			process.exit(1)
-		}
-
-		// Use the locally installed pnpm
-		console.log("Running pnpm install...")
-		pnpmInstall = spawnSync("node_modules/.bin/pnpm", ["install"], {
-			stdio: "inherit",
-			shell: true,
-			env: {
-				...process.env,
-				BOOTSTRAP_IN_PROGRESS: "1", // Set environment variable to indicate bootstrapping
-			},
-		})
-	}
-
-	if (pnpmInstall.status !== 0) {
-		console.error("pnpm install failed")
-		process.exit(pnpmInstall.status)
-	}
-
-	console.log("Bootstrap completed successfully")
-	process.exit(0)
-} catch (error) {
-	console.error("Bootstrap failed:", error)
-	process.exit(1)
-}

+ 81 - 0
scripts/bootstrap.mjs

@@ -0,0 +1,81 @@
+#!/usr/bin/env node
+
+import { spawnSync } from "child_process"
+import { existsSync, writeFileSync } from "fs"
+
+if (process.env.BOOTSTRAP_IN_PROGRESS) {
+	console.log("⏭️  Bootstrap already in progress, continuing with normal installation...")
+	process.exit(0)
+}
+
+// If we're already using pnpm, just exit normally.
+if (process.env.npm_execpath && process.env.npm_execpath.includes("pnpm")) {
+	process.exit(0)
+}
+
+console.log("🚀 Bootstrapping to pnpm...")
+
+/**
+ * Run pnpm install with bootstrap environment variable.
+ */
+function runPnpmInstall(pnpmCommand) {
+	return spawnSync(pnpmCommand, ["install"], {
+		stdio: "inherit",
+		shell: true,
+		env: {
+			...process.env,
+			BOOTSTRAP_IN_PROGRESS: "1", // Set environment variable to indicate bootstrapping
+		},
+	})
+}
+
+/**
+ * Create a temporary package.json if it doesn't exist.
+ */
+function ensurePackageJson() {
+	if (!existsSync("package.json")) {
+		console.log("📦 Creating temporary package.json...")
+		writeFileSync("package.json", JSON.stringify({ name: "temp", private: true }, null, 2))
+	}
+}
+
+try {
+	// Check if pnpm is installed globally.
+	const pnpmCheck = spawnSync("command", ["-v", "pnpm"], { shell: true })
+
+	let pnpmInstall
+
+	if (pnpmCheck.status === 0) {
+		console.log("✨ Found pnpm")
+		pnpmInstall = runPnpmInstall("pnpm")
+	} else {
+		console.log("⚠️  Unable to find pnpm, installing it temporarily...")
+		ensurePackageJson()
+
+		console.log("📥 Installing pnpm locally...")
+
+		const npmInstall = spawnSync("npm", ["install", "--no-save", "pnpm"], {
+			stdio: "inherit",
+			shell: true,
+		})
+
+		if (npmInstall.status !== 0) {
+			console.error("❌ Failed to install pnpm locally")
+			process.exit(1)
+		}
+
+		console.log("🔧 Running pnpm install with local installation...")
+		pnpmInstall = runPnpmInstall("node_modules/.bin/pnpm")
+	}
+
+	if (pnpmInstall.status !== 0) {
+		console.error("❌ pnpm install failed")
+		process.exit(pnpmInstall.status)
+	}
+
+	console.log("🎉 Bootstrap completed successfully!")
+	process.exit(0)
+} catch (error) {
+	console.error("💥 Bootstrap failed:", error.message)
+	process.exit(1)
+}