From c81232637eb16e5bf6e811676636c29691718a5c Mon Sep 17 00:00:00 2001 From: thatben Date: Thu, 20 Feb 2025 10:04:58 +0100 Subject: [PATCH] debugging path selection --- src/main.js | 15 --------- src/utils/pathSelector.js | 66 +++++++++++++++++++++++++++------------ 2 files changed, 46 insertions(+), 35 deletions(-) diff --git a/src/main.js b/src/main.js index a65c217..deea6a3 100644 --- a/src/main.js +++ b/src/main.js @@ -11,7 +11,6 @@ import { runCodex, checkNodeStatus } from './handlers/nodeHandlers.js'; import { showInfoMessage } from './utils/messages.js'; import { loadConfig } from './services/config.js'; import { showConfigMenu } from './configmenu.js'; -import {filesystemSync} from 'fs-filesystem'; async function showNavigationMenu() { console.log('\n') @@ -53,20 +52,6 @@ function handleExit() { } export async function main() { - - const result = filesystemSync(); - console.log('devices', JSON.stringify(result)); - - Object.keys(result).forEach(function(key) { - var val = result[key]; - val.volumes.forEach(function(volume) { - console.log("mounting point: " + volume.mountPoint); - }); - }); - - return; - - const commandArgs = parseCommandLineArgs(); if (commandArgs) { switch (commandArgs.command) { diff --git a/src/utils/pathSelector.js b/src/utils/pathSelector.js index f962e1d..24d9639 100644 --- a/src/utils/pathSelector.js +++ b/src/utils/pathSelector.js @@ -15,15 +15,21 @@ function showMsg(msg) { })); } -// function getAvailableRoots() { -// const platform = os.platform(); -// if (platform === 'win32') { -// const result = await runCommand('for /f "delims=" %a in (\'curl -s --ssl-reqd ip.codex.storage\') do @echo %a'); -// nat = result.trim(); -// } else { -// nat = await runCommand('curl -s https://ip.codex.storage'); -// } -// } +function getAvailableRoots() { + const devices = filesystemSync(); + var mountPoints = []; + Object.keys(devices).forEach(function(key) { + var val = devices[key]; + val.volumes.forEach(function(volume) { + mountPoints.push(volume.mountPoint); + }); + }); + + if (mountPoints.length < 1) { + throw new Error("Failed to detect file system devices."); + } + return mountPoints; +} function splitPath(str) { return str.replaceAll("\\", "/").split("/"); @@ -65,6 +71,19 @@ function showCurrent(currentPath) { } } +function hasValidRoot(roots, checkPath) { + if (checkPath.length < 1) return false; + var result = false; + roots.forEach(function(root) { + if (root.toLowerCase() == checkPath[0].toLowerCase()) { + console.log("valid root: " + combine(checkPath)); + result = true; + } + }); + if (!result) console.log("invalid root: " + combine(checkPath)); + return result; +} + async function showMain(currentPath) { showCurrent(currentPath); const { choice } = await inquirer.prompt([ @@ -92,23 +111,28 @@ async function showMain(currentPath) { } export async function showPathSelector(startingPath, pathMustExist) { + const roots = getAvailableRoots(); var currentPath = splitPath(startingPath); + if (!hasValidRoot(roots, currentPath)) { + currentPath = [roots[0]]; + } while (true) { const choice = await showMain(currentPath); + var newCurrentPath = currentPath; switch (choice.split('.')[0]) { case '1': - currentPath = await enterPath(currentPath, pathMustExist); + newCurrentPath = await enterPath(currentPath, pathMustExist); break; case '2': - currentPath = upOne(currentPath); + newCurrentPath = upOne(currentPath); break; case '3': - currentPath = await downOne(currentPath); + newCurrentPath = await downOne(currentPath); break; case '4': - currentPath = await createSubDir(currentPath, pathMustExist); + newCurrentPath = await createSubDir(currentPath, pathMustExist); break; case '5': if (pathMustExist && !isDir(combine(currentPath))) { @@ -120,6 +144,12 @@ export async function showPathSelector(startingPath, pathMustExist) { case '6': return combine(currentPath); } + + if (hasValidRoot(roots, newCurrentPath)) { + currentPath = newCurrentPath; + } else { + console.log("Selected path has no valid root."); + } } } @@ -158,17 +188,13 @@ function isSubDir(currentPath, entry) { function getSubDirOptions(currentPath) { const fullPath = combine(currentPath); - currentPath.forEach(function(part) { - console.log("part: '" + part + "'"); - }); - console.log("current: '" + fullPath + "'"); const entries = fs.readdirSync(fullPath); var result = []; var counter = 1; entries.forEach(function(entry) { - console.log("entry: " + entry); if (isSubDir(currentPath, entry)) { - result.push(counter + ". " + entry); + result.push(counter + '. ' + entry); + counter = counter + 1; } }); return result; @@ -194,7 +220,7 @@ async function downOne(currentPath) { return currentPath; }); - const subDir = choice.slice(3); + const subDir = choice.split('. ')[1]; return [...currentPath, subDir]; }