49 lines
1.5 KiB
TypeScript
49 lines
1.5 KiB
TypeScript
// lib/utils.ts
|
|
// Client-safe utility functions
|
|
import * as fs from 'fs';
|
|
import * as path from 'path';
|
|
|
|
/**
|
|
* 格式化版本号显示 (0_42 -> 0.42)
|
|
*/
|
|
export function formatVersion(version: string): string {
|
|
return version.replace(/_/g, '.');
|
|
}
|
|
|
|
/**
|
|
* 格式化文件大小
|
|
*/
|
|
export function formatFileSize(bytes: number): string {
|
|
if (bytes < 1024) return bytes + ' B';
|
|
if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(2) + ' KB';
|
|
if (bytes < 1024 * 1024 * 1024) return (bytes / (1024 * 1024)).toFixed(2) + ' MB';
|
|
return (bytes / (1024 * 1024 * 1024)).toFixed(2) + ' GB';
|
|
}
|
|
|
|
/**
|
|
* 格式化日期时间
|
|
*/
|
|
export function formatDateTime(date: Date): string {
|
|
const year = date.getFullYear();
|
|
const month = String(date.getMonth() + 1).padStart(2, '0');
|
|
const day = String(date.getDate()).padStart(2, '0');
|
|
const hours = String(date.getHours()).padStart(2, '0');
|
|
const minutes = String(date.getMinutes()).padStart(2, '0');
|
|
return `${year}-${month}-${day} ${hours}:${minutes}`;
|
|
}
|
|
|
|
/**
|
|
* 获取项目显示名称(从别名配置)
|
|
*/
|
|
export function getProjectDisplayName(projectName: string): string {
|
|
try {
|
|
const aliasesPath = path.join(process.cwd(), 'lib', 'project-aliases.json');
|
|
const aliasesContent = fs.readFileSync(aliasesPath, 'utf-8');
|
|
const aliases = JSON.parse(aliasesContent);
|
|
return aliases[projectName] || projectName;
|
|
} catch {
|
|
// If file doesn't exist or is invalid, return original name
|
|
return projectName;
|
|
}
|
|
}
|