| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- const child_process = require('child_process');
- let execCommandAsync = function (options) {
- if (!options || !options.command || !options.command.trim()) {
- return;
- }
- let finalArguments = [];
- if (options.args) {
- const argsArr = options.args.split('\n');
- for (let i = 0; i < argsArr.length; i++) {
- let arg = argsArr[i];
- if (!arg) {
- continue;
- }
- arg = arg.replace('\r', '').trim();
- if (arg) {
- finalArguments.push(arg);
- }
- }
- }
- try {
- const child = child_process.spawn(options.command, finalArguments, {
- encoding: 'utf8',
- detached: !!options.detached
- });
- if (options.onoutput) {
- child.stdout.on('data', (data) => {
- options.onoutput({
- source: 'stdout',
- content: data.toString(),
- count: 1
- });
- });
- }
- if (options.onoutput) {
- child.stderr.on('data', (data) => {
- options.onoutput({
- source: 'stderr',
- content: data.toString(),
- count: 1
- });
- });
- }
- if (options.onoutput) {
- child.on('close', (code) => {
- options.onoutput({
- source: 'close',
- content: code,
- count: 1
- });
- });
- }
- child.on('error', (error) => {
- if (options.onerror) {
- options.onerror({
- type: 'event',
- error: error
- });
- }
- });
- } catch (ex) {
- if (options.onerror) {
- options.onerror({
- type: 'exception',
- error: ex
- });
- }
- }
- };
- module.exports = {
- execCommandAsync: execCommandAsync
- }
|