Skip to content

Commit

Permalink
Change the output listener
Browse files Browse the repository at this point in the history
  • Loading branch information
Maddog2050 committed Oct 26, 2021
1 parent 131aad6 commit 425cfdb
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 20 deletions.
73 changes: 63 additions & 10 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2872,14 +2872,58 @@ if (process.env.NODE_DEBUG && /\btunnel\b/.test(process.env.NODE_DEBUG)) {
exports.debug = debug; // for test


/***/ }),

/***/ 137:
/***/ ((module) => {

/**
* Acts as a listener for @actions/exec, by capturing STDOUT and STDERR
* streams, and exposing them via a contents attribute.
*
* @example
* // Instantiate a new listener
* const listener = new OutputListener();
* // Register listener against STDOUT stream
* await exec.exec('ls', ['-ltr'], {
* listeners: {
* stdout: listener.listener
* }
* });
* // Log out STDOUT contents
* console.log(listener.contents);
*/
class OutputListener {
constructor() {
this._buff = [];
}

get listener() {
const listen = function listen(data) {
this._buff.push(data);
};
return listen.bind(this);
}

get contents() {
return this._buff
.map(chunk => chunk.toString())
.join('');
}
}

module.exports = OutputListener;

/***/ }),

/***/ 239:
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {

const core = __nccwpck_require__(186);
const io = __nccwpck_require__(436);
const { getExecOutput } = __nccwpck_require__(514);
const { exec } = __nccwpck_require__(514);

const OutputListener = __nccwpck_require__(137);

const stringArgv = __nccwpck_require__(453);

Expand All @@ -2893,26 +2937,35 @@ async function run() {
command = args[0];
args = args.slice(1);

// Create listeners to receive output (in memory) as well
const stdout = new OutputListener();
const stderr = new OutputListener();
const listeners = {
stdout: stdout.listener,
stderr: stderr.listener
};

const options = {
listeners,
ignoreReturnCode: true
};

// Check that the command exists
io.which(command, true);

const output = await getExecOutput(command, args, options);
const exitCode = await exec(command, args, options);

core.debug(`Program exited with code ${output.exitCode}.`);
core.debug(`stdout: ${output.stdout}`);
core.debug(`stderr: ${output.stderr}`);
core.debug(`exitcode: ${output.exitCode}`);
core.debug(`Program exited with code ${exitCode}.`);
core.debug(`stdout: ${stdout.contents}`);
core.debug(`stderr: ${stderr.contents}`);
core.debug(`exitcode: ${exitCode}`);

core.setOutput('stdout', output.stdout);
core.setOutput('stderr', output.stderr);
core.setOutput('exitcode', output.exitCode);
core.setOutput('stdout', stdout.contents);
core.setOutput('stderr', stderr.contents);
core.setOutput('exitcode', exitCode);

if (output.exitCode !== 0) {
core.setFailed(`Program exited with code ${output.exitCode}.`);
core.setFailed(`Program exited with code ${exitCode}.`);
}
}

Expand Down
36 changes: 36 additions & 0 deletions src/lib/output-listener.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Acts as a listener for @actions/exec, by capturing STDOUT and STDERR
* streams, and exposing them via a contents attribute.
*
* @example
* // Instantiate a new listener
* const listener = new OutputListener();
* // Register listener against STDOUT stream
* await exec.exec('ls', ['-ltr'], {
* listeners: {
* stdout: listener.listener
* }
* });
* // Log out STDOUT contents
* console.log(listener.contents);
*/
class OutputListener {
constructor() {
this._buff = [];
}

get listener() {
const listen = function listen(data) {
this._buff.push(data);
};
return listen.bind(this);
}

get contents() {
return this._buff
.map(chunk => chunk.toString())
.join('');
}
}

module.exports = OutputListener;
31 changes: 21 additions & 10 deletions src/run-wrapper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const core = require('@actions/core');
const io = require('@actions/io');
const { getExecOutput } = require('@actions/exec');
const { exec } = require('@actions/exec');

const OutputListener = require('./lib/output-listener');

const stringArgv = require('string-argv');

Expand All @@ -14,26 +16,35 @@ async function run() {
command = args[0];
args = args.slice(1);

// Create listeners to receive output (in memory) as well
const stdout = new OutputListener();
const stderr = new OutputListener();
const listeners = {
stdout: stdout.listener,
stderr: stderr.listener
};

const options = {
listeners,
ignoreReturnCode: true
};

// Check that the command exists
io.which(command, true);

const output = await getExecOutput(command, args, options);
const exitCode = await exec(command, args, options);

core.debug(`Program exited with code ${output.exitCode}.`);
core.debug(`stdout: ${output.stdout}`);
core.debug(`stderr: ${output.stderr}`);
core.debug(`exitcode: ${output.exitCode}`);
core.debug(`Program exited with code ${exitCode}.`);
core.debug(`stdout: ${stdout.contents}`);
core.debug(`stderr: ${stderr.contents}`);
core.debug(`exitcode: ${exitCode}`);

core.setOutput('stdout', output.stdout);
core.setOutput('stderr', output.stderr);
core.setOutput('exitcode', output.exitCode);
core.setOutput('stdout', stdout.contents);
core.setOutput('stderr', stderr.contents);
core.setOutput('exitcode', exitCode);

if (output.exitCode !== 0) {
core.setFailed(`Program exited with code ${output.exitCode}.`);
core.setFailed(`Program exited with code ${exitCode}.`);
}
}

Expand Down

0 comments on commit 425cfdb

Please sign in to comment.