frontpage/lib/commands/definitions.ts

202 lines
7.0 KiB
TypeScript
Raw Normal View History

2021-12-17 18:55:00 +01:00
import type { Command, Flag } from "./types";
2021-12-16 00:21:14 +01:00
2021-12-17 19:04:34 +01:00
function getCommandByName(name: string): Command|undefined {
return commandList.find(cmd => cmd.name === name);
}
2021-12-16 00:21:14 +01:00
function illegalUse(raw: string, cmd: Command): string[] {
return [
"Syntax error!",
`Cannot parse "${raw}"`,
""
].concat(printSyntax(cmd));
}
function checkFlags(flags: string[], cmd: Command): boolean {
if (!flags || flags === []) return true;
if (!cmd.flags) return false;
for (const flag of flags) {
const isLong = flag.substring(0,2) === "--";
2021-12-17 20:02:42 +01:00
const flagObj = Object.values(cmd.flags).find(f => isLong ? f.long === flag.substring(2) : f.short === flag.substring(1));
2021-12-16 00:21:14 +01:00
if (!flagObj) return false;
}
return true;
}
function checkSubcmd(subcmds: string[], cmd: Command): boolean {
if (!subcmds || subcmds === []) return true;
if (!cmd.subcommands) return false;
for (const sc of subcmds) {
2021-12-17 20:02:42 +01:00
const flagObj = Object.values(cmd.subcommands).find(s => s.name === sc);
2021-12-16 00:21:14 +01:00
if (!flagObj) return false;
}
return true;
}
2021-12-17 18:55:00 +01:00
function checkFlagInclude(flagsProvided: string[], flag: Flag): boolean {
2021-12-17 20:02:42 +01:00
if (!flag) return false;
2021-12-17 18:55:00 +01:00
return flagsProvided.includes(`-${flag.short}`) || flagsProvided.includes(`--${flag.long}`);
}
2021-12-16 00:21:14 +01:00
export function printSyntax(cmd: Command): string[] {
let flagsOption = "";
let flagsDesc = [];
2021-12-17 20:02:42 +01:00
if (cmd.flags && Object.keys(cmd.flags).length > 0) {
2021-12-16 00:21:14 +01:00
flagsOption = " [";
2021-12-16 01:03:54 +01:00
flagsDesc.push("");
2021-12-16 00:21:14 +01:00
flagsDesc.push("Flags:");
2021-12-17 20:02:42 +01:00
Object.values(cmd.flags).forEach((flag => {
2021-12-16 00:21:14 +01:00
flagsOption += `-${flag.short} `;
flagsDesc.push(`\t-${flag.short}\t--${flag.long}\t${flag.desc}`);
}));
flagsOption = flagsOption.substring(0, flagsOption.length-1) + "]";
}
let subcmdOption = "";
let subcmdDesc = [];
2021-12-17 20:02:42 +01:00
if (cmd.subcommands && Object.keys(cmd.subcommands).length > 0) {
2021-12-16 00:21:14 +01:00
subcmdOption = " [";
2021-12-16 01:03:54 +01:00
subcmdDesc.push("");
2021-12-17 19:04:34 +01:00
subcmdDesc.push("Arguments:");
2021-12-17 20:02:42 +01:00
Object.values(cmd.subcommands).forEach((subcmd => {
2021-12-16 00:21:14 +01:00
subcmdOption += `${subcmd.name}|`;
2021-12-17 20:02:42 +01:00
subcmdDesc.push(`\t${subcmd.name}\t${subcmd.desc}`);
2021-12-16 00:21:14 +01:00
}));
subcmdOption = subcmdOption.substring(0, subcmdOption.length-1) + "]";
}
2021-12-16 01:03:54 +01:00
return [`Usage: ${cmd.name}${flagsOption}${subcmdOption}`].concat(flagsDesc).concat(subcmdDesc);
2021-12-16 00:21:14 +01:00
}
const about: Command = {
name: "about",
desc: "Show information about this page.",
2021-12-17 20:02:42 +01:00
execute: () => {
2021-12-16 00:21:14 +01:00
return [
"Hello there wanderer.",
"So you want to know what this is about?",
"",
"Well, the answer is pretty unspectecular:",
"This site presents some stuff that the user named C0ntroller created.",
"If you wander arround you will find various projects.",
"",
"The navigation is done via this console interface.",
"Even when you open a project page you don't need your mouse - just press Esc to close it.",
"",
"I hope you enjoy your stay here!",
2021-12-26 16:17:40 +01:00
"If you wanted more information about the page itself, type %{project this} or %{help -t}."
2021-12-16 00:21:14 +01:00
];
}
};
2021-12-17 18:55:00 +01:00
const help: Command = {
name: "help",
desc: "Shows helptext.",
2021-12-17 20:02:42 +01:00
flags: {more: { long: "this", short: "t", desc: "Show information about this site." }},
execute: (flags) => {
if (help.flags && checkFlagInclude(flags, help.flags.more)) {
2021-12-17 18:55:00 +01:00
return [
"Hello user!",
"What you see here should resemble an CLI. If you ever used Linux this should be pretty easy for you.",
"Everyone else: Have no fear. It is pretty simple. You just type in commands and the output is shown here or it does something on the webite.",
"To find out, which commands are available, you can type just 'help'.",
"",
"Have fun!"
];
} else {
const available = ["Available commands:"];
commandList.forEach(cmd => available.push(`\t${cmd.name}\t${cmd.desc}`));
available.concat([
"",
"Need more help? Type 'help -m'!"
]);
return available;
}
}
};
2021-12-17 19:04:34 +01:00
const man: Command = {
name: "man",
desc: "Provides a manual for a command.",
2021-12-17 20:02:42 +01:00
subcommands: {
command: {name: "command", desc: "Name of a command"}
},
execute: (_flags, args) => {
2021-12-17 19:04:34 +01:00
if (args.length !== 1) {
return printSyntax(man);
} else {
const cmd = getCommandByName(args[0]);
if (!cmd) return [`Cannot find command '${args[0]}'.`];
else return printSyntax(cmd);
}
}
};
2021-12-17 20:02:42 +01:00
const project: Command = {
name: "project",
desc: "Show information about a project.",
flags: {
minimal: {short: "m", long: "minimal", desc: "Only show minimal information."},
source: {short: "s", long: "source", desc: "Open git repository of project."},
list: {short: "l", long: "list", desc: "Show list of projects."}
},
subcommands: {name: {name: "name", desc: "Name of the project."}},
2022-01-14 14:27:19 +01:00
execute: (flags, args, _raw, cmdIf) => {
2021-12-17 20:02:42 +01:00
if (project.flags && checkFlagInclude(flags, project.flags.list)) {
const result = ["Found the following projects:"];
cmdIf.projects.forEach(project => result.push(`\t${project.name}\t${project.short}`));
2021-12-17 20:02:42 +01:00
return result;
}
if (args.length !== 1) return printSyntax(project);
if (args[0] === "this") args[0] = "homepage";
const pjt = cmdIf.projects.find(p => p.name === args[0]);
2021-12-17 20:02:42 +01:00
if (!pjt) return [
`Cannot find project ${args[0]}!`,
"You can see available projects using 'project -l'."
];
if (project.flags && checkFlagInclude(flags, project.flags.source)) {
try {
window && window.open(pjt.repo, "_blank");
return ["Opened repository in new tab."];
} catch {
return ["Sorry, no repository for this project."];
}
}
if (project.flags && checkFlagInclude(flags, project.flags.minimal)) return pjt.desc;
2022-01-14 14:27:19 +01:00
cmdIf.callbacks.setModalProject(args[0]);
cmdIf.callbacks.setModalVisible(true);
2021-12-17 20:02:42 +01:00
return [];
}
};
2021-12-26 14:07:46 +01:00
const exitCmd: Command = {
name: "exit",
2021-12-26 15:37:08 +01:00
desc: "Tries to close this tab.",
2021-12-26 14:07:46 +01:00
execute: () => {
if (typeof window !== undefined) {
window.opener = null;
2022-01-05 17:52:20 +01:00
window.open("", "_self");
2021-12-26 14:07:46 +01:00
window.close();
}
return [
"If you can read this, closing the window did not work.",
2021-12-26 15:37:08 +01:00
"This is most likely because of a restriction in JavaScript.",
"#{Read more here|https://developer.mozilla.org/en-US/docs/Web/API/Window/close}."
2022-01-05 17:52:20 +01:00
];
2021-12-26 14:07:46 +01:00
}
2022-01-05 17:52:20 +01:00
};
2021-12-26 14:07:46 +01:00
2021-12-26 14:17:11 +01:00
const clear: Command = {
name: "clear",
desc: "Clears the output on screen.",
execute: () => []
2022-01-05 17:52:20 +01:00
};
2021-12-26 14:17:11 +01:00
export const commandList = [about, help, man, project, exitCmd, clear];