« get me outta code hell

let gen-thumbs be run directly - 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>2022-02-27 11:14:54 -0400
committer(quasar) nebula <qznebula@protonmail.com>2022-02-27 11:14:54 -0400
commit7ffc79f9c891becdcf778ab6e5faf4c8ca3b14da (patch)
treec20e14d8de7802a5fe0e6b45be15a250f0c7495b
parent1d5078c972c8b2f625aa341cf75cead82323f909 (diff)
let gen-thumbs be run directly
-rw-r--r--src/gen-thumbs.js27
-rw-r--r--src/util/node-utils.js10
2 files changed, 37 insertions, 0 deletions
diff --git a/src/gen-thumbs.js b/src/gen-thumbs.js
index d636d2fd..e6dfeae7 100644
--- a/src/gen-thumbs.js
+++ b/src/gen-thumbs.js
@@ -100,6 +100,7 @@ import {
 } from './util/cli.js';
 
 import {
+    isMain,
     promisifyProcess,
 } from './util/node-utils.js';
 
@@ -304,3 +305,29 @@ export default async function genThumbs(mediaPath, {
 
     return true;
 }
+
+if (isMain(import.meta.url)) {
+    (async function() {
+        const miscOptions = await parseOptions(process.argv.slice(2), {
+            'media-path': {
+                type: 'value'
+            },
+            'queue-size': {
+                type: 'value',
+                validate(size) {
+                    if (parseInt(size) !== parseFloat(size)) return 'an integer';
+                    if (parseInt(size) < 0) return 'a counting number or zero';
+                    return true;
+                }
+            },
+            queue: {alias: 'queue-size'},
+        });
+
+        const mediaPath = miscOptions['media-path'] || process.env.HSMUSIC_MEDIA;
+        const queueSize = +(miscOptions['queue-size'] ?? 0);
+
+        await genThumbs(mediaPath, {queueSize});
+    })().catch(err => {
+        console.error(err);
+    });
+}
diff --git a/src/util/node-utils.js b/src/util/node-utils.js
index d660612e..a46d6141 100644
--- a/src/util/node-utils.js
+++ b/src/util/node-utils.js
@@ -1,5 +1,7 @@
 // Utility functions which are only relevant to particular Node.js constructs.
 
+import { fileURLToPath } from 'url';
+
 // Very cool function origin8ting in... http-music pro8a8ly!
 // Sorry if we happen to 8e violating past-us's copyright, lmao.
 export function promisifyProcess(proc, showLogging = true) {
@@ -25,3 +27,11 @@ export function promisifyProcess(proc, showLogging = true) {
         })
     })
 }
+
+// Handy-dandy utility function for detecting whether the passed URL is the
+// running JavaScript file. This takes `import.meta.url` from ES6 modules, which
+// is great 'cuz (module === require.main) doesn't work without CommonJS
+// modules.
+export function isMain(importMetaURL) {
+    return (process.argv[1] === fileURLToPath(importMetaURL));
+}