diff --git a/.lintstagedrc.js b/.lintstagedrc.js index 717b7424a3c85a..74436ca10182cd 100644 --- a/.lintstagedrc.js +++ b/.lintstagedrc.js @@ -1,7 +1,7 @@ module.exports = { // Lint & Prettify TS and JS files - only the staged files "**/*.(ts|tsx|js|jsx)": (filenames) => [ - `cross-env NODE_ENV=test npx eslint --fix --max-warnings=0 ${filenames.map((f) => `'${f.replace(/'/g, "'\\''")}'`).join(" ")}`, + `cross-env NODE_ENV=test npx eslint --fix --max-warnings=0 --no-warn-ignored ${filenames.map((f) => `'${f.replace(/'/g, "'\\''")}'`).join(" ")}`, `npx prettier --write ${filenames.map((f) => `'${f.replace(/'/g, "'\\''")}'`).join(" ")}`, ], diff --git a/scripts/run-gatsby.js b/scripts/run-gatsby.js index 92c7c28a7c50e1..cf8cddde6109b4 100755 --- a/scripts/run-gatsby.js +++ b/scripts/run-gatsby.js @@ -103,34 +103,61 @@ if (!/--max-old-space-size/.test(env.NODE_OPTIONS || "")) { env.NODE_OPTIONS = `${env.NODE_OPTIONS || ""} ${heapFlag}`.trim(); } -const args = process.argv.slice(2); -console.info( - `[run-gatsby] ${totalGb.toFixed(1)} GB RAM / ${cores} cores -> ` + - `workers=${env.GATSBY_CPU_COUNT} sharp=${env.SHARP_CONCURRENCY} heap=${heapMb}MB`, -); - -const gatsbyBin = path.join( - __dirname, - "..", - "node_modules", - ".bin", - process.platform === "win32" ? "gatsby.cmd" : "gatsby", -); - -const child = spawn(gatsbyBin, args, { - env, - stdio: "inherit", - shell: process.platform === "win32", -}); +/** + * Resolve the Gatsby binary, preferring Windows shims in order. + * + * npm creates gatsby.cmd, while pnpm may create gatsby.exe. Bun's .bunx file + * is metadata, so use Gatsby's Node CLI entrypoint when that is the only shim. + * Non-Windows behavior is unchanged (plain `gatsby`). + */ +const resolveGatsbyBin = ({ + platform = process.platform, + existsSync = require("fs").existsSync, + binDir = path.join(__dirname, "..", "node_modules", ".bin"), +} = {}) => { + if (platform === "win32") { + for (const candidate of ["gatsby.cmd", "gatsby.exe"]) { + const full = path.join(binDir, candidate); + if (existsSync(full)) return full; + } + if (existsSync(path.join(binDir, "gatsby.bunx"))) { + return path.resolve(binDir, "..", "gatsby-cli", "cli.js"); + } + } + return path.join(binDir, "gatsby"); +}; -const forward = (signal) => () => child.kill(signal); -process.on("SIGINT", forward("SIGINT")); -process.on("SIGTERM", forward("SIGTERM")); +const gatsbyBin = resolveGatsbyBin(); + +if (require.main === module) { + const args = process.argv.slice(2); + console.info( + `[run-gatsby] ${totalGb.toFixed(1)} GB RAM / ${cores} cores -> ` + + `workers=${env.GATSBY_CPU_COUNT} sharp=${env.SHARP_CONCURRENCY} heap=${heapMb}MB`, + ); + + const isNodeEntrypoint = gatsbyBin.endsWith(".js"); + const child = spawn( + isNodeEntrypoint ? process.execPath : gatsbyBin, + isNodeEntrypoint ? [gatsbyBin, ...args] : args, + { + env, + stdio: "inherit", + shell: process.platform === "win32" && !isNodeEntrypoint, + }, + ); + + const forward = (signal) => () => child.kill(signal); + process.on("SIGINT", forward("SIGINT")); + process.on("SIGTERM", forward("SIGTERM")); + + child.on("exit", (code, signal) => { + if (signal) { + process.kill(process.pid, signal); + return; + } + process.exit(code ?? 0); + }); +} -child.on("exit", (code, signal) => { - if (signal) { - process.kill(process.pid, signal); - return; - } - process.exit(code ?? 0); -}); +module.exports = { resolveGatsbyBin }; diff --git a/scripts/run-gatsby.test.js b/scripts/run-gatsby.test.js new file mode 100644 index 00000000000000..90635a7524a8c1 --- /dev/null +++ b/scripts/run-gatsby.test.js @@ -0,0 +1,73 @@ +/* eslint-env node */ +"use strict"; + +/** + * Covers the Windows Gatsby binary fallback: gatsby.cmd -> gatsby.exe -> + * Gatsby's Node CLI when only gatsby.bunx metadata exists -> plain gatsby. + * Non-Windows behavior stays unchanged. + * + * Run with: node --test scripts/run-gatsby.test.js + */ +const { describe, it } = require("node:test"); +const assert = require("node:assert/strict"); +const path = require("node:path"); + +const { resolveGatsbyBin } = require("./run-gatsby"); + +const binDir = path.join("node_modules", ".bin"); + +const stubExists = (presentFiles) => { + const present = new Set(presentFiles); + return (file) => present.has(path.basename(file)); +}; + +describe("resolveGatsbyBin (win32)", () => { + it("prefers gatsby.cmd when all shims exist", () => { + const bin = resolveGatsbyBin({ + platform: "win32", + binDir, + existsSync: stubExists(["gatsby.cmd", "gatsby.exe", "gatsby.bunx"]), + }); + assert.equal(bin, path.join(binDir, "gatsby.cmd")); + }); + + it("falls back to gatsby.exe when gatsby.cmd is missing", () => { + const bin = resolveGatsbyBin({ + platform: "win32", + binDir, + existsSync: stubExists(["gatsby.exe", "gatsby.bunx"]), + }); + assert.equal(bin, path.join(binDir, "gatsby.exe")); + }); + + it("uses Gatsby's Node CLI when only the Bun metadata file is installed", () => { + const bin = resolveGatsbyBin({ + platform: "win32", + binDir, + existsSync: stubExists(["gatsby.bunx"]), + }); + assert.equal(bin, path.resolve(binDir, "..", "gatsby-cli", "cli.js")); + }); + + it("falls back to plain gatsby when no Windows shim exists", () => { + const bin = resolveGatsbyBin({ + platform: "win32", + binDir, + existsSync: stubExists([]), + }); + assert.equal(bin, path.join(binDir, "gatsby")); + }); +}); + +describe("resolveGatsbyBin (non-Windows)", () => { + for (const platform of ["linux", "darwin"]) { + it(`returns plain gatsby on ${platform} even when shims exist`, () => { + const bin = resolveGatsbyBin({ + platform, + binDir, + existsSync: stubExists(["gatsby.cmd", "gatsby.exe", "gatsby.bunx"]), + }); + assert.equal(bin, path.join(binDir, "gatsby")); + }); + } +});