-
Notifications
You must be signed in to change notification settings - Fork 18
/
util.ts
48 lines (40 loc) · 1.22 KB
/
util.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
import fs = require("fs");
import q = require("q");
import * as tl from "azure-pipelines-task-lib/task";
import * as path from "path";
import { IncomingMessage } from "http";
import { extract } from "tar";
import { downloadFrom } from "./download";
function saveResponseToFile(
response: IncomingMessage,
dest: string
): PromiseLike<void> {
const deferal = q.defer<void>();
const file = fs.createWriteStream(dest);
response.pipe(file);
file.on("finish", () => {
deferal.resolve();
});
return deferal.promise;
}
export async function downloadFile(url: string, dest: string): Promise<void> {
tl.debug(`downloading: ${url}`);
const response = await downloadFrom(url, location =>
tl.debug(`following redirect to location: ${location}`)
);
await saveResponseToFile(response, dest);
}
export function getTempPath(): string {
let tempNpmrcDir =
tl.getVariable("Agent.BuildDirectory") ||
tl.getVariable("Agent.ReleaseDirectory") ||
process.cwd();
let tempPath = path.join(tempNpmrcDir, "yarn");
if (tl.exist(tempPath) === false) {
tl.mkdirP(tempPath);
}
return tempPath;
}
export function detar(source: string, dest: string): PromiseLike<void> {
return extract({ file: source, cwd: dest });
}