« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/gen-thumbs.js23
-rwxr-xr-xsrc/upd8.js5
-rw-r--r--src/util/cli.js24
3 files changed, 28 insertions, 24 deletions
diff --git a/src/gen-thumbs.js b/src/gen-thumbs.js
index 6c82761f..3ccd8ce2 100644
--- a/src/gen-thumbs.js
+++ b/src/gen-thumbs.js
@@ -163,6 +163,7 @@ import {
 import dimensionsOf from 'image-size';
 
 import CacheableObject from '#cacheable-object';
+import {stringifyCache} from '#cli';
 import {commandExists, isMain, promisifyProcess, traverse} from '#node-utils';
 import {sortByName} from '#sort';
 
@@ -346,28 +347,6 @@ export function getThumbnailsAvailableForDimensions([width, height]) {
   ];
 }
 
-function stringifyCache(cache) {
-  if (Object.keys(cache).length === 0) {
-    return `{}`;
-  }
-
-  const entries = Object.entries(cache);
-  sortByName(entries, {getName: entry => entry[0]});
-
-  return [
-    `{`,
-    entries
-      .map(([key, value]) => [JSON.stringify(key), JSON.stringify(value)])
-      .map(([key, value]) => `${key}: ${value}`)
-      .map((line, index, array) =>
-        (index < array.length - 1
-          ? `${line},`
-          : line))
-      .map(line => `  ${line}`),
-    `}`,
-  ].flat().join('\n');
-}
-
 getThumbnailsAvailableForDimensions.all =
   Object.entries(thumbnailSpec)
     .map(([name, {size}]) => [name, size])
diff --git a/src/upd8.js b/src/upd8.js
index 3628bff5..306715dd 100755
--- a/src/upd8.js
+++ b/src/upd8.js
@@ -42,6 +42,7 @@ import wrap from 'word-wrap';
 
 import {mapAggregate, openAggregate, showAggregate} from '#aggregate';
 import CacheableObject from '#cacheable-object';
+import {stringifyCache} from '#cli';
 import {displayCompositeCacheAnalysis} from '#composite';
 import find, {bindFind, getAllFindSpecs} from '#find';
 import {processLanguageFile, watchLanguageFile, internalDefaultStringsFile}
@@ -2076,7 +2077,7 @@ async function main() {
     }
 
     try {
-      await writeFile(cacheFile, JSON.stringify(onlineThumbsCache ?? {}));
+      await writeFile(cacheFile, stringifyCache(onlineThumbsCache));
     } catch (caughtError) {
       writeError = caughtError;
     }
@@ -2133,7 +2134,7 @@ async function main() {
 
     if (onlineThumbsCache && !writeError) {
       try {
-        await writeFile(cacheFile, JSON.stringify(onlineThumbsCache));
+        await writeFile(cacheFile, stringifyCache(onlineThumbsCache));
       } catch (error) {
         console.error(error);
         logWarn`There was an error saving a local copy of the`;
diff --git a/src/util/cli.js b/src/util/cli.js
index 5d314dc4..be59b35e 100644
--- a/src/util/cli.js
+++ b/src/util/cli.js
@@ -493,3 +493,27 @@ export async function logicalPathTo(target) {
   const cwd = await logicalCWD();
   return relative(cwd, target);
 }
+
+export function stringifyCache(cache) {
+  cache ??= {};
+
+  if (Object.keys(cache).length === 0) {
+    return `{}`;
+  }
+
+  const entries = Object.entries(cache);
+  sortByName(entries, {getName: entry => entry[0]});
+
+  return [
+    `{`,
+    entries
+      .map(([key, value]) => [JSON.stringify(key), JSON.stringify(value)])
+      .map(([key, value]) => `${key}: ${value}`)
+      .map((line, index, array) =>
+        (index < array.length - 1
+          ? `${line},`
+          : line))
+      .map(line => `  ${line}`),
+    `}`,
+  ].flat().join('\n');
+}