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 , dest ) { 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 , dest ) { tl . debug ( `downloading: ${ url }` ) ; const response = await downloadFrom ( url , location => tl . debug ( `following redirect to location: ${ location }` ) ) ; await saveResponseToFile ( response , dest ) ; } export function getTempPath ( ) { 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 , dest ) { return extract ( { file : source , cwd : dest } ) ; }