2022-04-07 11:44:23 +02:00
import * as cache from '@actions/cache' ;
import * as core from '@actions/core' ;
2022-02-21 16:21:14 +03:00
import * as exec from '@actions/exec' ;
2022-02-22 15:42:32 +03:00
import { supportedPackageManagers , PackageManagerInfo } from './package-managers' ;
2022-02-21 16:21:14 +03:00
export const getCommandOutput = async ( toolCommand : string ) = > {
let { stdout , stderr , exitCode } = await exec . getExecOutput (
toolCommand ,
undefined ,
{ ignoreReturnCode : true }
) ;
if ( exitCode ) {
stderr = ! stderr . trim ( )
? ` The ' ${ toolCommand } ' command failed with exit code: ${ exitCode } `
: stderr ;
throw new Error ( stderr ) ;
}
return stdout . trim ( ) ;
} ;
2022-02-22 14:55:30 +03:00
export const getPackageManagerInfo = async ( packageManager : string ) = > {
2022-02-22 15:11:41 +03:00
if ( ! supportedPackageManagers [ packageManager ] ) {
2022-02-22 14:55:30 +03:00
throw new Error (
` It's not possible to use ${ packageManager } , please, check correctness of the package manager name spelling. `
) ;
}
2022-02-28 15:30:28 +03:00
const obtainedPackageManager = supportedPackageManagers [ packageManager ] ;
2022-02-22 14:55:30 +03:00
2022-02-28 14:02:27 +03:00
return obtainedPackageManager ;
2022-02-21 16:21:14 +03:00
} ;
export const getCacheDirectoryPath = async (
2022-02-21 16:29:32 +03:00
packageManagerInfo : PackageManagerInfo
2022-02-21 16:21:14 +03:00
) = > {
2022-04-18 17:57:33 +02:00
let pathList : string [ ] = [ ] ;
for ( let command of packageManagerInfo . cacheFolderCommandList ) {
pathList . push ( await getCommandOutput ( command ) ) ;
}
2022-02-21 16:21:14 +03:00
2022-04-18 17:57:33 +02:00
for ( let path of pathList ) {
if ( ! path ) {
throw new Error ( ` Could not get cache folder paths. ` ) ;
}
2022-02-21 16:21:14 +03:00
}
2022-04-18 17:57:33 +02:00
return pathList ;
2022-02-21 16:21:14 +03:00
} ;
2022-04-07 11:44:23 +02:00
export function isGhes ( ) : boolean {
const ghUrl = new URL (
process . env [ 'GITHUB_SERVER_URL' ] || 'https://github.com'
) ;
return ghUrl . hostname . toUpperCase ( ) !== 'GITHUB.COM' ;
}
export function isCacheFeatureAvailable ( ) : boolean {
if ( ! cache . isFeatureAvailable ( ) ) {
if ( isGhes ( ) ) {
throw new Error (
'Cache action is only supported on GHES version >= 3.5. If you are on version >=3.5 Please check with GHES admin if Actions cache service is enabled or not.'
) ;
} else {
core . warning (
'The runner was not able to contact the cache service. Caching will be skipped'
) ;
}
return false ;
}
return true ;
}