Skip to content

Commit

Permalink
use exec instead of spawn
Browse files Browse the repository at this point in the history
Signed-off-by: Travis Glenn Hansen <travisghansen@yahoo.com>
  • Loading branch information
travisghansen committed Jul 21, 2023
1 parent 6ad5d1c commit 0366684
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 36 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# v0.4.3

Released 2023-07-21

- use `exec` instead of `spawn`

# v0.4.2

Released 2023-07-21
Expand Down
104 changes: 68 additions & 36 deletions lib/ip.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ class IpCommand {

if (!options.executor) {
options.executor = {
spawn: cp.spawn
spawn: cp.spawn,
exec: cp.exec
};
}
}
Expand Down Expand Up @@ -211,45 +212,76 @@ class IpCommand {
'executing ip command: %s',
`${command} ${args.join(' ')}`
);
const child = ip.options.executor.spawn(command, args, options);

return new Promise((resolve, reject) => {
child.stdout.on('data', function (data) {
stdout = stdout + data;
});

child.stderr.on('data', function (data) {
stderr = stderr + data;
});

child.on('close', function (code) {
const result = {
code,
stdout,
stderr,
timeout: false,
command: `${command} ${args.join(' ')}`
};

ip.options.logger.debug('ip command result:', result);
// timeout scenario
if (code === null) {
result.timeout = true;
reject(result);
}
const use_spawn = false;
if (use_spawn) {
const child = ip.options.executor.spawn(command, args, options);
child.stdout.on('data', function (data) {
stdout = stdout + data;
});

child.stderr.on('data', function (data) {
stderr = stderr + data;
});

child.on('close', function (code) {
const result = {
code,
stdout,
stderr,
timeout: false,
command: `${command} ${args.join(' ')}`
};

ip.options.logger.debug('ip command result:', result);
// timeout scenario
if (code === null) {
result.timeout = true;
reject(result);
}

try {
result.parsed = JSON.parse(result.stdout);
} catch (e) {
// move along
}
try {
result.parsed = JSON.parse(result.stdout);
} catch (e) {
// move along
}

if (code) {
reject(result);
} else {
resolve(result);
}
});
if (code) {
reject(result);
} else {
resolve(result);
}
});
} else {
ip.options.executor.exec(
`${command} ${args.join(' ')}`,
options,
(error, stdout, stderr) => {
const result = {
stdout,
stderr,
timeout: false,
command: `${command} ${args.join(' ')}`,
error
};

ip.options.logger.debug('ip command result:', result);

if (error) {
reject(result);
}

try {
result.parsed = JSON.parse(result.stdout);
} catch (e) {
// move along
}

resolve(result);
}
);
}
});
}
}
Expand Down

0 comments on commit 0366684

Please sign in to comment.