-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathcmdline.ts
73 lines (63 loc) · 2.48 KB
/
cmdline.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import fs = require('fs');
import path = require('path');
import os = require('os');
import tl = require('vsts-task-lib/task');
import tr = require('vsts-task-lib/toolrunner');
var uuidV4 = require('uuid/v4');
async function run() {
try {
tl.setResourcePath(path.join(__dirname, 'task.json'));
// Get inputs.
let failOnStderr = tl.getBoolInput('failOnStderr', false);
let script: string = tl.getInput('script', false) || '';
let workingDirectory = tl.getPathInput('workingDirectory', /*required*/ true, /*check*/ true);
// Write the script to disk.
console.log(tl.loc('GeneratingScript'));
tl.assertAgent('2.115.0');
let tempDirectory = tl.getVariable('agent.tempDirectory');
tl.checkPath(tempDirectory, `${tempDirectory} (agent.tempDirectory)`);
let filePath = path.join(tempDirectory, uuidV4() + '.sh');
await fs.writeFileSync(
filePath,
script, // Don't add a BOM. It causes the script to fail on some operating systems (e.g. on Ubuntu 14).
{ encoding: 'utf8' });
// Print one-liner scripts.
if (script.indexOf('\n') < 0 && script.toUpperCase().indexOf('##VSO[') < 0) {
console.log(tl.loc('ScriptContents'));
console.log(script);
}
// Create the tool runner.
let bash = tl.tool(tl.which('bash', true))
.arg('--noprofile')
.arg(`--norc`)
.arg(filePath);
let options = <tr.IExecOptions>{
cwd: workingDirectory,
failOnStdErr: false,
errStream: process.stdout, // Direct all output to STDOUT, otherwise the output may appear out
outStream: process.stdout, // of order since Node buffers it's own STDOUT but not STDERR.
ignoreReturnCode: true
};
// Listen for stderr.
let stderrFailure = false;
if (failOnStderr) {
bash.on('stderr', (data) => {
stderrFailure = true;
});
}
// Run bash.
let exitCode: number = await bash.exec(options);
// Fail on exit code.
if (exitCode !== 0) {
tl.setResult(tl.TaskResult.Failed, tl.loc('JS_ExitCode', exitCode));
}
// Fail on stderr.
if (stderrFailure) {
tl.setResult(tl.TaskResult.Failed, tl.loc('JS_Stderr'));
}
}
catch (err) {
tl.setResult(tl.TaskResult.Failed, err.message || 'run() failed');
}
}
run();