Skip to content

Commit 22e9d33

Browse files
committed
Add option to pipe to file and another tool
1 parent 0811d35 commit 22e9d33

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

node/toolrunner.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import child = require('child_process');
77
import stream = require('stream');
88
import im = require('./internal');
99
import tcm = require('./taskcommand');
10+
import fs = require('fs');
1011

1112
/**
1213
* Interface for exec options
@@ -73,6 +74,7 @@ export class ToolRunner extends events.EventEmitter {
7374
private toolPath: string;
7475
private args: string[];
7576
private pipeOutputToTool: ToolRunner;
77+
private pipeOutputToFile: string;
7678

7779
private _debug(message) {
7880
this.emit('debug', message);
@@ -528,10 +530,12 @@ export class ToolRunner extends events.EventEmitter {
528530
/**
529531
* Pipe output of exec() to another tool
530532
* @param tool
533+
* @param file optional filename to additionally stream the output to.
531534
* @returns {ToolRunner}
532535
*/
533-
public pipeExecOutputToTool(tool: ToolRunner) : ToolRunner {
536+
public pipeExecOutputToTool(tool: ToolRunner, file?: string) : ToolRunner {
534537
this.pipeOutputToTool = tool;
538+
this.pipeOutputToFile = file;
535539
return this;
536540
}
537541

@@ -584,27 +588,48 @@ export class ToolRunner extends events.EventEmitter {
584588
this.pipeOutputToTool._getSpawnArgs(options),
585589
this.pipeOutputToTool._getSpawnOptions(options));
586590

591+
let fileStream: fs.WriteStream = this.pipeOutputToFile ? fs.createWriteStream(this.pipeOutputToFile) : null;
592+
if (fileStream) {
593+
fileStream.write(this._getCommandString(options) + os.EOL);
594+
}
595+
587596
//pipe stdout of first tool to stdin of second tool
588597
cpFirst.stdout.on('data', (data: Buffer) => {
589598
try {
599+
if (fileStream) {
600+
fileStream.write(data.toString());
601+
}
590602
cp.stdin.write(data);
591603
} catch (err) {
592604
this._debug('Failed to pipe output of ' + toolPathFirst + ' to ' + toolPath);
593605
this._debug(toolPath + ' might have exited due to errors prematurely. Verify the arguments passed are valid.');
594606
}
595607
});
596608
cpFirst.stderr.on('data', (data: Buffer) => {
609+
if (fileStream) {
610+
fileStream.write(data.toString());
611+
}
597612
successFirst = !options.failOnStdErr;
598613
if (!options.silent) {
599614
var s = options.failOnStdErr ? options.errStream : options.outStream;
600615
s.write(data);
601616
}
602617
});
603618
cpFirst.on('error', (err) => {
619+
if (fileStream) {
620+
fileStream.on('finish', () => {
621+
fileStream.close();
622+
});
623+
}
604624
cp.stdin.end();
605625
defer.reject(new Error(toolPathFirst + ' failed. ' + err.message));
606626
});
607627
cpFirst.on('close', (code, signal) => {
628+
if (fileStream) {
629+
fileStream.on('finish', () => {
630+
fileStream.close();
631+
});
632+
}
608633
if (code != 0 && !options.ignoreReturnCode) {
609634
successFirst = false;
610635
returnCodeFirst = code;

0 commit comments

Comments
 (0)