-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.js
More file actions
81 lines (65 loc) · 1.61 KB
/
Copy pathshell.js
File metadata and controls
81 lines (65 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const currentDirectory = ['~'];
const directories = [];
const shellName = 'shell v2.0 ';
const symbol = ' % ';
const listOfFiles = [];
const cd = function (args) {
if (directories.includes(args.slice(-1).join( ))) {
currentDirectory.push(args.join(' '));
const path = shellName + (currentDirectory.join('/') )+ symbol;
return path;
}
console.log('No such directories');
};
const pwd = function () {
return currentDirectory;
};
const ls = function () {
console.log(listOfFiles.join(' '));
return;
};
const touch = function (args) {
const newFile = args.join();
listOfFiles.push(newFile);
};
const makedirectory = function (args) {
if (args.includes(' ')) {
console.log('use camelcase');
return;
}
directories.push(args.join());
}
const externalCommands = function (commandString) {
const [command, ...args] = commandString.split(' ');
switch (command) {
case 'touch':
return touch(args);
case 'mkdir':
return makedirectory(args);
default:
console.log('shell v2.0 command not found');
}
};
const echo = function (args) {
console.log(args.join(' '));
};
const runCommand = function (commandString) {
const [command, ...args] = commandString.split(' ');
switch (command) {
case 'cd':
return cd(args);
case 'pwd':
console.log(pwd());
return;
case 'ls':
return ls();
case 'echo':
return echo(args);
default:
return externalCommands(commandString);
}
};
while (true) {
const command = prompt(shellName + currentDirectory.join('/') + symbol);
const runningCommand = runCommand(command);
}