import os from 'os' import {HttpClient} from '@actions/http-client' import {BearerCredentialHandler} from '@actions/http-client/lib/auth' interface GitHubTag { tag_name: string } // arch in [arm, arm64, x64...] (https://nodejs.org/docs/latest-v16.x/api/os.html#osarch) // return value in [amd64, arm64, arm] const mapArch = (arch: string): string => { const mappings: Record = { x64: 'amd64' } return mappings[arch] || arch } // os in [darwin, linux, win32...] (https://nodejs.org/docs/latest-v16.x/api/os.html#osplatform) // return value in [darwin, linux, windows] const mapOS = (platform: string): string => { const mappings: Record = { win32: 'windows' } return mappings[platform] || platform } export const getDownloadUrl = async (version: string): Promise => { const platform = mapOS(os.platform()) const arch = mapArch(os.arch()) const resolvedVersion = await resolveVersion(version) const filename = `supabase_${resolvedVersion}_${platform}_${arch}` return `https://github.com/supabase/cli/releases/download/v${resolvedVersion}/${filename}.tar.gz` } // Authenticate with GH_TOKEN to avoid GitHub API rate limits const token = process.env['GH_TOKEN'] const http = new HttpClient( 'supabase/setup-cli', token ? [new BearerCredentialHandler(token)] : undefined ) const resolveVersion = async (version: string): Promise => { if (version !== 'latest') { return version } const url = 'https://api.github.com/repos/supabase/cli/releases/latest' const tag = (await http.getJson(url)).result?.tag_name if (!tag) { throw new Error('Cannot fetch tag info') } return tag.substring(1) }