feat: add getProjectDisplayName utility
This commit is contained in:
@@ -1,4 +1,11 @@
|
|||||||
import { formatVersion, formatFileSize, formatDateTime } from '../utils';
|
import { formatVersion, formatFileSize, formatDateTime, getProjectDisplayName } from '../utils';
|
||||||
|
|
||||||
|
// Mock fs module
|
||||||
|
const fs = require('fs');
|
||||||
|
jest.mock('fs', () => ({
|
||||||
|
...jest.requireActual('fs'),
|
||||||
|
readFileSync: jest.fn(),
|
||||||
|
}));
|
||||||
|
|
||||||
describe('formatVersion', () => {
|
describe('formatVersion', () => {
|
||||||
test('应该将下划线替换为点号', () => {
|
test('应该将下划线替换为点号', () => {
|
||||||
@@ -63,3 +70,39 @@ describe('formatDateTime', () => {
|
|||||||
expect(formatDateTime(date)).toBe('2024-01-05 09:05');
|
expect(formatDateTime(date)).toBe('2024-01-05 09:05');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('getProjectDisplayName', () => {
|
||||||
|
const originalAliases = {
|
||||||
|
'FT': '外放构建',
|
||||||
|
'FT_DEV': '开发构建',
|
||||||
|
};
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
// Mock the aliases file
|
||||||
|
fs.readFileSync.mockReturnValue(JSON.stringify(originalAliases));
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
jest.clearAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return display name for aliased project', () => {
|
||||||
|
expect(getProjectDisplayName('FT')).toBe('外放构建');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return directory name when no alias exists', () => {
|
||||||
|
expect(getProjectDisplayName('UNKNOWN')).toBe('UNKNOWN');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return directory name when aliases file is empty', () => {
|
||||||
|
fs.readFileSync.mockReturnValue('{}');
|
||||||
|
expect(getProjectDisplayName('FT')).toBe('FT');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return directory name when aliases file cannot be read', () => {
|
||||||
|
fs.readFileSync.mockImplementation(() => {
|
||||||
|
throw new Error('File not found');
|
||||||
|
});
|
||||||
|
expect(getProjectDisplayName('FT')).toBe('FT');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
17
lib/utils.ts
17
lib/utils.ts
@@ -1,5 +1,7 @@
|
|||||||
// lib/utils.ts
|
// lib/utils.ts
|
||||||
// Client-safe utility functions
|
// Client-safe utility functions
|
||||||
|
import * as fs from 'fs';
|
||||||
|
import * as path from 'path';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 格式化版本号显示 (0_42 -> 0.42)
|
* 格式化版本号显示 (0_42 -> 0.42)
|
||||||
@@ -29,3 +31,18 @@ export function formatDateTime(date: Date): string {
|
|||||||
const minutes = String(date.getMinutes()).padStart(2, '0');
|
const minutes = String(date.getMinutes()).padStart(2, '0');
|
||||||
return `${year}-${month}-${day} ${hours}:${minutes}`;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user