Files
ftdl/lib/apk-parser.ts
tech 8d7d2f37d7 fix: show actual environment name for 'other' category
- Add rawEnvironment field to preserve extracted environment name
- Display rawEnvironment (e.g., 'timeshift') instead of 'other' in badge

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-05 15:13:52 +08:00

65 lines
1.8 KiB
TypeScript

import { APK_TABS } from './constants';
export type ApkEnvironment = 'product' | 'dev' | 'sandbox' | 'other';
export interface ParsedApkMetadata {
environment: ApkEnvironment;
rawEnvironment: string;
commit: string | null;
isValid: boolean;
}
export function getApkTabs(): string[] {
return APK_TABS;
}
export function detectEnvironment(prefix: string, configuredTabs: string[]): { environment: ApkEnvironment; rawEnvironment: string } {
// Empty prefix → other
if (!prefix) {
return { environment: 'other', rawEnvironment: 'other' };
}
// No underscore means product (e.g., "ft")
if (!prefix.includes('_')) {
return { environment: 'product', rawEnvironment: 'product' };
}
// Extract environment from last segment after underscore
const lastUnderscoreIndex = prefix.lastIndexOf('_');
const rawEnv = prefix.substring(lastUnderscoreIndex + 1);
// Check if environment is configured
if (configuredTabs.includes(rawEnv)) {
return { environment: rawEnv as ApkEnvironment, rawEnvironment: rawEnv };
}
return { environment: 'other', rawEnvironment: rawEnv };
}
export function parseApkFilename(filename: string, version: string): ParsedApkMetadata {
// Use version as delimiter (with leading underscore)
const delimiter = `_${version}`;
const delimiterIndex = filename.indexOf(delimiter);
let prefix: string;
if (delimiterIndex > 0) {
prefix = filename.substring(0, delimiterIndex);
} else {
// Fallback: can't find version delimiter
prefix = '';
}
const { environment, rawEnvironment } = detectEnvironment(prefix, APK_TABS);
// Extract commit ID
const commitMatch = filename.match(/([a-f0-9]{7,})\.apk$/);
const commit = commitMatch ? commitMatch[1] : null;
return {
environment,
rawEnvironment,
commit,
isValid: commit !== null,
};
}