Unable to pass the Step number 6

I’m stuck on Stage #MG5(change to your stage, ex. #JM3).
I’ve tried manually adding all the paths for the commands but every time running the tests is checking for new commands which is not there in my path database. I’m not able to use the path module and fs module functionalities as it’s looking only for Windows file path for the command, my system is not POSIX Compliant(mention what you’ve tried so far).

Here are my logs:

include relevant logs here (please make sure to keep the backticks around this!)

And here’s a snippet of my code:


const readline = require("readline");

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  prompt:"$ "
});

// Uncomment this block to pass the first stage


rl.prompt()
rl.on('line',(input)=>{
    input=input.trim();
    execCmd(input)
    rl.prompt()
})

let CMDS=["echo","exit","type"]
let usr_bin=["ls"]
let _bin=["cat","cp","mkdir"]
let _usr_local_bin=["valid_command"]
let _tmp_foo=["my_exe"]

function execCmd(command){
    const{cmd,args}=getCmd(command)

    if(cmd==="echo")
    {
        console.log(args.join(" "))
    }
    else if(cmd==="exit")
    {
        process.exit(0)
    }
    else if(cmd==="type")
    {
        printType(args[0])
    }
    else{
        console.log(`${cmd}: command not found`)
    }
}



function getCmd(command)
{
    args=command.split(/\s+/)
    cmd=args[0]
    args.shift()

    return {cmd,args}
}

function printType(cmdName)
{   
    let found=false;
    if(CMDS.includes(cmdName))
    {
        console.log(`${cmdName} is a shell builtin`)
        found=true
    }
    
    else if(usr_bin.includes(cmdName))
    {   
        console.log(`${cmdName} is /usr/bin/${cmdName}`)
        found=true
    }

    else if(_usr_local_bin.includes(cmdName))
    {   
          console.log(`${cmdName} is /usr/bin/${cmdName}`)
          found=true
    }

    else if(_bin.includes(cmdName))
    {
      console.log(`${cmdName} is /bin/${cmdName}`)
      found=true
    }

    else if(_tmp_foo.includes(cmdName))
    {
      console.log(`${cmdName} is tmp/foo/${cmdName}`)
      found=true
    }


    if(!found)
    {
        console.log(`${cmdName}: not found`)
    }
}
  
include relevant code here (please make sure to keep the backticks around this!)

Hey @the-specs-coder, could you upload your code to GitHub and share the link? It will be much easier to debug if I can run it directly.

Sure. Here is the link GitHub - the-specs-coder/Shell-Code

I’ve tried manually adding all the paths for the commands

@the-specs-coder Hardcoding the paths isn’t the right approach.

Instead, you’ll want to check if the executables exist in PATH and resolve it dynamically.

but every time running the tests is checking for new commands which is not there in my path database

That’s expected. Our test runner introduces randomness to simulate real-world shell behavior.

I’m not able to use the path module and fs module functionalities as it’s looking only for Windows file path for the command, my system is not POSIX Compliant(mention what you’ve tried so far).

You can test your code remotely using our test runner. If you’d like to test locally, we recommend using WSL (Windows Subsystem for Linux).

Let me know if you need further clarification!

Closing this thread due to inactivity. If you still need assistance, feel free to reopen or start a new discussion!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.