import { execFileSync } from 'child_process'; import fs from 'fs'; import os from 'os'; import path from 'path'; import type Limrun from '@limrun/api'; /** * Uploads a Mac app to asset storage for an install: an archive as it is, or a `.app` folder as a * tar.gz of it, which keeps the symlinks a Mac app's frameworks need. Returns the asset with its * download URL. The asset keeps the file's own name. */ export async function uploadMacApp( client: Limrun, file: string, ttl: string | undefined, info: (line: string) => void, ): Promise<{ name: string; signedDownloadUrl: string; md5?: string }> { const resolved = path.resolve(file); if (!fs.existsSync(resolved)) throw new Error(`File not found: ${resolved}`); const name = path.basename(resolved); let upload = resolved; let tmpDir: string | undefined; if (fs.statSync(resolved).isDirectory()) { info(`Archiving ${name}...`); tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'lim-mac-app-')); upload = path.join(tmpDir, `${name}.tar.gz`); execFileSync('tar', ['-czf', upload, '-C', path.dirname(resolved), name]); } try { info(`Uploading ${name}...`); return await client.assets.getOrUpload({ path: upload, name, ...(ttl && { ttl }) }); } finally { if (tmpDir) fs.rmSync(tmpDir, { recursive: true, force: true }); } } /** Resolves an app asset by exact name to the URL the instance downloads it from. */ export async function macAppDownload(client: Limrun, name: string): Promise<{ url: string; md5?: string }> { const [asset] = await client.assets.list({ nameFilter: name, includeDownloadUrl: true, includeAppStore: name.startsWith('appstore/'), limit: 1, }); if (!asset || asset.name !== name || !asset.signedDownloadUrl) { throw new Error(`No asset named ${name} with an uploaded file.`); } return { url: asset.signedDownloadUrl, ...(asset.md5 && { md5: asset.md5 }) }; }