OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
import { spawn } from 'node:child_process';
export interface RunResult {
code: number;
stdout: string;
}
const MISSING_BINARY_CODE = 127;
// `inherit` streams straight to the terminal (up, logs); otherwise output is
// captured for the caller to inspect. A missing binary resolves like a failed
// command instead of throwing, so host checks can report it.
export const run = (
command: string[],
options: {
cwd?: string;
inherit?: boolean;
env?: Record<string, string>;
} = {}
): Promise<RunResult> =>
new Promise((resolve) => {
const [binary = '', ...args] = command;
const child = spawn(binary, args, {
cwd: options.cwd,
env: options.env ? { ...process.env, ...options.env } : process.env,
stdio: options.inherit ? 'inherit' : ['ignore', 'pipe', 'ignore'],
});
let stdout = '';
child.stdout?.on('data', (chunk) => {
stdout += chunk;
});
child.on('error', () => resolve({ code: MISSING_BINARY_CODE, stdout }));
child.on('close', (code) => resolve({ code: code ?? 1, stdout }));
});