mirror of
https://github.com/nicotsx/ironmount.git
synced 2025-12-10 12:10:51 +01:00
add mysql, mariadb, postgresql, sqlite volumes support
This commit is contained in:
@@ -5,6 +5,8 @@ interface Params {
|
||||
args: string[];
|
||||
env?: NodeJS.ProcessEnv;
|
||||
signal?: AbortSignal;
|
||||
stdin?: string;
|
||||
timeout?: number;
|
||||
onStdout?: (data: string) => void;
|
||||
onStderr?: (error: string) => void;
|
||||
onError?: (error: Error) => Promise<void> | void;
|
||||
@@ -19,17 +21,32 @@ type SpawnResult = {
|
||||
};
|
||||
|
||||
export const safeSpawn = (params: Params) => {
|
||||
const { command, args, env = {}, signal, ...callbacks } = params;
|
||||
const { command, args, env = {}, signal, stdin, timeout, ...callbacks } = params;
|
||||
|
||||
return new Promise<SpawnResult>((resolve) => {
|
||||
return new Promise<SpawnResult>((resolve, reject) => {
|
||||
let stdoutData = "";
|
||||
let stderrData = "";
|
||||
let timeoutId: NodeJS.Timeout | undefined;
|
||||
|
||||
const child = spawn(command, args, {
|
||||
env: { ...process.env, ...env },
|
||||
signal: signal,
|
||||
});
|
||||
|
||||
// Handle timeout if specified
|
||||
if (timeout) {
|
||||
timeoutId = setTimeout(() => {
|
||||
child.kill("SIGTERM");
|
||||
reject(new Error(`Command timed out after ${timeout}ms`));
|
||||
}, timeout);
|
||||
}
|
||||
|
||||
// Write stdin if provided
|
||||
if (stdin && child.stdin) {
|
||||
child.stdin.write(stdin);
|
||||
child.stdin.end();
|
||||
}
|
||||
|
||||
child.stdout.on("data", (data) => {
|
||||
if (callbacks.onStdout) {
|
||||
callbacks.onStdout(data.toString());
|
||||
@@ -47,6 +64,7 @@ export const safeSpawn = (params: Params) => {
|
||||
});
|
||||
|
||||
child.on("error", async (error) => {
|
||||
if (timeoutId) clearTimeout(timeoutId);
|
||||
if (callbacks.onError) {
|
||||
await callbacks.onError(error);
|
||||
}
|
||||
@@ -54,14 +72,11 @@ export const safeSpawn = (params: Params) => {
|
||||
await callbacks.finally();
|
||||
}
|
||||
|
||||
resolve({
|
||||
exitCode: -1,
|
||||
stdout: stdoutData,
|
||||
stderr: stderrData,
|
||||
});
|
||||
reject(error);
|
||||
});
|
||||
|
||||
child.on("close", async (code) => {
|
||||
if (timeoutId) clearTimeout(timeoutId);
|
||||
if (callbacks.onClose) {
|
||||
await callbacks.onClose(code);
|
||||
}
|
||||
@@ -69,11 +84,15 @@ export const safeSpawn = (params: Params) => {
|
||||
await callbacks.finally();
|
||||
}
|
||||
|
||||
resolve({
|
||||
exitCode: code === null ? -1 : code,
|
||||
stdout: stdoutData,
|
||||
stderr: stderrData,
|
||||
});
|
||||
if (code !== 0 && code !== null) {
|
||||
reject(new Error(`Command failed with exit code ${code}: ${stderrData || stdoutData}`));
|
||||
} else {
|
||||
resolve({
|
||||
exitCode: code === null ? -1 : code,
|
||||
stdout: stdoutData,
|
||||
stderr: stderrData,
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user