feat: initial commit

This commit is contained in:
Mauricio Siu
2024-04-28 23:57:52 -06:00
parent 8857a20344
commit be56ba046c
412 changed files with 60777 additions and 1 deletions

View File

@@ -0,0 +1,3 @@
import { exec } from "node:child_process";
import util from "node:util";
export const execAsync = util.promisify(exec);

View File

@@ -0,0 +1,58 @@
import {
type ChildProcess,
type SpawnOptions,
spawn,
} from "node:child_process";
import BufferList from "bl";
export const spawnAsync = (
command: string,
args?: string[] | undefined,
onData?: (data: string) => void, // Callback opcional para manejar datos en tiempo real
options?: SpawnOptions,
): Promise<BufferList> & { child: ChildProcess } => {
const child = spawn(command, args ?? [], options ?? {});
const stdout = child.stdout ? new BufferList() : new BufferList();
const stderr = child.stderr ? new BufferList() : new BufferList();
if (child.stdout) {
child.stdout.on("data", (data) => {
stdout.append(data);
if (onData) {
onData(data.toString());
}
});
}
if (child.stderr) {
child.stderr.on("data", (data) => {
stderr.append(data);
if (onData) {
onData(data.toString());
}
});
}
const promise = new Promise<BufferList>((resolve, reject) => {
child.on("error", reject);
child.on("close", (code) => {
if (code === 0) {
resolve(stdout);
} else {
const err = new Error(`child exited with code ${code}`) as Error & {
code: number;
stderr: BufferList;
stdout: BufferList;
};
err.code = code || -1;
err.stderr = stderr;
err.stdout = stdout;
reject(err);
}
});
}) as Promise<BufferList> & { child: ChildProcess };
promise.child = child;
return promise;
};