« get me outta code hell

write: static-build: use web routes - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-03-26 21:17:27 -0300
committer(quasar) nebula <qznebula@protonmail.com>2024-05-01 20:00:46 -0300
commita2bd7af8919e282b1ae8ff3c339dea50b1051249 (patch)
treeebe87a8b7b5495b5472a7fee543e29854989f5b9 /src
parent2f2c83f3c31a7ebb91ec40b2a837386459cd9ce2 (diff)
write: static-build: use web routes
Diffstat (limited to 'src')
-rw-r--r--src/write/build-modes/static-build.js85
1 files changed, 47 insertions, 38 deletions
diff --git a/src/write/build-modes/static-build.js b/src/write/build-modes/static-build.js
index 679d39d8..a70da3e7 100644
--- a/src/write/build-modes/static-build.js
+++ b/src/write/build-modes/static-build.js
@@ -52,6 +52,10 @@ export const config = {
   thumbs: {
     default: 'perform',
   },
+
+  webRoutes: {
+    required: true,
+  },
 };
 
 export function getCLIOptions() {
@@ -99,9 +103,7 @@ export function getCLIOptions() {
 
 export async function go({
   cliOptions,
-  _dataPath,
   mediaPath,
-  mediaCachePath,
   queueSize,
 
   defaultLanguage,
@@ -110,6 +112,7 @@ export async function go({
   srcRootPath,
   thumbsCache,
   urls,
+  webRoutes,
   wikiData,
 
   cachebust,
@@ -148,12 +151,9 @@ export async function go({
 
   await mkdir(outputPath, {recursive: true});
 
-  await writeSymlinks({
-    srcRootPath,
-    mediaPath,
-    mediaCachePath,
+  await writeWebRouteSymlinks({
     outputPath,
-    urls,
+    webRoutes,
   });
 
   if (writeAll) {
@@ -432,42 +432,51 @@ async function writePage({
   ].filter(Boolean));
 }
 
-function writeSymlinks({
-  srcRootPath,
-  mediaPath,
-  mediaCachePath,
+function writeWebRouteSymlinks({
   outputPath,
-  urls,
+  webRoutes,
 }) {
-  return progressPromiseAll('Writing site symlinks.', [
-    link(path.join(srcRootPath, 'util'), 'shared.utilityRoot'),
-    link(path.join(srcRootPath, 'static'), 'shared.staticRoot'),
-    link(mediaPath, 'media.root'),
-    link(mediaCachePath, 'thumb.root'),
-  ]);
-
-  async function link(directory, urlKey) {
-    const pathname = urls.from('shared.root').toDevice(urlKey);
-    const file = path.join(outputPath, pathname);
-
-    try {
-      await unlink(file);
-    } catch (error) {
-      if (error.code !== 'ENOENT') {
-        throw error;
+  const promises =
+    webRoutes.map(async route => {
+      // TODO: Make web routes specify `to` via url spec
+      /*
+      const pathname = urls.from('shared.root').toDevice(urlKey);
+      const file = path.join(outputPath, pathname);
+      */
+
+      const parts =
+        route.to
+          .replace(/^\//, '')
+          .split('/');
+
+      const parentDirectoryParts = parts.slice(0, -1);
+      const symlinkNamePart = parts.at(-1);
+
+      const parentDirectory = path.join(outputPath, ...parentDirectoryParts);
+      const symlinkPath = path.join(parentDirectory, symlinkNamePart);
+
+      try {
+        await unlink(symlinkPath);
+      } catch (error) {
+        if (error.code !== 'ENOENT') {
+          throw error;
+        }
       }
-    }
 
-    try {
-      await symlink(path.resolve(directory), file);
-    } catch (error) {
-      if (error.code === 'EPERM') {
-        await symlink(path.resolve(directory), file, 'junction');
-      } else {
-        throw error;
+      await mkdir(parentDirectory, {recursive: true});
+
+      try {
+        await symlink(route.from, symlinkPath);
+      } catch (error) {
+        if (error.code === 'EPERM') {
+          await symlink(route.from, symlinkPath, 'junction');
+        } else {
+          throw error;
+        }
       }
-    }
-  }
+    });
+
+  return progressPromiseAll(`Writing web route symlinks.`, promises);
 }
 
 async function writeFavicon({