mirror of
https://github.com/opentofu/setup-opentofu.git
synced 2025-12-06 16:15:57 +00:00
42 lines
854 B
JavaScript
42 lines
854 B
JavaScript
|
|
/**
|
||
|
|
* Copyright (c) HashiCorp, Inc.
|
||
|
|
* SPDX-License-Identifier: MPL-2.0
|
||
|
|
*/
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 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;
|