Core process execution and spawning functionality for running external commands with enhanced logging, error handling, and cross-platform support. Includes utilities for password removal, environment management, and retry mechanisms.
Executes external commands with comprehensive logging and error handling.
/**
* Execute a command with enhanced logging and error handling
* @param file - The executable file path
* @param args - Command arguments array
* @param options - Execution options (working directory, environment, etc.)
* @param isLogOutIfDebug - Whether to log output when debug is enabled
* @returns Promise resolving to command output
*/
function exec(
file: string,
args?: Array<string> | null,
options?: ExecFileOptions,
isLogOutIfDebug?: boolean
): Promise<string>;Usage Examples:
import { exec } from "builder-util";
// Basic command execution
const result = await exec("ls", ["-la"]);
console.log(result);
// With options
const output = await exec("npm", ["install"], {
cwd: "/project/path",
env: { NODE_ENV: "production" }
});
// With custom environment
const buildOutput = await exec("npm", ["run", "build"], {
cwd: process.cwd(),
env: { ...process.env, FORCE_COLOR: "1" }
});Creates child processes with cross-platform support and configurable stdio handling.
/**
* Spawn a child process with promise-based completion
* @param command - Command to execute
* @param args - Arguments for the command
* @param options - Spawn options
* @param extraOptions - Additional builder-util specific options
* @returns Promise resolving when process completes
*/
function spawn(
command: string,
args?: Array<string> | null,
options?: SpawnOptions,
extraOptions?: ExtraSpawnOptions
): Promise<any>;
/**
* Directly spawn a child process (non-promise based)
* @param command - Command to execute
* @param args - Arguments for the command
* @param options - Spawn options
* @param extraOptions - Additional options
* @returns ChildProcess instance
*/
function doSpawn(
command: string,
args: Array<string>,
options?: SpawnOptions,
extraOptions?: ExtraSpawnOptions
): ChildProcess;Usage Examples:
import { spawn, doSpawn } from "builder-util";
// Promise-based spawning
await spawn("git", ["clone", "https://github.com/user/repo.git"]);
// Direct spawning for custom handling
const childProcess = doSpawn("npm", ["run", "watch"], {
stdio: "inherit"
});
childProcess.on("exit", (code) => {
console.log(`Process exited with code ${code}`);
});Spawns a process and writes data to its stdin.
/**
* Spawn a process and write data to its stdin
* @param command - Command to execute
* @param args - Command arguments
* @param data - Data to write to stdin
* @param options - Spawn options
* @returns Promise resolving when process completes
*/
function spawnAndWrite(
command: string,
args: Array<string>,
data: string,
options?: SpawnOptions
): Promise<any>;Usage Examples:
import { spawnAndWrite } from "builder-util";
// Write configuration to a process
await spawnAndWrite("app-builder", ["configure"], JSON.stringify({
platform: "win32",
arch: "x64"
}));
// Send input data to command
await spawnAndWrite("openssl", ["enc", "-aes-256-cbc"], secretData);Executes the app-builder utility with environment setup and retry support.
/**
* Execute app-builder with enhanced environment and retry support
* @param args - Arguments for app-builder
* @param childProcessConsumer - Optional function to handle child process
* @param extraOptions - Additional spawn options
* @param maxRetries - Maximum retry attempts (0 for no retries)
* @returns Promise resolving to command output
*/
function executeAppBuilder(
args: Array<string>,
childProcessConsumer?: (childProcess: ChildProcess) => void,
extraOptions?: SpawnOptions,
maxRetries?: number
): Promise<string>;Usage Examples:
import { executeAppBuilder } from "builder-util";
// Basic app-builder execution
const result = await executeAppBuilder(["pack", "--platform", "win32"]);
// With retry support
const output = await executeAppBuilder(
["build", "--config", "config.json"],
undefined,
{ cwd: "/build/path" },
3 // Retry up to 3 times
);
// With process consumer for custom handling
await executeAppBuilder(
["compress", "input.tar"],
(childProcess) => {
childProcess.stdout?.on("data", (data) => {
console.log("Progress:", data.toString());
});
}
);Removes sensitive information from command strings for safe logging.
/**
* Remove passwords and sensitive data from command strings
* @param input - Input string potentially containing sensitive data
* @returns String with sensitive data replaced by SHA256 hashes
*/
function removePassword(input: string): string;Usage Examples:
import { removePassword } from "builder-util";
// Clean command string for logging
const command = "7z a archive.7z files/ -p secretpassword";
const cleanCommand = removePassword(command);
// Result: "7z a archive.7z files/ -p <hash> (sha256 hash)"
// Clean complex commands
const complex = "app --accessKey AKIAIOSFODNN7EXAMPLE --secretKey wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY";
const cleaned = removePassword(complex);interface ExtraSpawnOptions {
isPipeInput?: boolean;
}
class ExecError extends Error {
readonly exitCode: number;
alreadyLogged: boolean;
static readonly code = "ERR_ELECTRON_BUILDER_CANNOT_EXECUTE";
constructor(
command: string,
exitCode: number,
out: string,
errorOut: string,
code?: string
);
}Process execution functions throw ExecError instances when commands fail:
import { exec, ExecError } from "builder-util";
try {
await exec("invalid-command");
} catch (error) {
if (error instanceof ExecError) {
console.log("Command failed with exit code:", error.exitCode);
console.log("Error output:", error.message);
}
}