« get me outta code hell

web-routes: basic implementation - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-03-26 21:13:32 -0300
committer(quasar) nebula <qznebula@protonmail.com>2024-05-01 20:00:45 -0300
commitcf47bb13a9a6713d94034e41d63e756d1f02a0f2 (patch)
tree098676b4d5fee194c7152cda7b5108247c2f0727
parentc3d3d65145105c15b1cfa69c927067de48a37862 (diff)
web-routes: basic implementation
-rw-r--r--package.json1
-rwxr-xr-xsrc/upd8.js46
-rw-r--r--src/web-routes.js61
3 files changed, 108 insertions, 0 deletions
diff --git a/package.json b/package.json
index 0a28b226..983c6796 100644
--- a/package.json
+++ b/package.json
@@ -46,6 +46,7 @@
         "#thumbs": "./src/gen-thumbs.js",
         "#urls": "./src/util/urls.js",
         "#validators": "./src/data/validators.js",
+        "#web-routes": "./src/web-routes.js",
         "#wiki-data": "./src/util/wiki-data.js",
         "#yaml": "./src/data/yaml.js"
     },
diff --git a/src/upd8.js b/src/upd8.js
index 96a1e9e8..a1ef2924 100755
--- a/src/upd8.js
+++ b/src/upd8.js
@@ -50,6 +50,7 @@ import {isMain, traverse} from '#node-utils';
 import {sortByName} from '#sort';
 import {empty, withEntries} from '#sugar';
 import {generateURLs, urlSpec} from '#urls';
+import {identifyAllWebRoutes} from '#web-routes';
 import {linkWikiDataArrays, loadAndProcessDataDocuments, sortWikiDataArrays}
   from '#yaml';
 
@@ -169,6 +170,9 @@ async function main() {
     preloadFileSizes:
       {...defaultStepStatus, name: `preload file sizes`},
 
+    identifyWebRoutes:
+      {...defaultStepStatus, name: `identify web routes`},
+
     performBuild:
       {...defaultStepStatus, name: `perform selected build mode`},
   };
@@ -659,6 +663,11 @@ async function main() {
       },
     });
 
+    fallbackStep('identifyWebRoutes', {
+      default: 'skip',
+      buildConfig: 'webRoutes',
+    });
+
     fallbackStep('verifyImagePaths', {
       default: 'perform',
       buildConfig: 'mediaValidation',
@@ -1771,6 +1780,42 @@ async function main() {
     }
   }
 
+  let webRoutes = null;
+
+  if (stepStatusSummary.identifyWebRoutes.status === STATUS_NOT_STARTED) {
+    Object.assign(stepStatusSummary.identifyWebRoutes, {
+      status: STATUS_STARTED_NOT_DONE,
+      timeStart: Date.now(),
+    });
+
+    try {
+      webRoutes = await identifyAllWebRoutes({
+        mediaCachePath,
+        mediaPath,
+      });
+    } catch (error) {
+      console.error(error);
+
+      logError`There was an issue identifying web routes!`;
+      fileIssue();
+
+      Object.assign(stepStatusSummary.identifyWebRoutes, {
+        status: STATUS_FATAL_ERROR,
+        message: `JavaScript error - view log for details`,
+        timeEnd: Date.now(),
+      });
+
+      return false;
+    }
+
+    logInfo`Successfully determined web routes.`;
+
+    Object.assign(stepStatusSummary.identifyWebRoutes, {
+      status: STATUS_DONE_CLEAN,
+      timeEnd: Date.now(),
+    });
+  }
+
   if (stepStatusSummary.performBuild.status === STATUS_NOT_APPLICABLE) {
     return true;
   }
@@ -1827,6 +1872,7 @@ async function main() {
       thumbsCache,
       urls,
       urlSpec,
+      webRoutes,
       wikiData,
 
       cachebust: '?' + CACHEBUST,
diff --git a/src/web-routes.js b/src/web-routes.js
new file mode 100644
index 00000000..1d010b66
--- /dev/null
+++ b/src/web-routes.js
@@ -0,0 +1,61 @@
+import {readdir} from 'node:fs/promises';
+import * as path from 'node:path';
+import {fileURLToPath} from 'node:url';
+
+const __dirname = path.dirname(fileURLToPath(import.meta.url));
+
+const codeSrcPath = __dirname;
+const codeRootPath = path.resolve(codeSrcPath, '..');
+
+function getNodeDependencyRootPath(dependencyName) {
+  const packageJSON =
+    import.meta.resolve(dependencyName + '/package.json');
+
+  return path.dirname(fileURLToPath(packageJSON));
+}
+
+export const stationaryCodeRoutes = [
+  {
+    from: path.join(codeSrcPath, 'static'),
+    to: '/static',
+  },
+
+  {
+    from: path.join(codeSrcPath, 'util'),
+    to: '/util',
+  },
+];
+
+export const dependencyRoutes = [];
+
+export const allStaticWebRoutes = [
+  ...stationaryCodeRoutes,
+  ...dependencyRoutes,
+];
+
+export async function identifyDynamicWebRoutes({
+  mediaPath,
+  mediaCachePath,
+}) {
+  const routeFunctions = [
+    () => Promise.resolve([
+      {from: path.resolve(mediaPath), to: '/media'},
+      {from: path.resolve(mediaCachePath), to: '/thumb'},
+    ]),
+  ];
+
+  const routeCheckPromises =
+    routeFunctions.map(fn => fn());
+
+  const routeCheckResults =
+    await Promise.all(routeCheckPromises);
+
+  return routeCheckResults.flat();
+}
+
+export async function identifyAllWebRoutes(opts) {
+  return [
+    ...allStaticWebRoutes,
+    ...await identifyDynamicWebRoutes(opts),
+  ];
+}