Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions node/mock-task.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Q = require('q');
import path = require('path');
import fs = require('fs');
import task = require('./task');
Expand Down Expand Up @@ -331,7 +330,7 @@ export function mv(source: string, dest: string, force: boolean, continueOnError
//-----------------------------------------------------
// Exec convenience wrapper
//-----------------------------------------------------
export function exec(tool: string, args: any, options?: trm.IExecOptions): Q.Promise<number> {
export function exec(tool: string, args: any, options?: trm.IExecOptions): Promise<number> {
var toolPath = which(tool, true);
var tr: trm.ToolRunner = this.tool(toolPath);
if (args) {
Expand Down
165 changes: 81 additions & 84 deletions node/mock-toolrunner.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Q = require('q');
import os = require('os');
import events = require('events');
import ma = require('./mock-answer');
Expand Down Expand Up @@ -268,107 +267,105 @@ export class ToolRunner extends events.EventEmitter {
* @deprecated use `execAsync` instead
* @returns a promise with return code.
*/
public exec(options?: IExecOptions): Q.Promise<number> {
var defer = Q.defer<number>();
public exec(options?: IExecOptions): Promise<number> {
return new Promise<number>((resolve, reject) => {
this._debug('exec tool: ' + this.toolPath);
this._debug('Arguments:');
this.args.forEach((arg) => {
this._debug(' ' + arg);
});

var success = true;
options = options || <IExecOptions>{};

var ops: IExecOptions = {
cwd: options.cwd || process.cwd(),
env: options.env || process.env,
silent: options.silent || false,
outStream: options.outStream || process.stdout,
errStream: options.errStream || process.stderr,
failOnStdErr: options.failOnStdErr || false,
ignoreReturnCode: options.ignoreReturnCode || false,
windowsVerbatimArguments: options.windowsVerbatimArguments
};

var argString = this.args.join(' ') || '';
var cmdString = this.toolPath;
if (argString) {
cmdString += (' ' + argString);
}

this._debug('exec tool: ' + this.toolPath);
this._debug('Arguments:');
this.args.forEach((arg) => {
this._debug(' ' + arg);
});
// Using split/join to replace the temp path
cmdString = this.ignoreTempPath(cmdString);

var success = true;
options = options || <IExecOptions>{};
if (!ops.silent) {
if(this.pipeOutputToTool) {
var pipeToolArgString = this.pipeOutputToTool.args.join(' ') || '';
var pipeToolCmdString = this.ignoreTempPath(this.pipeOutputToTool.toolPath);
if(pipeToolArgString) {
pipeToolCmdString += (' ' + pipeToolArgString);
}

cmdString += ' | ' + pipeToolCmdString;
}

var ops: IExecOptions = {
cwd: options.cwd || process.cwd(),
env: options.env || process.env,
silent: options.silent || false,
outStream: options.outStream || process.stdout,
errStream: options.errStream || process.stderr,
failOnStdErr: options.failOnStdErr || false,
ignoreReturnCode: options.ignoreReturnCode || false,
windowsVerbatimArguments: options.windowsVerbatimArguments
};
ops.outStream.write('[command]' + cmdString + os.EOL);
}

var argString = this.args.join(' ') || '';
var cmdString = this.toolPath;
if (argString) {
cmdString += (' ' + argString);
}
// TODO: filter process.env
var res = mock.getResponse('exec', cmdString, debug);
if (res.stdout) {
this.emit('stdout', res.stdout);
if (!ops.silent) {
ops.outStream.write(res.stdout + os.EOL);
}
const stdLineArray = res.stdout.split(os.EOL);
for (const line of stdLineArray.slice(0, -1)) {
this.emit('stdline', line);
}
if(stdLineArray.length > 0 && stdLineArray[stdLineArray.length - 1].length > 0) {
this.emit('stdline', stdLineArray[stdLineArray.length - 1]);
}
}

// Using split/join to replace the temp path
cmdString = this.ignoreTempPath(cmdString);
if (res.stderr) {
this.emit('stderr', res.stderr);

if (!ops.silent) {
if(this.pipeOutputToTool) {
var pipeToolArgString = this.pipeOutputToTool.args.join(' ') || '';
var pipeToolCmdString = this.ignoreTempPath(this.pipeOutputToTool.toolPath);
if(pipeToolArgString) {
pipeToolCmdString += (' ' + pipeToolArgString);
success = !ops.failOnStdErr;
if (!ops.silent) {
var s = ops.failOnStdErr ? ops.errStream : ops.outStream;
s.write(res.stderr + os.EOL);
}
const stdErrArray = res.stderr.split(os.EOL);
for (const line of stdErrArray.slice(0, -1)) {
this.emit('errline', line);
}
if (stdErrArray.length > 0 && stdErrArray[stdErrArray.length - 1].length > 0) {
this.emit('errline', stdErrArray[stdErrArray.length - 1]);
}

cmdString += ' | ' + pipeToolCmdString;
}

ops.outStream.write('[command]' + cmdString + os.EOL);
}

// TODO: filter process.env
var res = mock.getResponse('exec', cmdString, debug);
if (res.stdout) {
this.emit('stdout', res.stdout);
var code = res.code;

if (!ops.silent) {
ops.outStream.write(res.stdout + os.EOL);
}
const stdLineArray = res.stdout.split(os.EOL);
for (const line of stdLineArray.slice(0, -1)) {
this.emit('stdline', line);
}
if(stdLineArray.length > 0 && stdLineArray[stdLineArray.length - 1].length > 0) {
this.emit('stdline', stdLineArray[stdLineArray.length - 1]);
ops.outStream.write('rc:' + res.code + os.EOL);
}
}

if (res.stderr) {
this.emit('stderr', res.stderr);
if (code != 0 && !ops.ignoreReturnCode) {
success = false;
}

success = !ops.failOnStdErr;
if (!ops.silent) {
var s = ops.failOnStdErr ? ops.errStream : ops.outStream;
s.write(res.stderr + os.EOL);
ops.outStream.write('success:' + success + os.EOL);
}
const stdErrArray = res.stderr.split(os.EOL);
for (const line of stdErrArray.slice(0, -1)) {
this.emit('errline', line);
if (success) {
return resolve(code);
}
if (stdErrArray.length > 0 && stdErrArray[stdErrArray.length - 1].length > 0) {
this.emit('errline', stdErrArray[stdErrArray.length - 1]);
else {
return reject(new Error(this.toolPath + ' failed with return code: ' + code));
}
}


var code = res.code;

if (!ops.silent) {
ops.outStream.write('rc:' + res.code + os.EOL);
}

if (code != 0 && !ops.ignoreReturnCode) {
success = false;
}

if (!ops.silent) {
ops.outStream.write('success:' + success + os.EOL);
}
if (success) {
defer.resolve(code);
}
else {
defer.reject(new Error(this.toolPath + ' failed with return code: ' + code));
}

return <Q.Promise<number>>defer.promise;
});
}

//
Expand Down
11 changes: 0 additions & 11 deletions node/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"adm-zip": "^0.5.10",
"minimatch": "3.0.5",
"nodejs-file-downloader": "^4.11.1",
"q": "^1.5.1",
"semver": "^5.7.2",
"shelljs": "^0.8.5",
"uuid": "^3.0.1"
Expand All @@ -39,7 +38,6 @@
"@types/minimatch": "3.0.3",
"@types/mocha": "^9.1.1",
"@types/node": "^16.11.39",
"@types/q": "^1.5.4",
"@types/semver": "^7.3.4",
"@types/shelljs": "^0.8.8",
"mocha": "^9.2.2",
Expand Down
3 changes: 1 addition & 2 deletions node/task.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Q = require('q');
import shell = require('shelljs');
import childProcess = require('child_process');
import fs = require('fs');
Expand Down Expand Up @@ -1534,7 +1533,7 @@ export function execAsync(tool: string, args: any, options?: trm.IExecOptions):
* @param options optional exec options. See IExecOptions
* @returns number
*/
export function exec(tool: string, args: any, options?: trm.IExecOptions): Q.Promise<number> {
export function exec(tool: string, args: any, options?: trm.IExecOptions): Promise<number> {
let tr: trm.ToolRunner = this.tool(tool);
if (args) {
if (args instanceof Array) {
Expand Down
Loading