2018-11-28 22:07:51 +01:00
|
|
|
class Commands {
|
2018-11-28 22:23:11 +01:00
|
|
|
constructor(terminal, fileSystem) {
|
|
|
|
this._terminal = terminal;
|
2018-11-28 23:22:17 +01:00
|
|
|
this._fs = fileSystem;
|
2018-11-28 22:23:11 +01:00
|
|
|
|
2018-11-28 22:07:51 +01:00
|
|
|
this._list = {
|
|
|
|
clear: {
|
2018-11-28 22:23:11 +01:00
|
|
|
fun: this.clear,
|
2018-11-28 22:07:51 +01:00
|
|
|
summary: `clear terminal output`,
|
|
|
|
usage: `clear`,
|
2018-11-28 23:22:17 +01:00
|
|
|
desc: `Clears all previous terminal output.`
|
2018-11-28 22:07:51 +01:00
|
|
|
},
|
|
|
|
cd: {
|
2018-11-28 22:23:11 +01:00
|
|
|
fun: this.cd,
|
2018-11-28 22:07:51 +01:00
|
|
|
summary: `change directory`,
|
|
|
|
usage: `cd [DIRECTORY]`,
|
|
|
|
desc: "" +
|
|
|
|
`Changes the current working directory to [DIRECTORY].
|
|
|
|
If [DIRECTORY] is empty, nothing happens.`.trimLines()
|
|
|
|
},
|
|
|
|
echo: {
|
|
|
|
fun: Commands.echo,
|
|
|
|
summary: `display text`,
|
|
|
|
usage: `echo [TEXT]`,
|
|
|
|
desc: `Displays [TEXT].`.trimLines()
|
|
|
|
},
|
|
|
|
exit: {
|
|
|
|
fun: Commands.exit,
|
|
|
|
summary: `close session`,
|
|
|
|
usage: `exit`,
|
2018-11-28 23:22:17 +01:00
|
|
|
desc: `Closes the terminal session.`
|
2018-11-28 22:07:51 +01:00
|
|
|
},
|
|
|
|
help: {
|
|
|
|
fun: this.help,
|
|
|
|
summary: `display documentation`,
|
|
|
|
usage: `help [COMMAND]`,
|
|
|
|
desc: "" +
|
|
|
|
`Displays help documentation for [COMMAND].
|
|
|
|
If [COMMAND] is empty, a list of all commands is shown.`.trimLines()
|
|
|
|
},
|
|
|
|
ls: {
|
|
|
|
fun: this.ls,
|
|
|
|
summary: `list directory contents`,
|
|
|
|
usage: `ls [DIRECTORY]`,
|
|
|
|
desc: "" +
|
|
|
|
`Displays the files and directories in [DIRECTORY].
|
|
|
|
If [DIRECTORY] is empty, the files and directories in the current working directory are shown.`.trimLines()
|
|
|
|
},
|
|
|
|
mkdir: {
|
2018-11-28 22:23:11 +01:00
|
|
|
fun: this.mkdir,
|
2018-11-28 22:07:51 +01:00
|
|
|
summary: `create directory`,
|
|
|
|
usage: `mkdir [DIRECTORY]`,
|
2018-11-28 23:22:17 +01:00
|
|
|
desc: `Creates a directory with name [DIRECTORY].`
|
|
|
|
},
|
|
|
|
poweroff: {
|
|
|
|
fun: Commands.poweroff,
|
|
|
|
summary: `power off machine`,
|
|
|
|
usage: `poweroff`,
|
|
|
|
desc: `Shuts down this machine.`
|
2018-11-28 22:07:51 +01:00
|
|
|
},
|
|
|
|
pwd: {
|
2018-11-28 22:23:11 +01:00
|
|
|
fun: this.pwd,
|
2018-11-28 22:07:51 +01:00
|
|
|
summary: `print working directory`,
|
|
|
|
usage: `pwd`,
|
2018-11-28 23:22:17 +01:00
|
|
|
desc: `Displays the current working directory.`
|
2018-11-28 22:07:51 +01:00
|
|
|
},
|
|
|
|
rm: {
|
2018-11-28 22:23:11 +01:00
|
|
|
fun: this.rm,
|
2018-11-28 22:07:51 +01:00
|
|
|
summary: `remove file`,
|
|
|
|
usage: `rm [-f | --force] FILE`,
|
2018-11-28 23:22:17 +01:00
|
|
|
desc: `Removes FILE if it is a file.`
|
2018-11-28 22:07:51 +01:00
|
|
|
},
|
|
|
|
rmdir: {
|
2018-11-28 22:23:11 +01:00
|
|
|
fun: this.rmdir,
|
2018-11-28 22:07:51 +01:00
|
|
|
summary: `remove directory`,
|
|
|
|
usage: `rmdir [-f | --force] DIR`,
|
2018-11-28 23:22:17 +01:00
|
|
|
desc: `Removes DIR if it is a directory.`
|
2018-11-28 22:07:51 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2018-11-28 19:51:48 +01:00
|
|
|
|
|
|
|
|
2018-11-28 22:07:51 +01:00
|
|
|
parse(input) {
|
2018-11-28 22:41:59 +01:00
|
|
|
const args = input.split(" ");
|
|
|
|
const command = (args[0] || "").toLowerCase();
|
2018-11-28 19:51:48 +01:00
|
|
|
|
2018-11-28 22:07:51 +01:00
|
|
|
if (Object.keys(this._list).indexOf(command) >= 0) {
|
|
|
|
return this._list[command].fun.bind(this)(args);
|
2018-11-28 22:41:59 +01:00
|
|
|
} else if (command.trim() === "") {
|
|
|
|
return "";
|
2018-11-28 22:07:51 +01:00
|
|
|
} else {
|
|
|
|
return `Unknown command '${args[0]}'`
|
|
|
|
}
|
|
|
|
}
|
2018-11-28 19:51:48 +01:00
|
|
|
|
|
|
|
|
2018-11-28 22:23:11 +01:00
|
|
|
cd(args) {
|
2018-11-28 23:22:17 +01:00
|
|
|
return this._fs.cd(args[1]);
|
2018-11-28 22:07:51 +01:00
|
|
|
}
|
2018-11-28 19:51:48 +01:00
|
|
|
|
2018-11-28 22:23:11 +01:00
|
|
|
clear() {
|
|
|
|
this._terminal.clear();
|
2018-11-28 22:41:59 +01:00
|
|
|
return "";
|
2018-11-28 22:07:51 +01:00
|
|
|
}
|
2018-11-28 19:51:48 +01:00
|
|
|
|
2018-11-28 22:07:51 +01:00
|
|
|
static echo(args) {
|
|
|
|
return args
|
2018-11-28 22:41:59 +01:00
|
|
|
.slice(1).join(" ")
|
|
|
|
.replace("hunter2", "*******");
|
2018-11-28 22:07:51 +01:00
|
|
|
}
|
2018-11-28 19:51:48 +01:00
|
|
|
|
2018-11-28 22:07:51 +01:00
|
|
|
static exit() {
|
|
|
|
terminal.reset();
|
2018-11-28 22:41:59 +01:00
|
|
|
return "";
|
2018-11-28 22:07:51 +01:00
|
|
|
}
|
2018-11-28 19:51:48 +01:00
|
|
|
|
2018-11-28 22:07:51 +01:00
|
|
|
help(args) {
|
2018-11-28 22:41:59 +01:00
|
|
|
const command = (args[1] || "").toLowerCase();
|
2018-11-28 22:07:51 +01:00
|
|
|
const commandNames = Object.keys(this._list);
|
2018-11-28 19:51:48 +01:00
|
|
|
|
2018-11-28 22:07:51 +01:00
|
|
|
if (commandNames.indexOf(command) >= 0) {
|
|
|
|
const info = this._list[command];
|
2018-11-28 19:51:48 +01:00
|
|
|
|
2018-11-28 22:07:51 +01:00
|
|
|
return "" +
|
|
|
|
`${command} - ${info.summary}
|
2018-11-28 19:51:48 +01:00
|
|
|
|
2018-11-28 22:07:51 +01:00
|
|
|
<b>Usage</b>
|
|
|
|
${info.usage}
|
2018-11-28 19:51:48 +01:00
|
|
|
|
2018-11-28 22:07:51 +01:00
|
|
|
<b>Description</b>
|
|
|
|
${info.desc}`.trimLines();
|
|
|
|
} else {
|
|
|
|
const commandWidth = Math.max.apply(null, commandNames.map(it => it.length)) + 4;
|
|
|
|
const commandEntries = commandNames.map(
|
|
|
|
it => `${it.padEnd(commandWidth, ' ')}${this._list[it].summary}`
|
|
|
|
);
|
2018-11-28 19:51:48 +01:00
|
|
|
|
2018-11-28 22:07:51 +01:00
|
|
|
return "" +
|
|
|
|
`<b>List of commands</b>
|
2018-11-28 22:41:59 +01:00
|
|
|
${commandEntries.join("\n")}
|
2018-11-28 19:51:48 +01:00
|
|
|
|
2018-11-28 22:07:51 +01:00
|
|
|
Write "help [COMMAND]" for more information on a command.`.trimLines();
|
|
|
|
}
|
2018-11-28 19:51:48 +01:00
|
|
|
}
|
|
|
|
|
2018-11-28 22:07:51 +01:00
|
|
|
ls(args) {
|
2018-11-28 23:22:17 +01:00
|
|
|
const files = this._fs.ls(args[1]);
|
2018-11-28 22:07:51 +01:00
|
|
|
if (files === undefined) {
|
|
|
|
return `The directory '${args[1]}' does not exist`;
|
|
|
|
}
|
2018-11-28 19:51:48 +01:00
|
|
|
|
2018-11-28 22:07:51 +01:00
|
|
|
const dirList = [];
|
|
|
|
const fileList = [];
|
2018-11-28 19:51:48 +01:00
|
|
|
|
2018-11-28 22:07:51 +01:00
|
|
|
Object.keys(files).sort().forEach(fileIndex => {
|
|
|
|
const file = files[fileIndex];
|
|
|
|
|
2018-11-28 22:41:59 +01:00
|
|
|
if (typeof file === "string") {
|
2018-11-28 22:07:51 +01:00
|
|
|
fileList.push(file);
|
|
|
|
} else {
|
|
|
|
dirList.push(`${fileIndex}/`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-11-28 22:41:59 +01:00
|
|
|
return dirList.concat(fileList).join("\n");
|
2018-11-28 19:51:48 +01:00
|
|
|
}
|
|
|
|
|
2018-11-28 22:23:11 +01:00
|
|
|
mkdir(args) {
|
2018-11-28 23:22:17 +01:00
|
|
|
return this._fs.mkdir(args[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static poweroff() {
|
|
|
|
const date = new Date();
|
|
|
|
date.setSeconds(date.getSeconds() + 30);
|
|
|
|
document.cookie = `poweroff=true; expires=${date.toUTCString()}; path=/`;
|
|
|
|
|
|
|
|
setTimeout(() => location.reload(), 2000);
|
|
|
|
return "" +
|
|
|
|
`Shutdown NOW!
|
|
|
|
|
|
|
|
*** FINAL System shutdown message from felix@fwdekker.com ***
|
|
|
|
|
|
|
|
System going down IMMEDIATELY
|
|
|
|
|
|
|
|
|
|
|
|
System shutdown time has arrived`.trimLines();
|
2018-11-28 19:51:48 +01:00
|
|
|
}
|
|
|
|
|
2018-11-28 22:23:11 +01:00
|
|
|
pwd() {
|
2018-11-28 23:22:17 +01:00
|
|
|
return this._fs.pwd;
|
2018-11-28 22:07:51 +01:00
|
|
|
}
|
2018-11-28 19:51:48 +01:00
|
|
|
|
2018-11-28 22:23:11 +01:00
|
|
|
rm(args) {
|
2018-11-28 23:22:17 +01:00
|
|
|
return this._fs.rm(args[1]);
|
2018-11-28 22:07:51 +01:00
|
|
|
}
|
2018-11-28 19:51:48 +01:00
|
|
|
|
2018-11-28 22:23:11 +01:00
|
|
|
rmdir(args) {
|
2018-11-28 22:07:51 +01:00
|
|
|
let path;
|
|
|
|
let force;
|
2018-11-28 22:41:59 +01:00
|
|
|
if (args[1] === "-f" || args[1] === "--force") {
|
2018-11-28 22:07:51 +01:00
|
|
|
path = args[2];
|
|
|
|
force = true;
|
|
|
|
} else {
|
|
|
|
path = args[1];
|
|
|
|
force = false;
|
|
|
|
}
|
|
|
|
|
2018-11-28 23:22:17 +01:00
|
|
|
return this._fs.rmdir(path, force);
|
2018-11-28 19:51:48 +01:00
|
|
|
}
|
2018-11-28 22:07:51 +01:00
|
|
|
}
|