Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .lintstagedrc.js
Original file line number Diff line number Diff line change
@@ -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(" ")}`,
],

Expand Down
76 changes: 47 additions & 29 deletions scripts/run-gatsby.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,34 +103,52 @@ 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, but pnpm/bun may only create gatsby.exe or
* gatsby.bunx. 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", "gatsby.bunx"]) {
const full = path.join(binDir, candidate);
if (existsSync(full)) return full;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
}
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 child = spawn(gatsbyBin, args, {
env,
stdio: "inherit",
shell: process.platform === "win32",
});

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 };
72 changes: 72 additions & 0 deletions scripts/run-gatsby.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* eslint-env node */
"use strict";

/**
* Covers the Windows Gatsby binary fallback: gatsby.cmd -> gatsby.exe ->
* gatsby.bunx -> 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("falls back to gatsby.bunx when only bun modules are installed", () => {
const bin = resolveGatsbyBin({
platform: "win32",
binDir,
existsSync: stubExists(["gatsby.bunx"]),
});
assert.equal(bin, path.join(binDir, "gatsby.bunx"));
});

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"));
});
}
});
Loading