diff options
| author | Paul Buetow <paul@buetow.org> | 2026-01-27 21:10:17 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-01-27 21:10:17 +0200 |
| commit | c43c6a934659787caee89ca1a61e996988cad9a2 (patch) | |
| tree | 5337566c8eb474bf0122af324a15c8d6adaa4b4b /build.js | |
| parent | 4fcc1ad69c402cdc9cb6b9a59218ee6f99bceff1 (diff) | |
Add dist directory generation and deploy target
Build now creates dist/ with all static assets (html, css, js, data, images).
Added 'just dist' target to rsync dist/ to remote server.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Diffstat (limited to 'build.js')
| -rw-r--r-- | build.js | 84 |
1 files changed, 74 insertions, 10 deletions
@@ -3,7 +3,7 @@ * Site Generator * * Reads markdown summaries from summaries/{id}.md and embeds them into - * data/books.json so the site works offline without a server. + * data/books.json, then creates a dist directory with all static assets. * * Usage: node build.js * @@ -13,8 +13,10 @@ const fs = require('fs'); const path = require('path'); -const SUMMARIES_DIR = path.join(__dirname, 'summaries'); -const BOOKS_JSON = path.join(__dirname, 'data', 'books.json'); +const ROOT_DIR = __dirname; +const SUMMARIES_DIR = path.join(ROOT_DIR, 'summaries'); +const BOOKS_JSON = path.join(ROOT_DIR, 'data', 'books.json'); +const DIST_DIR = path.join(ROOT_DIR, 'dist'); function readSummary(id) { const filePath = path.join(SUMMARIES_DIR, `${id}.md`); @@ -31,10 +33,32 @@ function readSummary(id) { return summaryLines || null; } -function build() { - console.log('Building site...\n'); +/** + * Recursively copies a directory from src to dest. + */ +function copyDir(src, dest) { + fs.mkdirSync(dest, { recursive: true }); + const entries = fs.readdirSync(src, { withFileTypes: true }); + + for (const entry of entries) { + const srcPath = path.join(src, entry.name); + const destPath = path.join(dest, entry.name); + + if (entry.isDirectory()) { + copyDir(srcPath, destPath); + } else { + fs.copyFileSync(srcPath, destPath); + } + } +} + +/** + * Embeds summaries from markdown files into books.json. + * Returns the updated books data. + */ +function embedSummaries() { + console.log('Embedding summaries...\n'); - // Read books.json const booksData = JSON.parse(fs.readFileSync(BOOKS_JSON, 'utf8')); let updated = 0; @@ -54,11 +78,9 @@ function build() { } } - // Write updated books.json fs.writeFileSync(BOOKS_JSON, JSON.stringify(booksData, null, 2)); - console.log('\n=== Build Complete ==='); - console.log(`Summaries embedded: ${updated}`); + console.log(`\nSummaries embedded: ${updated}`); console.log(`Missing summaries: ${missing.length}`); if (missing.length > 0) { @@ -69,7 +91,49 @@ function build() { }); } - console.log('\nSite is ready. Open index.html in a browser.'); + return booksData; +} + +/** + * Creates the dist directory with all static assets for distribution. + */ +function createDist() { + console.log('\n--- Creating dist directory ---\n'); + + // Remove existing dist directory + if (fs.existsSync(DIST_DIR)) { + fs.rmSync(DIST_DIR, { recursive: true }); + } + fs.mkdirSync(DIST_DIR); + + // Copy static files + fs.copyFileSync(path.join(ROOT_DIR, 'index.html'), path.join(DIST_DIR, 'index.html')); + console.log('✓ index.html'); + + // Copy directories + copyDir(path.join(ROOT_DIR, 'css'), path.join(DIST_DIR, 'css')); + console.log('✓ css/'); + + copyDir(path.join(ROOT_DIR, 'js'), path.join(DIST_DIR, 'js')); + console.log('✓ js/'); + + copyDir(path.join(ROOT_DIR, 'data'), path.join(DIST_DIR, 'data')); + console.log('✓ data/'); + + copyDir(path.join(ROOT_DIR, 'images'), path.join(DIST_DIR, 'images')); + console.log('✓ images/'); + + console.log('\nDist directory created at: dist/'); +} + +function build() { + console.log('=== Building Site ===\n'); + + embedSummaries(); + createDist(); + + console.log('\n=== Build Complete ==='); + console.log('Site is ready. Open dist/index.html in a browser.'); } build(); |
