2019-10-30 14:48:49 -04:00
import * as core from "@actions/core" ;
import * as io from "@actions/io" ;
2019-11-06 13:41:45 -05:00
import * as fs from "fs" ;
2019-10-30 14:48:49 -04:00
import * as os from "os" ;
import * as path from "path" ;
2020-03-18 09:43:56 -04:00
import * as util from "util" ;
2019-10-30 14:48:49 -04:00
import * as uuidV4 from "uuid/v4" ;
2019-11-13 10:54:39 -05:00
import { Events , Outputs , State } from "../constants" ;
2019-10-30 14:48:49 -04:00
import { ArtifactCacheEntry } from "../contracts" ;
// From https://github.com/actions/toolkit/blob/master/packages/tool-cache/src/tool-cache.ts#L23
export async function createTempDirectory ( ) : Promise < string > {
const IS_WINDOWS = process . platform === "win32" ;
let tempDirectory : string = process . env [ "RUNNER_TEMP" ] || "" ;
if ( ! tempDirectory ) {
let baseLocation : string ;
if ( IS_WINDOWS ) {
// On Windows use the USERPROFILE env variable
baseLocation = process . env [ "USERPROFILE" ] || "C:\\" ;
} else {
if ( process . platform === "darwin" ) {
baseLocation = "/Users" ;
} else {
baseLocation = "/home" ;
}
}
tempDirectory = path . join ( baseLocation , "actions" , "temp" ) ;
}
const dest = path . join ( tempDirectory , uuidV4 . default ( ) ) ;
await io . mkdirP ( dest ) ;
return dest ;
}
2019-11-06 13:41:45 -05:00
export function getArchiveFileSize ( path : string ) : number {
return fs . statSync ( path ) . size ;
}
2019-10-30 14:48:49 -04:00
export function isExactKeyMatch (
key : string ,
cacheResult? : ArtifactCacheEntry
) : boolean {
return ! ! (
cacheResult &&
cacheResult . cacheKey &&
cacheResult . cacheKey . localeCompare ( key , undefined , {
sensitivity : "accent"
} ) === 0
) ;
}
2019-11-12 16:48:02 -05:00
export function setCacheState ( state : ArtifactCacheEntry ) : void {
core . saveState ( State . CacheResult , JSON . stringify ( state ) ) ;
}
export function setCacheHitOutput ( isCacheHit : boolean ) : void {
core . setOutput ( Outputs . CacheHit , isCacheHit . toString ( ) ) ;
}
2019-10-30 14:48:49 -04:00
export function setOutputAndState (
key : string ,
cacheResult? : ArtifactCacheEntry
2019-11-12 16:48:02 -05:00
) : void {
2019-10-30 14:48:49 -04:00
setCacheHitOutput ( isExactKeyMatch ( key , cacheResult ) ) ;
// Store the cache result if it exists
cacheResult && setCacheState ( cacheResult ) ;
}
export function getCacheState ( ) : ArtifactCacheEntry | undefined {
const stateData = core . getState ( State . CacheResult ) ;
core . debug ( ` State: ${ stateData } ` ) ;
2019-11-13 16:13:00 -05:00
if ( stateData ) {
return JSON . parse ( stateData ) as ArtifactCacheEntry ;
}
return undefined ;
2019-10-30 14:48:49 -04:00
}
2019-11-21 14:37:54 -05:00
export function logWarning ( message : string ) : void {
const warningPrefix = "[warning]" ;
core . info ( ` ${ warningPrefix } ${ message } ` ) ;
}
2019-10-30 14:48:49 -04:00
export function resolvePath ( filePath : string ) : string {
if ( filePath [ 0 ] === "~" ) {
const home = os . homedir ( ) ;
if ( ! home ) {
2019-11-01 10:35:38 -04:00
throw new Error ( "Unable to resolve `~` to HOME" ) ;
2019-10-30 14:48:49 -04:00
}
return path . join ( home , filePath . slice ( 1 ) ) ;
}
return path . resolve ( filePath ) ;
}
2019-11-13 10:54:39 -05:00
export function getSupportedEvents ( ) : string [ ] {
return [ Events . Push , Events . PullRequest ] ;
}
// Currently the cache token is only authorized for push and pull_request events
// All other events will fail when reading and saving the cache
// See GitHub Context https://help.github.com/actions/automating-your-workflow-with-github-actions/contexts-and-expression-syntax-for-github-actions#github-context
export function isValidEvent ( ) : boolean {
const githubEvent = process . env [ Events . Key ] || "" ;
return getSupportedEvents ( ) . includes ( githubEvent ) ;
}
2020-03-18 09:43:56 -04:00
export function unlinkFile ( path : fs.PathLike ) : Promise < void > {
return util . promisify ( fs . unlink ) ( path ) ;
}