From 7ffc79f9c891becdcf778ab6e5faf4c8ca3b14da Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sun, 27 Feb 2022 11:14:54 -0400 Subject: let gen-thumbs be run directly --- src/gen-thumbs.js | 27 +++++++++++++++++++++++++++ src/util/node-utils.js | 10 ++++++++++ 2 files changed, 37 insertions(+) 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)); +} -- cgit 1.3.0-6-gf8a5