From c668ab157916281be818c0102837fe98e67a0d63 Mon Sep 17 00:00:00 2001 From: Nikita Melnikov Date: Wed, 28 Dec 2022 04:16:24 +0400 Subject: [PATCH 1/4] add error handler for creating dist folder --- package.json | 2 +- src/backend/build-static.ts | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 490b138c..ea91a71f 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "codex.docs", "license": "Apache-2.0", - "version": "v2.2.0-rc.11", + "version": "v2.2.0-rc.12", "type": "module", "bin": { "codex.docs": "dist/backend/app.js" diff --git a/src/backend/build-static.ts b/src/backend/build-static.ts index 2b17fa4b..c398952f 100644 --- a/src/backend/build-static.ts +++ b/src/backend/build-static.ts @@ -54,7 +54,15 @@ export default async function buildStatic(): Promise { const pagesOrder = await PagesOrder.getAll(); const allPages = await Page.getAll(); - await mkdirp(distPath); + try { + console.log('Create dist folder'); + await mkdirp(distPath); + } catch (e) { + console.log('Error while creating dist folder', e); + + return; + } + /** * Renders single page From 4d9418024497b41ed5a3d121078cbe0e27418c52 Mon Sep 17 00:00:00 2001 From: Nikita Melnikov Date: Wed, 28 Dec 2022 04:23:17 +0400 Subject: [PATCH 2/4] fix favicon in generated files --- src/backend/build-static.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/backend/build-static.ts b/src/backend/build-static.ts index c398952f..7d11e63a 100644 --- a/src/backend/build-static.ts +++ b/src/backend/build-static.ts @@ -13,6 +13,7 @@ import fse from 'fs-extra'; import appConfig from './utils/appConfig.js'; import Aliases from './controllers/aliases.js'; import Pages from './controllers/pages.js'; +import { downloadFavicon } from './utils/downloadFavicon.js'; /** * Build static pages from database @@ -82,12 +83,18 @@ export default async function buildStatic(): Promise { const previousPage = await PagesFlatArray.getPageBefore(pageId); const nextPage = await PagesFlatArray.getPageAfter(pageId); const menu = createMenuTree(parentIdOfRootPages, allPages, pagesOrder, 2); + const favicon = appConfig.favicon ? await downloadFavicon(appConfig.favicon, distPath) : { + destination: '/favicon.png', + type: 'image/png', + }; + const result = await renderTemplate('./views/pages/page.twig', { page, pageParent, previousPage, nextPage, menu, + favicon, config: appConfig.frontend, }); From c8a2b87b1c9d486e99f4e90a6b470e81a42eabdb Mon Sep 17 00:00:00 2001 From: Nikita Melnikov Date: Wed, 28 Dec 2022 04:46:03 +0400 Subject: [PATCH 3/4] add option for generating pages inside separate folders --- package.json | 2 +- src/backend/build-static.ts | 19 ++++++++++++++++++- src/backend/utils/appConfig.ts | 5 ++++- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index ea91a71f..9cb812d7 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "codex.docs", "license": "Apache-2.0", - "version": "v2.2.0-rc.12", + "version": "v2.2.0-rc.13", "type": "module", "bin": { "codex.docs": "dist/backend/app.js" diff --git a/src/backend/build-static.ts b/src/backend/build-static.ts index 7d11e63a..93492687 100644 --- a/src/backend/build-static.ts +++ b/src/backend/build-static.ts @@ -73,6 +73,11 @@ export default async function buildStatic(): Promise { */ async function renderPage(page: Page, isIndex?: boolean): Promise { console.log(`Rendering page ${page.uri}`); + const pageUri = page.uri; + + if (!pageUri) { + throw new Error('Page uri is not defined'); + } const pageParent = await page.getParent(); const pageId = page._id; @@ -98,7 +103,19 @@ export default async function buildStatic(): Promise { config: appConfig.frontend, }); - const filename = (isIndex || page.uri === '') ? 'index.html' : `${page.uri}.html`; + let filename: string; + + if (isIndex) { + filename = 'index.html'; + } else if (config?.pagesInsideFolders) { // create folder for each page if pagesInsideFolders is true + const pagePath = path.resolve(distPath, pageUri); + + await mkdirp(pagePath); + + filename = path.resolve(pagePath, 'index.html'); + } else { + filename = `${page.uri}.html`; + } await fs.writeFile(path.resolve(distPath, filename), result); console.log(`Page ${page.uri} rendered`); diff --git a/src/backend/utils/appConfig.ts b/src/backend/utils/appConfig.ts index 8d3e60ed..34a5e610 100644 --- a/src/backend/utils/appConfig.ts +++ b/src/backend/utils/appConfig.ts @@ -90,7 +90,10 @@ const FrontendConfig = z.object({ */ const StaticBuildConfig = z.object({ outputDir: z.string(), // Output directory for static build - overwrite: z.boolean().optional().default(true), + overwrite: z.boolean().optional() // Overwrite output directory + .default(true), + pagesInsideFolders: z.boolean().optional() // Create separate folder for each page + .default(true), indexPage: z.object({ enabled: z.boolean(), // Is index page enabled uri: z.string(), // Index page uri From 6e2cb1bf8fa8f06f875053d012adf5195c6332d6 Mon Sep 17 00:00:00 2001 From: Nikita Melnikov Date: Wed, 28 Dec 2022 05:39:56 +0400 Subject: [PATCH 4/4] fix favicon problem --- package.json | 2 +- src/backend/build-static.ts | 35 ++++++++++++++-------------- src/backend/utils/downloadFavicon.ts | 17 +++++++------- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/package.json b/package.json index 9cb812d7..74626808 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "codex.docs", "license": "Apache-2.0", - "version": "v2.2.0-rc.13", + "version": "v2.2.0-rc.16", "type": "module", "bin": { "codex.docs": "dist/backend/app.js" diff --git a/src/backend/build-static.ts b/src/backend/build-static.ts index 93492687..ca0894aa 100644 --- a/src/backend/build-static.ts +++ b/src/backend/build-static.ts @@ -64,6 +64,24 @@ export default async function buildStatic(): Promise { return; } + console.log('Copy public directory'); + const publicDir = path.resolve(dirname, '../../public'); + + console.log(`Copy from ${publicDir} to ${distPath}`); + + try { + await fse.copy(publicDir, distPath); + console.log('Public directory copied'); + } catch (e) { + console.log('Error while copying public directory'); + console.error(e); + } + + const favicon = appConfig.favicon ? await downloadFavicon(appConfig.favicon, distPath, '') : { + destination: '/favicon.png', + type: 'image/png', + }; + /** * Renders single page @@ -88,10 +106,6 @@ export default async function buildStatic(): Promise { const previousPage = await PagesFlatArray.getPageBefore(pageId); const nextPage = await PagesFlatArray.getPageAfter(pageId); const menu = createMenuTree(parentIdOfRootPages, allPages, pagesOrder, 2); - const favicon = appConfig.favicon ? await downloadFavicon(appConfig.favicon, distPath) : { - destination: '/favicon.png', - type: 'image/png', - }; const result = await renderTemplate('./views/pages/page.twig', { page, @@ -151,19 +165,6 @@ export default async function buildStatic(): Promise { } console.log('Static files built'); - console.log('Copy public directory'); - const publicDir = path.resolve(dirname, '../../public'); - - console.log(`Copy from ${publicDir} to ${distPath}`); - - try { - await fse.copy(publicDir, distPath); - console.log('Public directory copied'); - } catch (e) { - console.log('Error while copying public directory'); - console.error(e); - } - if (appConfig.uploads.driver === 'local') { console.log('Copy uploads directory'); await fse.copy(path.resolve(cwd, appConfig.uploads.local.path), path.resolve(distPath, 'uploads')); diff --git a/src/backend/utils/downloadFavicon.ts b/src/backend/utils/downloadFavicon.ts index 73aa2c00..ed10cdbd 100644 --- a/src/backend/utils/downloadFavicon.ts +++ b/src/backend/utils/downloadFavicon.ts @@ -1,5 +1,5 @@ import path from 'path'; -import fs from 'fs'; +import fs from 'fs/promises'; import fetch, { RequestInit } from 'node-fetch'; /** @@ -32,9 +32,10 @@ function checkIsUrl(str: string): boolean { * * @param destination - url or path of favicon * @param faviconFolder - folder to save favicon + * @param subRoute - subroute from which the favicon will be served * @returns { Promise } - Promise with data about favicon */ -export async function downloadFavicon(destination: string, faviconFolder: string): Promise { +export async function downloadFavicon(destination: string, faviconFolder: string, subRoute = '/favicon'): Promise { // Check of destination is empty if (!destination) { throw Error('Favicon destination is empty'); @@ -48,8 +49,10 @@ export async function downloadFavicon(destination: string, faviconFolder: string // Check if string is url if (!checkIsUrl(destination)) { + await fs.copyFile(destination, path.join(faviconFolder, filename)); + return { - destination: `/${filename}`, + destination: `${subRoute}/${filename}`, type: `image/${format}`, } as FaviconData; } @@ -72,14 +75,10 @@ export async function downloadFavicon(destination: string, faviconFolder: string const filePath = path.join(faviconFolder, `favicon.${format}`); // Save file - await fs.writeFile(filePath, fileData, (err) => { - if (err) { - console.log(err); - } - }); + await fs.writeFile(filePath, fileData); return { - destination: `/favicon/favicon.${format}`, + destination: `${subRoute}/favicon.${format}`, type: `image/${format}`, } as FaviconData; }