| 2 | import * as bin from './bin'; |
| 3 | |
| 4 | export const shell = async ( |
| 5 | command: string, |
| 6 | setHistory: (value: string) => void, |
| 7 | clearHistory: () => void, |
| 8 | setCommand: React.Dispatch<React.SetStateAction<string>>, |
| 9 | ) => { |
| 10 | const args = command.split(' '); |
| 11 | args[0] = args[0].toLowerCase(); |
| 12 | |
| 13 | if (args[0] === 'clear') { |
| 14 | clearHistory(); |
| 15 | } else if (command === '') { |
| 16 | setHistory(''); |
| 17 | } else if (Object.keys(bin).indexOf(args[0]) === -1) { |
| 18 | setHistory( |
| 19 | `shell: command not found: ${args[0]}. Try 'help' to get started.`, |
| 20 | ); |
| 21 | } else { |
| 22 | const output = await bin[args[0]](args.slice(1)); |
| 23 | setHistory(output); |
| 24 | } |
| 25 | |
| 26 | setCommand(''); |
| 27 | }; |