@@ -34,13 +34,90 @@ const defaultConfig: SiteConfig = {
3434 } ,
3535} ;
3636
37+ function deepMerge ( target : any , source : any ) : any {
38+ // Recursively merge source into target
39+ for ( const key of Object . keys ( source ) ) {
40+ if (
41+ source [ key ] &&
42+ typeof source [ key ] === 'object' &&
43+ ! Array . isArray ( source [ key ] )
44+ ) {
45+ if ( ! target [ key ] || typeof target [ key ] !== 'object' ) {
46+ target [ key ] = { } ;
47+ }
48+ deepMerge ( target [ key ] , source [ key ] ) ;
49+ } else {
50+ if ( target [ key ] === undefined ) {
51+ target [ key ] = source [ key ] ;
52+ }
53+ }
54+ }
55+ return target ;
56+ }
57+
58+ function isValidConfig ( config : any ) : boolean {
59+ if (
60+ ! config ||
61+ typeof config !== 'object' ||
62+ ! config . sections ||
63+ typeof config . sections !== 'object' ||
64+ ! config . elements ||
65+ typeof config . elements !== 'object'
66+ ) {
67+ return false ;
68+ }
69+ // Optionally, check for required keys
70+ const sectionKeys = [
71+ 'about' ,
72+ 'workExperience' ,
73+ 'talks' ,
74+ 'writing' ,
75+ 'socialLinks' ,
76+ ] ;
77+ const elementKeys = [
78+ 'avatar' ,
79+ 'themeSwitch' ,
80+ 'header' ,
81+ 'footer' ,
82+ ] ;
83+ for ( const key of sectionKeys ) {
84+ if ( typeof config . sections [ key ] !== 'boolean' ) {
85+ return false ;
86+ }
87+ }
88+ for ( const key of elementKeys ) {
89+ if ( typeof config . elements [ key ] !== 'boolean' ) {
90+ return false ;
91+ }
92+ }
93+ return true ;
94+ }
95+
3796export function getSiteConfig ( ) : SiteConfig {
97+ const configPath = path . join ( process . cwd ( ) , 'config.yml' ) ;
98+ let loadedConfig : any = { } ;
3899 try {
39- const configPath = path . join ( process . cwd ( ) , 'config.yml' ) ;
40100 const fileContents = fs . readFileSync ( configPath , 'utf8' ) ;
41- return yaml . load ( fileContents ) as SiteConfig ;
42- } catch ( error ) {
43- console . warn ( 'config.yml not found, using defaults' ) ;
101+ loadedConfig = yaml . load ( fileContents ) ;
102+ if ( ! loadedConfig || typeof loadedConfig !== 'object' ) {
103+ console . warn ( 'config.yml is empty or not a valid YAML object, using defaults' ) ;
104+ return defaultConfig ;
105+ }
106+ // Merge loaded config with defaults
107+ const mergedConfig = deepMerge ( loadedConfig , defaultConfig ) ;
108+ if ( ! isValidConfig ( mergedConfig ) ) {
109+ console . warn ( 'config.yml is malformed or missing required fields, using defaults' ) ;
110+ return defaultConfig ;
111+ }
112+ return mergedConfig as SiteConfig ;
113+ } catch ( error : any ) {
114+ if ( error . code === 'ENOENT' ) {
115+ console . warn ( 'config.yml not found, using defaults' ) ;
116+ } else if ( error . name === 'YAMLException' || error instanceof yaml . YAMLException ) {
117+ console . warn ( 'config.yml is malformed YAML, using defaults' ) ;
118+ } else {
119+ console . warn ( `Error loading config.yml: ${ error . message } , using defaults` ) ;
120+ }
44121 return defaultConfig ;
45122 }
46123}
0 commit comments