« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/search.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/search.js')
-rw-r--r--src/search.js45
1 files changed, 28 insertions, 17 deletions
diff --git a/src/search.js b/src/search.js
index 9b5d2d7a..4a711b23 100644
--- a/src/search.js
+++ b/src/search.js
@@ -10,21 +10,8 @@ import Thing from '#thing';
 
 import {makeSearchIndexes} from './util/searchSchema.js';
 
-export async function writeSearchIndex({
-  wikiCachePath,
-  wikiData,
-}) {
-  if (!wikiCachePath) {
-    throw new Error(`Expected wikiCachePath to write into`);
-  }
-
-  // Basic flow is:
-  // 1. Define schema for type
-  // 2. Add documents to index
-  // 3. Save index to exportable json
-
-  const indexes = makeSearchIndexes(FlexSearch);
-
+async function populateSearchIndexes(indexes, wikiData) {
+  // Albums, tracks
   for (const album of wikiData.albumData) {
     indexes.albums.add({
       reference: Thing.getReference(album),
@@ -35,8 +22,8 @@ export async function writeSearchIndex({
     for (const track of album.tracks) {
       indexes.tracks.add({
         reference: Thing.getReference(track),
-        album: album.name,
         name: track.name,
+        album: album.name,
 
         artists: [
           track.artistContribs.map(contrib => contrib.artist.name),
@@ -49,6 +36,7 @@ export async function writeSearchIndex({
     }
   }
 
+  // Artists
   for (const artist of wikiData.artistData) {
     if (artist.isAlias) {
       continue;
@@ -60,7 +48,8 @@ export async function writeSearchIndex({
     });
   }
 
-  // Export indexes to json
+
+async function exportIndexesToJson(indexes) {
   const searchData = {};
 
   // Map each index to an export promise, and await all.
@@ -73,6 +62,28 @@ export async function writeSearchIndex({
         });
       }));
 
+  return searchData;
+}
+
+export async function writeSearchJson({
+  wikiCachePath,
+  wikiData,
+}) {
+  if (!wikiCachePath) {
+    throw new Error(`Expected wikiCachePath to write into`);
+  }
+
+  // Basic flow is:
+  // 1. Define schema for type
+  // 2. Add documents to index
+  // 3. Save index to exportable json
+
+  const indexes = makeSearchIndexes(FlexSearch);
+
+  await populateSearchIndexes(indexes, wikiData);
+
+  const searchData = await exportIndexesToJson(indexes);
+
   const outputDirectory =
     path.join(wikiCachePath, 'search');