System detection utilities for gathering CPU information and CI environment details to optimize Gatsby's build processes and provide appropriate defaults based on the runtime environment.
Determines the number of available CPU cores with environment variable overrides for build optimization.
/**
* Calculate CPU core count with environment variable support
* @param ignoreEnvVar - If true, ignores GATSBY_CPU_COUNT environment variable
* @returns Number of CPU cores (physical cores by default, or as configured)
* @throws Error if there's a problem counting CPU cores
*/
function cpuCoreCount(ignoreEnvVar?: boolean): number;Usage Examples:
import { cpuCoreCount } from "gatsby-core-utils";
// Get available CPU cores for parallel processing
const cores = cpuCoreCount();
console.log(`Available cores: ${cores}`);
// Use for determining worker process count
const workerCount = Math.max(1, cores - 1); // Leave one core free
const workers = new Array(workerCount).fill(null).map(() => createWorker());
// Force physical core count (ignore environment override)
const physicalCores = cpuCoreCount(true);
console.log(`Physical cores: ${physicalCores}`);Environment Variable Configuration:
The GATSBY_CPU_COUNT environment variable can override default behavior:
# Use logical cores (includes hyperthreading)
export GATSBY_CPU_COUNT=logical_cores
# Use physical cores only (default behavior)
export GATSBY_CPU_COUNT=physical_cores
# Set specific core count
export GATSBY_CPU_COUNT=4Internal Implementation Details:
The cpuCoreCount() function uses platform-specific detection methods internally:
lscpu command to count unique physical coressysctl hw.physicalcpu_max commandos.cpus().length if platform-specific detection failsDetects if the code is running in a Continuous Integration environment.
/**
* Determines if the current environment is a CI environment
* Checks for various CI platform indicators and environment variables
* @returns True if running in any recognized CI environment
*/
function isCI(): boolean;Usage Examples:
import { isCI } from "gatsby-core-utils";
// Adjust behavior for CI environments
if (isCI()) {
console.log("Running in CI - enabling optimizations");
// Disable interactive prompts
// Use different caching strategies
// Enable verbose logging
} else {
console.log("Running in development - enabling dev features");
// Enable file watching
// Show interactive prompts
// Use local caching
}
// Conditional feature flags
const enableAnalytics = !isCI(); // Disable analytics in CI
const useProgressBar = !isCI(); // Disable progress bars in CIIdentifies the specific CI platform being used.
/**
* Gets the name of the CI environment
* @returns CI platform name or null if not in CI
*/
function getCIName(): string | null;Usage Examples:
import { getCIName, isCI } from "gatsby-core-utils";
// Platform-specific optimizations
const ciName = getCIName();
if (ciName) {
console.log(`Running on ${ciName}`);
switch (ciName) {
case "GitHub Actions":
// GitHub Actions specific settings
process.env.FORCE_COLOR = "1"; // Enable colors
break;
case "Vercel Now":
// Vercel specific optimizations
console.log("Optimizing for Vercel deployment");
break;
case "CircleCI":
// CircleCI specific settings
console.log("Using CircleCI optimizations");
break;
}
}
// Error reporting with CI context
function reportError(error) {
const context = {
ci: isCI(),
platform: getCIName(),
// ... other context
};
errorReporter.report(error, context);
}Supported CI Platforms:
GITHUB_ACTIONS)CIRCLECI, CIRCLE_BRANCH)VERCEL_URL, NOW_REGION, NOW_BUILDER, VERCEL_BUILDER)CODESANDBOX_SSE)CI environment variable)CI_NAME environment variable)Detects the terminal program being used for optimal output formatting.
/**
* Detects the terminal program being used
* @returns Terminal program name or undefined if not detected
*/
function getTermProgram(): string | undefined;Usage Examples:
import { getTermProgram } from "gatsby-core-utils";
// Optimize output for specific terminals
const terminal = getTermProgram();
switch (terminal) {
case "vscode":
// VS Code integrated terminal optimizations
console.log("Detected VS Code terminal");
break;
case "Windows Terminal":
// Windows Terminal specific features
console.log("Using Windows Terminal features");
break;
case "iTerm.app":
// iTerm2 specific optimizations
console.log("iTerm2 detected - enabling enhanced features");
break;
}
// Conditional feature enabling
const supportsRichOutput = ["vscode", "Windows Terminal", "iTerm.app"].includes(terminal);Detection Sources:
TERM_PROGRAM environment variable (macOS, Linux)WT_SESSION environment variable (Windows Terminal)import { cpuCoreCount, isCI } from "gatsby-core-utils";
// Optimize parallel processing based on environment
function getBuildConcurrency() {
const cores = cpuCoreCount();
if (isCI()) {
// In CI, use more aggressive parallelization
return cores;
} else {
// In development, leave resources for other processes
return Math.max(1, cores - 1);
}
}
const concurrency = getBuildConcurrency();
console.log(`Using ${concurrency} parallel build processes`);import { cpuCoreCount, getCIName } from "gatsby-core-utils";
// Adjust memory limits based on system capabilities
function getMemoryLimits() {
const cores = cpuCoreCount();
const ciName = getCIName();
// Different CI platforms have different resource constraints
switch (ciName) {
case "GitHub Actions":
return { maxMemoryPerWorker: "1GB", maxWorkers: 2 };
case "Vercel Now":
return { maxMemoryPerWorker: "512MB", maxWorkers: 1 };
default:
return {
maxMemoryPerWorker: "2GB",
maxWorkers: Math.min(cores, 4)
};
}
}System information functions handle failures gracefully:
cpuCoreCount() falls back to 1 if detection fails and throws descriptive errorscpuCoreCount() falls back to logical core count if platform-specific detection failsisCI() and getCIName() return safe defaults (false/null) if detection failsgetTermProgram() returns undefined if terminal cannot be detectedThese functions are designed to provide sensible defaults rather than failing, ensuring Gatsby builds can proceed even when system detection encounters issues.