export type ApkEnvironment = 'dev' | 'sandbox' | 'product' | 'other'; export interface ParsedApkMetadata { environment: ApkEnvironment; commit: string | null; isValid: boolean; } // Environment mapping based on filename patterns const ENVIRONMENT_MAP: Record = { 'timeshift': 'dev', 'lan': 'dev', 'dev': 'dev', 'sandbox': 'sandbox', }; export function detectEnvironment(filename: string): ApkEnvironment { // Pattern: ft_[environment]_version_commit.apk // Extract the environment segment between first and second underscores const match = filename.match(/^ft_([^_]+)_/); if (!match) { // If it doesn't start with ft_ at all, it's an invalid format if (!filename.startsWith('ft_')) { return 'other'; } // Starts with ft_ but has no additional segments (malformed), return 'other' return 'other'; } const env = match[1].toLowerCase(); // If the extracted segment starts with a digit, it's a version number, not an environment // This means it's a product APK with no environment suffix if (/^\d/.test(env)) { return 'product'; } return ENVIRONMENT_MAP[env] || 'other'; } export function parseApkFilename(filename: string): ParsedApkMetadata { const environment = detectEnvironment(filename); const commitMatch = filename.match(/([a-f0-9]{7,})\.apk$/); const commit = commitMatch ? commitMatch[1] : null; const isValid = commit !== null; return { environment, commit, isValid, }; }