import { createInterface } from "readline";
import { existsSync } from "fs";
const rl = createInterface({
input: process.stdin,
output: process.stdout,
prompt: '$ '
});
const commands = ['echo', 'exit', 'type'];
const paths = process.env["PATH"]?.split(":") || [];
rl.prompt();
const type = (input: string) => {
if (commands.includes(input)) {
console.log(`${input} is a shell builtin`);
} else {
for (const p of paths) {
const filePath = `${p}/${input}`
if (existsSync(filePath)) {
console.log(`${input} is ${filePath}`);
return;
}
}
console.log(`${input}: not found`);
}
}
const main = () => {
rl.on('line', (command) => {
if (command.trim() === 'exit 0') {
process.exit(0);
}
if (command.startsWith("echo ")) {
console.log(command.substring(5))
} else if (command.startsWith("type ")) {
type(command.substring(5));
} else {
console.log(`${command}: command not found`)
}
rl.prompt();
})
}
main();
remote: [your-program] $ type my_exe
remote: [your-program] my_exe is /tmp/bar/my_exe
remote: [tester::#MG5] ^ Line does not match expected value.
remote: [tester::#MG5] Expected: “my_exe is /tmp/baz/my_exe”
remote: [tester::#MG5] Received: “my_exe is /tmp/bar/my_exe”
remote: [your-program] $
remote: [tester::#MG5] Assertion failed.
remote: [tester::#MG5] Test failed
and When I reverse the array
error is
remote: [your-program] my_exe is /tmp/qux/my_exe
remote: [tester::#MG5] ^ Line does not match expected value.
remote: [tester::#MG5] Expected: “my_exe is /tmp/baz/my_exe”
remote: [tester::#MG5] Received: “my_exe is /tmp/qux/my_exe”
remote: [your-program] $
remote: [tester::#MG5] Assertion failed.
remote: [tester::#MG5] Test failed
