| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.spawn = exports.exec = void 0;
- var child_process_1 = require("child_process");
- var MAX_BUFFER = 10 * Math.pow(2, 20);
- function exec(command, stdout, stderr, stdin) {
- return new Promise(function (resolve, reject) {
- try {
- var stdoutbuf_1 = '';
- var stderrbuf_1 = '';
- // EXEC CHILD PROCESS
- var p = (0, child_process_1.exec)(command, { maxBuffer: MAX_BUFFER }, function (err, out) {
- if (stdoutbuf_1.length > 0 && typeof stdout === 'function')
- stdout(stdoutbuf_1);
- if (stderrbuf_1.length > 0 && typeof stderr === 'function')
- stderr(stderrbuf_1);
- if (err)
- return reject(err);
- resolve(out);
- });
- // 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', function (chunk) {
- stdoutbuf_1 += chunk;
- var i = -1;
- while ((i = stdoutbuf_1.indexOf('\n')) >= 0) {
- var line = stdoutbuf_1.substring(0, i);
- stdoutbuf_1 = stdoutbuf_1.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', function (chunk) {
- stderrbuf_1 += chunk;
- var i = -1;
- while ((i = stderrbuf_1.indexOf('\n')) >= 0) {
- var line = stderrbuf_1.substring(0, i);
- stderrbuf_1 = stderrbuf_1.substring(i + 1);
- if (typeof stderr === 'function') {
- stderr(line);
- }
- }
- });
- }
- else if (typeof stderr !== 'undefined') {
- p.stderr.pipe(stderr);
- }
- }
- catch (err) {
- reject(err);
- }
- });
- }
- exports.exec = exec;
- function spawn(command, args, stdout, stderr, stdin) {
- return new Promise(function (resolve, reject) {
- try {
- var stdoutbuf_2 = '';
- var stderrbuf_2 = '';
- var p = (0, child_process_1.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', function (chunk) {
- stdoutbuf_2 += chunk;
- var i = -1;
- while ((i = stdoutbuf_2.indexOf('\n')) >= 0) {
- var line = stdoutbuf_2.substring(0, i);
- stdoutbuf_2 = stdoutbuf_2.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', function (chunk) {
- stderrbuf_2 += chunk;
- var i = -1;
- while ((i = stderrbuf_2.indexOf('\n')) >= 0) {
- var line = stderrbuf_2.substring(0, i);
- stderrbuf_2 = stderrbuf_2.substring(i + 1);
- if (typeof stderr === 'function') {
- stderr(line);
- }
- }
- });
- }
- else if (typeof stderr !== 'undefined') {
- p.stderr.pipe(stderr);
- }
- p.on('close', function (code, sig) {
- if (!code)
- resolve();
- else
- reject();
- });
- p.on('error', reject);
- p.on('exit', function (code, sig) {
- if (!code)
- resolve();
- else
- reject();
- });
- }
- catch (err) {
- reject(err);
- }
- });
- }
- exports.spawn = spawn;
- //# sourceMappingURL=index.js.map
|