aboutsummaryrefslogtreecommitdiffstats
path: root/app/builder/build.ts
diff options
context:
space:
mode:
authorLeonardo Bishop <me@leonardobishop.com>2023-08-28 22:02:57 +0100
committerLeonardo Bishop <me@leonardobishop.com>2023-08-28 22:02:57 +0100
commit7c9abacf956c0e135c1094e38087e018dd572965 (patch)
tree866c0a3c27cdaf0bd97ba2403a33349793f3652d /app/builder/build.ts
parent7c8ca1c5cf0067a350578d5589139abf076ef1ec (diff)
Rename files to more descriptive names
Diffstat (limited to 'app/builder/build.ts')
-rw-r--r--app/builder/build.ts88
1 files changed, 0 insertions, 88 deletions
diff --git a/app/builder/build.ts b/app/builder/build.ts
deleted file mode 100644
index 6807b89..0000000
--- a/app/builder/build.ts
+++ /dev/null
@@ -1,88 +0,0 @@
-import { render } from './render.js';
-import { Page, PageDirectory } from './pages.js';
-import fs from 'fs';
-import path from 'path';
-import { logger } from '../logger.js';
-
-export async function buildPages(): Promise<{ success: boolean, errors: number, pageDirectory: PageDirectory}> {
- // Recreate output directory
- if (process.env.SKIP_OUTPUT_DIR_CREATION !== 'true') {
- try {
- if (fs.existsSync(process.env.OUTPUT_DIR)) {
- fs.rmSync(process.env.OUTPUT_DIR, { recursive: true });
- }
- fs.mkdirSync(process.env.OUTPUT_DIR);
- } catch (e) {
- logger.error(`Failed to create output directory: ${e.message}`);
- return { success: false, errors: 0, pageDirectory: null };
- }
- }
-
-
- // Load pages
- logger.info(`Reading pages from disk...`);
- const pageDirectory = new PageDirectory(process.env.PAGES_DIR);
- await pageDirectory.init();
-
- let pagesCount = Object.keys(pageDirectory.getPages()).length;
- logger.info(`Found ${pagesCount} pages.`);
-
-
- // Render pages
- logger.info(`Rendering pages...`);
- let pagesRendered = 0;
- let pagesFailed = 0;
- for (const page of pageDirectory.getPages()) {
- if (await renderPage(page, pageDirectory)) {
- pagesRendered++;
- } else {
- pagesFailed++;
- }
- }
-
- logger.info(`Rendered ${pagesRendered} of ${pagesCount} pages.`);
-
-
- // Copy static files
- logger.info(`Copying static files...`);
- try {
- fs.cpSync(`${process.env.STATIC_DIR}`, `${process.env.OUTPUT_DIR}/static`, { recursive: true });
- logger.info(`Done.`);
- } catch (e) {
- logger.error(`Failed to copy static files: ${e.message}`);
- }
-
- return { success: pagesFailed == 0, errors: pagesFailed, pageDirectory: pageDirectory};
-}
-
-async function renderPage(page: Page, pageDirectory: PageDirectory): Promise<boolean> {
- let html;
- try {
- html = await render(page, pageDirectory);
- } catch (e) {
- logger.error(`Failed to render page ${page.originalPath}: ${e.message}`);
- return false;
- }
-
- try {
- const file = page.buildPath;
- const dir = path.dirname(file);
- if (!fs.existsSync(dir)) {
- fs.mkdirSync(dir, { recursive: true });
- }
- fs.writeFileSync(file, html);
- } catch (e) {
- logger.error(`Failed to write page ${page.buildPath}: ${e.message}`);
- return false;
- }
- return true;
-}
-
-export async function rebuildSinglePage(path: string, pageDirectory: PageDirectory): Promise<boolean> {
- const page = await pageDirectory.loadPage(path);
- if (!page) {
- return false;
- }
-
- return await renderPage(page, pageDirectory);
-}