« get me outta code hell

isMain true if directory of index.js targetted - 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-12-06 08:41:53 -0400
committer(quasar) nebula <qznebula@protonmail.com>2022-12-06 08:41:53 -0400
commit0bc52b8d8fb15c4420e49a2503f82e799a18e20d (patch)
tree2932c1883d0cfcb3086bcffc53166c1e7ee78efe
parentb494fd1a9f4c77994a239ce12b04cafa5ae3315d (diff)
isMain true if directory of index.js targetted
-rw-r--r--src/util/node-utils.js9
1 files changed, 8 insertions, 1 deletions
diff --git a/src/util/node-utils.js b/src/util/node-utils.js
index 252e920a..76684827 100644
--- a/src/util/node-utils.js
+++ b/src/util/node-utils.js
@@ -1,6 +1,7 @@
 // Utility functions which are only relevant to particular Node.js constructs.
 
 import {fileURLToPath} from 'url';
+import * as path from 'path';
 
 import _commandExists from 'command-exists';
 
@@ -45,5 +46,11 @@ export function promisifyProcess(proc, showLogging = true) {
 // is great 'cuz (module === require.main) doesn't work without CommonJS
 // modules.
 export function isMain(importMetaURL) {
-  return process.argv[1] === fileURLToPath(importMetaURL);
+  const metaPath = fileURLToPath(importMetaURL);
+  const relative = path.relative(process.argv[1], metaPath);
+  const isIndexJS = path.basename(metaPath) === 'index.js';
+  return [
+    '',
+    isIndexJS && 'index.js'
+  ].includes(relative);
 }