import * as fs from "fs" ; import * as path from "path" ; import * as url from "url" ; import * as tl from "azure-pipelines-task-lib/task" ; export function getTempPath ( ) { const tempNpmrcDir = tl . getVariable ( "Agent.BuildDirectory" ) || tl . getVariable ( "Agent.TempDirectory" ) ; const tempPath = path . join ( tempNpmrcDir , "npm" ) ; if ( tl . exist ( tempPath ) === false ) { tl . mkdirP ( tempPath ) ; } return tempPath ; } function copyFile ( src , dst ) { const content = fs . readFileSync ( src ) ; fs . writeFileSync ( dst , content ) ; } export function saveFile ( file ) { if ( file && tl . exist ( file ) ) { const tempPath = getTempPath ( ) ; const baseName = path . basename ( file ) ; const destination = path . join ( tempPath , baseName ) ; tl . debug ( tl . loc ( "SavingFile" , file ) ) ; copyFile ( file , destination ) ; } } export function saveFileWithName ( file , name , filePath ) { if ( file && tl . exist ( file ) ) { const destination = path . join ( filePath , name + ".npmrc" ) ; tl . debug ( tl . loc ( "SavingFile" , file ) ) ; copyFile ( file , destination ) ; } } export function restoreFile ( file ) { if ( file ) { const tempPath = getTempPath ( ) ; const baseName = path . basename ( file ) ; const source = path . join ( tempPath , baseName ) ; if ( tl . exist ( source ) ) { tl . debug ( tl . loc ( "RestoringFile" , file ) ) ; copyFile ( source , file ) ; tl . rmRF ( source ) ; } } } export function restoreFileWithName ( file , name , filePath ) { if ( file ) { const source = path . join ( filePath , name + ".npmrc" ) ; if ( tl . exist ( source ) ) { tl . debug ( tl . loc ( "RestoringFile" , file ) ) ; copyFile ( source , file ) ; tl . rmRF ( source ) ; } } } export function toNerfDart ( uri ) { var parsed = url . parse ( uri ) ; delete parsed . protocol ; delete parsed . auth ; delete parsed . query ; delete parsed . search ; delete parsed . hash ; return url . resolve ( url . format ( parsed ) , "." ) ; } export function getProjectAndFeedIdFromInputParam ( inputParam ) { const feedProject = tl . getInput ( inputParam ) ; var projectId = null ; var feedId = feedProject ; if ( feedProject && feedProject . includes ( "/" ) ) { const feedProjectParts = feedProject . split ( "/" ) ; projectId = feedProjectParts [ 0 ] || null ; feedId = feedProjectParts [ 1 ] ; } return { feedId : feedId , projectId : projectId } ; }