« get me outta code hell

util: add logicalCWD, logicalPathTo cli functions - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-10-30 18:24:08 -0300
committer(quasar) nebula <qznebula@protonmail.com>2023-10-30 18:24:08 -0300
commitcc6fff8d198953b66ec984248cd1a4e02937b55b (patch)
tree841be09ca352200ff989790962b95706cc1ddf1b /src
parent8978169af6529321e2db6193bc5fe193126e8258 (diff)
util: add logicalCWD, logicalPathTo cli functions
Diffstat (limited to 'src')
-rw-r--r--src/util/cli.js31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/util/cli.js b/src/util/cli.js
index 4c08c085..973fef19 100644
--- a/src/util/cli.js
+++ b/src/util/cli.js
@@ -340,3 +340,34 @@ export function fileIssue({
   console.error(colors.red(`- https://hsmusic.wiki/feedback/`));
   console.error(colors.red(`- https://github.com/hsmusic/hsmusic-wiki/issues/`));
 }
+
+export async function logicalCWD() {
+  if (process.env.PWD) {
+    return process.env.PWD;
+  }
+
+  const {exec} = await import('node:child_process');
+  const {stat} = await import('node:fs/promises');
+
+  try {
+    await stat('/bin/sh');
+  } catch (error) {
+    // Not logical, so sad.
+    return process.cwd();
+  }
+
+  const proc = exec('/bin/pwd -L');
+
+  let output = '';
+  proc.stdout.on('data', buf => { output += buf; });
+
+  await new Promise(resolve => proc.on('exit', resolve));
+
+  return output.trim();
+}
+
+export async function logicalPathTo(target) {
+  const {relative} = await import('node:path');
+  const cwd = await logicalCWD();
+  return relative(cwd, target);
+}