@@ -413,30 +413,65 @@ aigc_sync_app_js() {
413413 posts=(" intro-to-aigc" )
414414 fi
415415
416- # Generate postFiles array content
417- local post_array=" const postFiles = ["
418- for i in " ${! posts[@]} " ; do
419- post_array+=$' \n ' " '${posts[$i]} '"
420- if [ $i -lt $(( ${# posts[@]} - 1 )) ]; then
421- post_array+=" ,"
416+ # Helper function to update postFiles array in a file
417+ update_app_js_file () {
418+ local file=" $1 "
419+ local temp_file=" ${file} .tmp"
420+
421+ if [ ! -f " $file " ]; then
422+ return 1
422423 fi
423- done
424- post_array+=$' \n ];'
424+
425+ # Find line numbers
426+ local start_line=$( grep -n " const postFiles = \[" " $file " | cut -d: -f1)
427+ local end_line=$( grep -n " ^ \];" " $file " | cut -d: -f1)
428+
429+ if [ -z " $start_line " ] || [ -z " $end_line " ] || [ " $start_line " -ge " $end_line " ]; then
430+ echo -e " ${RED} ❌ Could not find postFiles array in $file ${NC} " >&2
431+ return 1
432+ fi
433+
434+ # Write the file with updated postFiles array
435+ {
436+ sed -n " 1,$(( start_line - 1 )) p" " $file "
437+ printf " const postFiles = [\n"
438+ for i in " ${! posts[@]} " ; do
439+ printf " '%s'" " ${posts[$i]} "
440+ if [ $i -lt $(( ${# posts[@]} - 1 )) ]; then
441+ printf " ,\n"
442+ else
443+ printf " \n"
444+ fi
445+ done
446+ printf " ];\n"
447+ sed -n " $(( end_line + 1 )) ,\$ p" " $file "
448+ } > " $temp_file "
449+
450+ if [ -s " $temp_file " ]; then
451+ mv " $temp_file " " $file "
452+ return 0
453+ else
454+ rm -f " $temp_file "
455+ echo -e " ${RED} ❌ Failed to write to temporary file for $file ${NC} " >&2
456+ return 1
457+ fi
458+ }
425459
426460 # Update aigc/app.js
427- if [ -f " aigc/app.js" ] ; then
428- # Use a temporary file for complex replacement
429- sed " /const postFiles = \[/,/\];/c \\
430- $post_array " " aigc/app.js " > " aigc/app.js.tmp " && mv " aigc/app.js.tmp " " aigc/app.js"
461+ if update_app_js_file " aigc/app.js" ; then
462+ : # Success
463+ else
464+ echo -e " ${YELLOW} ⚠️ Failed to update aigc/app.js${NC} " >&2
431465 fi
432466
433467 # Update app.js in all post directories
434- find " aigc/posts" -maxdepth 1 -type d | while read post_dir; do
435- if [ -f " $post_dir /app.js" ] && [ " $post_dir " != " aigc/posts" ]; then
436- sed " /const postFiles = \[/,/\];/c\\
437- $post_array " " $post_dir /app.js" > " $post_dir /app.js.tmp" && mv " $post_dir /app.js.tmp" " $post_dir /app.js"
438- fi
439- done
468+ if [ -d " aigc/posts" ]; then
469+ find " aigc/posts" -maxdepth 1 -type d ! -name " posts" | while read post_dir; do
470+ if [ -f " $post_dir /app.js" ]; then
471+ update_app_js_file " $post_dir /app.js" > /dev/null 2>&1 || true
472+ fi
473+ done
474+ fi
440475}
441476
442477aigc_register () {
0 commit comments