From 81daaa174a6f9ba2ba330027ba093a28fd7d6c81 Mon Sep 17 00:00:00 2001 From: Knight Date: Sat, 28 Dec 2024 11:01:47 -0500 Subject: [PATCH] Dumb hardcoding --- docker-compose.yml | 2 +- generateSite.js | 86 ++++++++++++++++++++++++++-------------------- start.sh | 5 ++- 3 files changed, 52 insertions(+), 41 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index cbbd0d4..61f403f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,7 +5,7 @@ services: build: . volumes: - ./images:/app/images - - ./images.json:/app/images.json + - /var/core/analogGallery/images.json:/app/images.json expose: - "3000" environment: diff --git a/generateSite.js b/generateSite.js index 4eb8929..aa839bd 100644 --- a/generateSite.js +++ b/generateSite.js @@ -7,38 +7,42 @@ import { fileURLToPath } from 'url'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); async function generateSite() { - // Read the images data - const imagesData = JSON.parse( - await fs.readFile('images.json', 'utf-8') - ); - - console.log(imagesData); - - // Create output directory if it doesn't exist - await fs.mkdir('public', { recursive: true }); - - // Copy the CSS file to public directory - await fs.copyFile('public/styles.css', 'public/styles.css').catch(err => { - console.error('Error copying CSS file:', err); - }); - - // Copy the favicon - await fs.copyFile('public/AnalogCameraS.png', 'public/favicon.png').catch(err => { - console.error('Error copying favicon:', err); - }); - - // Generate individual pages for each image - for (let i = 0; i < imagesData.length; i++) { - const image = imagesData[i]; - const prevImage = imagesData[i > 0 ? i - 1 : imagesData.length - 1]; - const nextImage = imagesData[(i + 1) % imagesData.length]; + try { + // Check if images.json exists before trying to read it + await fs.access('images.json', fs.constants.F_OK); - // Use image ID for filename - const fileName = `${image.id}.html`; - const prevFileName = `${prevImage.id}.html`; - const nextFileName = `${nextImage.id}.html`; + // Read the images data + const imagesData = JSON.parse( + await fs.readFile('images.json', 'utf-8') + ); + + console.log(imagesData); + + // Create output directory if it doesn't exist + await fs.mkdir('public', { recursive: true }); - const html = ` + // Copy the CSS file to public directory + await fs.copyFile('public/styles.css', 'public/styles.css').catch(err => { + console.error('Error copying CSS file:', err); + }); + + // Copy the favicon + await fs.copyFile('public/AnalogCameraS.png', 'public/favicon.png').catch(err => { + console.error('Error copying favicon:', err); + }); + + // Generate individual pages for each image + for (let i = 0; i < imagesData.length; i++) { + const image = imagesData[i]; + const prevImage = imagesData[i > 0 ? i - 1 : imagesData.length - 1]; + const nextImage = imagesData[(i + 1) % imagesData.length]; + + // Use image ID for filename + const fileName = `${image.id}.html`; + const prevFileName = `${prevImage.id}.html`; + const nextFileName = `${nextImage.id}.html`; + + const html = ` @@ -98,11 +102,11 @@ async function generateSite() { `; - await fs.writeFile(`public/${fileName}`, html); - } + await fs.writeFile(`public/${fileName}`, html); + } - // Update index.html to redirect to first image's ID - const indexHtml = ` + // Update index.html to redirect to first image's ID + const indexHtml = ` @@ -113,9 +117,17 @@ async function generateSite() { `; - await fs.writeFile('public/index.html', indexHtml); - - console.log('Site generated successfully!'); + await fs.writeFile('public/index.html', indexHtml); + + console.log('Site generated successfully!'); + } catch (error) { + if (error.code === 'ENOENT') { + console.error('images.json does not exist. Please run fetchImages.js first.'); + } else { + console.error('Error generating site:', error); + } + throw error; + } } async function serveStaticSite(port = 3000) { diff --git a/start.sh b/start.sh index eb6a2bb..3be32d2 100644 --- a/start.sh +++ b/start.sh @@ -1,3 +1,2 @@ -#!/bin/sh -node generateSite.js & # Run the site generator in background -node fetchImages.js # Run the image fetcher in foreground \ No newline at end of file +#!/bin/bash +node fetchImages.js && node generateSite.js \ No newline at end of file