feat: add APK filename parser utilities

- Add detectEnvironment() to parse environment from APK filenames
- Add parseApkFilename() to extract environment and commit ID metadata
- Support dev, sandbox, product, and other environment types
- Map environment patterns: timeshift, lan, dev -> dev; sandbox -> sandbox
- Detect product APKs when no environment suffix present
- Handle invalid formats gracefully
This commit is contained in:
2026-03-04 21:13:29 +08:00
parent 889c6ee2f6
commit 5e280d4cbe
2 changed files with 109 additions and 0 deletions

53
lib/apk-parser.ts Normal file
View File

@@ -0,0 +1,53 @@
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<string, ApkEnvironment> = {
'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,
};
}