import fs from 'fs' ; import path from 'path' ; import * as semver from 'semver' ; import * as exec from '@actions/exec' ; import * as context from './context' ; export async function getMetadataFile ( ) { return path . join ( context . tmpDir ( ) , 'metadata-file' ) . split ( path . sep ) . join ( path . posix . sep ) ; } export async function getMetadata ( ) { const metadataFile = await getMetadataFile ( ) ; if ( ! fs . existsSync ( metadataFile ) ) { return undefined ; } const content = fs . readFileSync ( metadataFile , { encoding : 'utf-8' } ) . trim ( ) ; if ( content === 'null' ) { return undefined ; } return content ; } export async function isAvailable ( ) { return await exec . getExecOutput ( 'docker' , [ 'buildx' ] , { ignoreReturnCode : true , silent : true } ) . then ( res => { if ( res . stderr . length > 0 && res . exitCode != 0 ) { return false ; } return res . exitCode == 0 ; } ) ; } export async function getVersion ( ) { return await exec . getExecOutput ( 'docker' , [ 'buildx' , 'version' ] , { ignoreReturnCode : true , silent : true } ) . then ( res => { if ( res . stderr . length > 0 && res . exitCode != 0 ) { throw new Error ( res . stderr . trim ( ) ) ; } return parseVersion ( res . stdout . trim ( ) ) ; } ) ; } export function parseVersion ( stdout ) { const matches = /\sv?([0-9a-f]{7}|[0-9.]+)/ . exec ( stdout ) ; if ( ! matches ) { throw new Error ( `Cannot parse buildx version` ) ; } return matches [ 1 ] ; } export function satisfies ( version , range ) { return semver . satisfies ( version , range ) || /^[0-9a-f]{7}$/ . exec ( version ) !== null ; }