| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- import { exec as shell_exec, spawn as shell_spawn } from 'child_process';
- const MAX_BUFFER = 10 * Math.pow(2, 20);
- export function exec(
- command: string,
- stdout?: ((...args: any[]) => void) | NodeJS.WriteStream,
- stderr?: ((...args: any[]) => void) | NodeJS.WriteStream,
- stdin?: NodeJS.ReadableStream | string
- ) {
- return new Promise<string>((resolve, reject) => {
- try {
- let stdoutbuf = '';
- let stderrbuf = '';
- // EXEC CHILD PROCESS
- const p = shell_exec(command, { maxBuffer: MAX_BUFFER }, (err, out) => {
- if (stdoutbuf.length > 0 && typeof stdout === 'function') stdout(stdoutbuf);
- if (stderrbuf.length > 0 && typeof stderr === 'function') stderr(stderrbuf);
- if (err) return reject(err);
- resolve(out);
- });
- // PIPE STDIN (?)
- if (p.stdin) {
- if (typeof stdin === 'string') {
- p.stdin.setDefaultEncoding('utf-8');
- if (!stdin.endsWith('\n')) stdin += '\n';
- p.stdin.write(stdin);
- p.stdin.end();
- } else if (typeof stdin !== 'undefined') {
- stdin.pipe(p.stdin);
- }
- }
- // PIPE STDOUT
- if (p.stdout) {
- if (typeof stdout === 'function') {
- p.stdout.on('data', (chunk) => {
- stdoutbuf += chunk;
- let i = -1;
- while ((i = stdoutbuf.indexOf('\n')) >= 0) {
- const line = stdoutbuf.substring(0, i);
- stdoutbuf = stdoutbuf.substring(i + 1);
- if (typeof stdout === 'function') {
- stdout(line);
- }
- }
- });
- } else if (typeof stdout !== 'undefined') {
- p.stdout.pipe(stdout);
- }
- }
- // PIPE STDERR
- if (p.stderr) {
- if (typeof stderr === 'function') {
- p.stderr.on('data', (chunk) => {
- stderrbuf += chunk;
- let i = -1;
- while ((i = stderrbuf.indexOf('\n')) >= 0) {
- const line = stderrbuf.substring(0, i);
- stderrbuf = stderrbuf.substring(i + 1);
- if (typeof stderr === 'function') {
- stderr(line);
- }
- }
- });
- } else if (typeof stderr !== 'undefined') {
- p.stderr.pipe(stderr);
- }
- }
- } catch (err) {
- reject(err);
- }
- });
- }
- export function spawn(
- command: string,
- args: string[],
- stdout?: ((...args: any[]) => void) | NodeJS.WriteStream,
- stderr?: ((...args: any[]) => void) | NodeJS.WriteStream,
- stdin?: NodeJS.ReadableStream | string
- ) {
- return new Promise<void>((resolve, reject) => {
- try {
- let stdoutbuf = '';
- let stderrbuf = '';
- const p = shell_spawn(command, args);
- // PIPE STDIN (?)
- if (typeof stdin === 'string') {
- p.stdin.setDefaultEncoding('utf-8');
- if (!stdin.endsWith('\n')) stdin += '\n';
- p.stdin.write(stdin);
- p.stdin.end();
- } else if (typeof stdin !== 'undefined') {
- stdin.pipe(p.stdin);
- }
- // PIPE STDOUT
- if (typeof stdout === 'function') {
- p.stdout.on('data', (chunk) => {
- stdoutbuf += chunk;
- let i = -1;
- while ((i = stdoutbuf.indexOf('\n')) >= 0) {
- const line = stdoutbuf.substring(0, i);
- stdoutbuf = stdoutbuf.substring(i + 1);
- if (typeof stdout === 'function') {
- stdout(line);
- }
- }
- });
- } else if (typeof stdout !== 'undefined') {
- p.stdout.pipe(stdout);
- }
- // PIPE STDERR
- if (typeof stderr === 'function') {
- p.stderr.on('data', (chunk) => {
- stderrbuf += chunk;
- let i = -1;
- while ((i = stderrbuf.indexOf('\n')) >= 0) {
- const line = stderrbuf.substring(0, i);
- stderrbuf = stderrbuf.substring(i + 1);
- if (typeof stderr === 'function') {
- stderr(line);
- }
- }
- });
- } else if (typeof stderr !== 'undefined') {
- p.stderr.pipe(stderr);
- }
- p.on('close', (code, sig) => {
- if (!code) resolve();
- else reject();
- });
- p.on('error', reject);
- p.on('exit', (code, sig) => {
- if (!code) resolve();
- else reject();
- });
- } catch (err) {
- reject(err);
- }
- });
- }
|