2022-05-25 12:07:29 +02:00
import * as cache from '@actions/cache' ;
import * as core from '@actions/core' ;
import * as exec from '@actions/exec' ;
import { supportedPackageManagers , PackageManagerInfo } from './package-managers' ;
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 ();
};
export const getPackageManagerInfo = async ( packageManager : string ) => {
if ( ! supportedPackageManagers [ packageManager ]) {
throw new Error (
`It's not possible to use ${ packageManager } , please, check correctness of the package manager name spelling.`
);
}
const obtainedPackageManager = supportedPackageManagers [ packageManager ];
return obtainedPackageManager ;
};
export const getCacheDirectoryPath = async (
packageManagerInfo : PackageManagerInfo
) => {
2022-12-19 11:22:17 +01:00
const pathList = await Promise . all (
2023-03-08 10:45:16 +02:00
packageManagerInfo . cacheFolderCommandList . map ( async command =>
2022-05-25 12:07:29 +02:00
getCommandOutput ( command )
)
);
2022-12-19 11:22:17 +01:00
const cachePaths = pathList . filter ( item => item );
2022-05-25 12:07:29 +02:00
2022-12-19 11:22:17 +01:00
if ( ! cachePaths . length ) {
2022-05-25 12:07:29 +02:00
throw new Error ( `Could not get cache folder paths.` );
}
2022-12-19 11:22:17 +01:00
return cachePaths ;
2022-05-25 12:07:29 +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 {
2022-12-16 23:05:54 +09:00
if ( cache . isFeatureAvailable ()) {
return true ;
}
2022-05-25 12:07:29 +02:00
2022-12-16 23:05:54 +09:00
if ( isGhes ()) {
core . warning (
'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.'
);
2022-05-25 12:07:29 +02:00
return false ;
}
2022-12-16 23:05:54 +09:00
core . warning (
'The runner was not able to contact the cache service. Caching will be skipped'
);
return false ;
2022-05-25 12:07:29 +02:00
}