forked from tools/josh
1
0
Fork 0
josh/src/main/js/Main.ts

82 lines
2.3 KiB
TypeScript
Raw Normal View History

import * as semver from "semver";
2021-04-22 16:19:15 +02:00
// @ts-ignore
const {$, doAfterLoad, nav} = window.fwdekker;
import {FileSystem} from "./FileSystem";
import {Persistence} from "./Persistence";
2021-04-22 16:19:15 +02:00
import {ExpectedGoodbyeError} from "./Shared";
2019-11-01 11:59:33 +01:00
import {Terminal} from "./Terminal";
2019-10-27 01:22:23 +02:00
2019-10-31 23:22:37 +01:00
declare global {
interface Window {
/**
* The main terminal.
*/
terminal: Terminal
/**
* Executes a command in the main terminal.
*
* @param command the command to execute
*/
execute: (command: string) => void
}
}
/**
2021-03-31 17:34:32 +02:00
* Compares version numbers to ensure no compatibility errors ensue.
*/
2021-04-22 16:19:15 +02:00
doAfterLoad(() => {
const userVersion = Persistence.getVersion();
const latestVersion = "%%VERSION_NUMBER%%";
if (semver.lt(userVersion, latestVersion)) {
Persistence.reset();
2021-03-31 17:34:32 +02:00
Persistence.setWasUpdated(true); // Message is displayed after reload
location.reload(true);
throw new ExpectedGoodbyeError("Goodbye");
}
if (Persistence.getWasUpdated()) {
2021-04-22 16:19:15 +02:00
$("#terminalOutput").innerHTML = "" +
2021-04-24 22:29:23 +02:00
"<span class=\"errorMessage\">The terminal application has been updated. To prevent unexpected errors, " +
"all previous user changes have been reset.</span>\n\n";
Persistence.setWasUpdated(false);
}
Persistence.setVersion(latestVersion);
});
/**
* Exits the application if the server is "shut down".
*/
2021-04-22 16:19:15 +02:00
doAfterLoad(() => {
2021-03-31 17:34:32 +02:00
if (!Persistence.getPoweroff()) return;
2021-04-22 16:19:15 +02:00
$("#terminalOutput").innerText = "Could not connect to fwdekker.com. Retrying in 10 seconds.";
2021-03-31 17:34:32 +02:00
setTimeout(() => location.reload(), 10000);
throw new ExpectedGoodbyeError("Goodbye");
});
/**
* Initializes the application.
*/
2021-04-22 16:19:15 +02:00
doAfterLoad(async () => {
$("#nav").appendChild(nav("/"));
2021-03-31 17:31:42 +02:00
if (!Persistence.hasFileSystem())
await FileSystem.loadNavApi();
2019-10-27 01:22:23 +02:00
window.terminal = new Terminal(
2021-04-22 16:19:15 +02:00
$("#terminal"),
$("#terminalInputField"),
$("#terminalOutput"),
$("#terminalInputPrefix"),
$("#terminalSuggestions")
2019-10-27 01:22:23 +02:00
);
2019-10-31 23:22:37 +01:00
window.execute = (command: string) => window.terminal.processInput(command);
2019-10-27 01:22:23 +02:00
2021-03-31 17:34:32 +02:00
// @ts-ignore: Ugly hack to check if user is logged in
if (window.terminal.shell.environment.get("user") !== "")
2020-09-30 16:50:45 +02:00
window.execute("ls -l");
2019-10-27 01:22:23 +02:00
});