Skip to content

Commit

Permalink
Add Improved Modular Build Function
Browse files Browse the repository at this point in the history
  • Loading branch information
LebCit committed Dec 26, 2023
1 parent e96ca4a commit 1ed9505
Show file tree
Hide file tree
Showing 16 changed files with 636 additions and 295 deletions.
294 changes: 0 additions & 294 deletions functions/build.js

This file was deleted.

66 changes: 66 additions & 0 deletions functions/build/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Internal modules
import { cp, rm } from "node:fs/promises"

// Helper functions
import { ensureFoldersExist } from "./helpers/ensureFoldersExist.js"
import { removeEmptyFolders } from "./helpers/removeEmptyFolders.js"
import { copyStaticFolderWithFilters } from "./helpers/copyStaticFolderWithFilters.js"

// Settings
import { settings } from "../../config/settings.js"

// Routes
import { archiveRoute } from "./routes/archiveRoute.js"
import { mainRoute } from "./routes/mainRoute.js"
import { markdownRoute } from "./routes/markdownRoute.js"
import { rssRoute } from "./routes/rssRoute.js"
import { searchRoute } from "./routes/searchRoute.js"
import { sitemapRoute } from "./routes/sitemapRoute.js"
import { tagRoute } from "./routes/tagRoute.js"
import { tagsRoute } from "./routes/tagsRoute.js"

/**
* Function to generate a static site
* ==================================
*/
async function build() {
try {
// Remove admin link from menu
delete settings.menuLinks.admin

// Remove existing "_site" directory if it exists
await rm("_site", { recursive: true, force: true })

// Ensure necessary folders exist
await ensureFoldersExist([
"_site/page",
"_site/pages",
"_site/posts",
"_site/templates",
"_site/tags",
"_site/search",
"_site/static",
])

// Copy "static" folder to "_site/static" with filter function
await cp("static", "_site/static", { recursive: true, filter: copyStaticFolderWithFilters })

// Execute the route creation functions
markdownRoute()
mainRoute()
archiveRoute()
tagsRoute()
tagRoute()
rssRoute()
sitemapRoute()
settings.searchFeature ? searchRoute() : delete settings.menuLinks.search

// Remove empty folders at the end of a build
await removeEmptyFolders("_site")
} catch (error) {
console.error("Build error:", error)
}
}

// Run the build function
build()
35 changes: 35 additions & 0 deletions functions/build/helpers/copyStaticFolderWithFilters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { normalize } from "node:path"
import { settings } from "../../../config/settings.js"

/**
* Helper function to filter the copy of "static" into "_site/static".
* ===================================================================
*
* @param {string} src - The path to the source "static" folder.
* @param {string} dest - The path to the destination "_site/static" folder.
*/
export const copyStaticFolderWithFilters = async (src, dest) => {
try {
const normalizedSrc = normalize(src)

// Exclude paths ending with "admin" or "dump" and their contents
if (normalizedSrc.endsWith("admin")) {
return false // Exclude this path
}

// Include only the "currentTheme" folder within the "themes" folder
if (normalizedSrc.includes("themes") && !normalizedSrc.endsWith("themes")) {
if (normalizedSrc.includes(settings.currentTheme)) {
return true // Include this path (the "currentTheme" folder)
} else {
return false // Exclude other paths within "themes"
}
}

// Include all other files/folders
return true
} catch (error) {
console.error(`Error in copyStaticFolderWithFilters: ${error.message}`)
throw error
}
}
Loading

0 comments on commit 1ed9505

Please sign in to comment.