This commit is contained in:
@@ -1,41 +1,11 @@
|
||||
import type { CVData, Profile, Experience, Education, Skill } from './types.js';
|
||||
import { readFileSync, readdirSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
import type { Experience, Education, Skill, Profile } from './types';
|
||||
|
||||
const CONTENT_DIR = join(process.cwd(), 'src', 'lib', 'content');
|
||||
|
||||
function loadJsonFile<T>(filepath: string): T | null {
|
||||
try {
|
||||
const content = readFileSync(filepath, 'utf-8');
|
||||
return JSON.parse(content) as T;
|
||||
} catch (error) {
|
||||
console.warn(`Could not load ${filepath}:`, error instanceof Error ? error.message : error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function loadJsonFilesFromDir<T>(dirPath: string): T[] {
|
||||
try {
|
||||
const files = readdirSync(dirPath, { withFileTypes: true });
|
||||
const items: T[] = [];
|
||||
|
||||
for (const file of files) {
|
||||
if (file.isFile() && file.name.endsWith('.json')) {
|
||||
const item = loadJsonFile<T>(join(dirPath, file.name));
|
||||
if (item) {
|
||||
items.push(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return items;
|
||||
} catch (error) {
|
||||
console.warn(
|
||||
`Could not read directory ${dirPath}:`,
|
||||
error instanceof Error ? error.message : error
|
||||
);
|
||||
return [];
|
||||
}
|
||||
async function loadJsonFilesFromGlob<T>(
|
||||
modules: Record<string, () => Promise<{ default: T }>>
|
||||
): Promise<T[]> {
|
||||
return Promise.all(
|
||||
Object.entries(modules).map(async ([_, module]) => await module().then((m) => m.default))
|
||||
);
|
||||
}
|
||||
|
||||
function decodeBase64Email(email: string): string {
|
||||
@@ -46,42 +16,50 @@ function decodeBase64Email(email: string): string {
|
||||
}
|
||||
}
|
||||
|
||||
export function loadProfile(): Profile {
|
||||
const profile = loadJsonFile<Profile>(join(CONTENT_DIR, 'profile.json')) ?? {
|
||||
name: 'Your Name',
|
||||
title: 'Developer',
|
||||
email: 'email@example.com',
|
||||
phone: undefined,
|
||||
location: 'Location',
|
||||
website: undefined,
|
||||
github: 'username',
|
||||
summary: 'A passionate developer.',
|
||||
};
|
||||
export async function loadProfile() {
|
||||
const profile: Profile = await import('$lib/content/profile.json');
|
||||
const media = import.meta.glob<{ default: string }>('$lib/media/*');
|
||||
const profilePictureModule = await (profile.profilePicture
|
||||
? media[`/${profile.profilePicture}`]?.()
|
||||
: undefined);
|
||||
const avatarModule = await (profile.avatar ? media[`/${profile.avatar}`]?.() : undefined);
|
||||
|
||||
return {
|
||||
...profile,
|
||||
email: decodeBase64Email(profile.email),
|
||||
profilePicture: profilePictureModule?.default,
|
||||
avatar: avatarModule?.default,
|
||||
};
|
||||
}
|
||||
|
||||
export function loadExperience(): Experience[] {
|
||||
return loadJsonFilesFromDir<Experience>(join(CONTENT_DIR, 'experience'));
|
||||
export async function loadExperience() {
|
||||
return loadJsonFilesFromGlob(
|
||||
import.meta.glob<{ default: Experience }>('$lib/content/experience/*.json')
|
||||
);
|
||||
}
|
||||
|
||||
export function loadEducation(): Education[] {
|
||||
return loadJsonFilesFromDir<Education>(join(CONTENT_DIR, 'education'));
|
||||
export function loadEducation() {
|
||||
return loadJsonFilesFromGlob(
|
||||
import.meta.glob<{ default: Education }>('$lib/content/education/*.json')
|
||||
);
|
||||
}
|
||||
|
||||
export function loadSkills(): Skill[] {
|
||||
return loadJsonFilesFromDir<Skill>(join(CONTENT_DIR, 'skills'));
|
||||
export function loadSkills() {
|
||||
return loadJsonFilesFromGlob(import.meta.glob<{ default: Skill }>('$lib/content/skills/*.json'));
|
||||
}
|
||||
|
||||
export function loadAllContent(): CVData {
|
||||
export async function loadAllContent() {
|
||||
const [profile, experience, education, skills] = await Promise.all([
|
||||
loadProfile(),
|
||||
loadExperience(),
|
||||
loadEducation(),
|
||||
loadSkills(),
|
||||
]);
|
||||
|
||||
return {
|
||||
profile: loadProfile(),
|
||||
experience: loadExperience(),
|
||||
education: loadEducation(),
|
||||
skills: loadSkills(),
|
||||
projects: [],
|
||||
profile,
|
||||
experience,
|
||||
education,
|
||||
skills,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user