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

75 lines
2.1 KiB
TypeScript
Raw Normal View History

import * as semver from "semver";
import {Persistence} from "./Persistence";
import {addOnLoad, ExpectedGoodbyeError, q} 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
}
}
/**
* Compares version numbers to ensure no compatibility errors ensure.
*/
addOnLoad(() => {
const userVersion = Persistence.getVersion();
const latestVersion = "%%VERSION_NUMBER%%";
if (semver.lt(userVersion, latestVersion)) {
Persistence.reset();
Persistence.setWasUpdated(true); // Message is displayed after reload
location.reload(true);
throw new ExpectedGoodbyeError("Goodbye");
}
if (Persistence.getWasUpdated()) {
q("#terminalOutput").innerHTML = "" +
"<span style=\"color:red\">This website 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".
*/
2019-10-27 01:22:23 +02:00
addOnLoad(() => {
if (Persistence.getPoweroff()) {
q("#terminalOutput").innerText = "Could not connect to fwdekker.com. Retrying in 10 seconds.";
setTimeout(() => location.reload(), 10000);
throw new ExpectedGoodbyeError("Goodbye");
}
});
/**
* Initializes the application.
*/
addOnLoad(() => {
2019-10-27 01:22:23 +02:00
window.terminal = new Terminal(
q("#terminal"),
q("#terminalInputField"),
2019-10-27 01:22:23 +02:00
q("#terminalOutput"),
q("#terminalInputPrefix"),
q("#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
2019-10-31 23:22:37 +01:00
// @ts-ignore: Ugly hack to execute it anyway
if (window.terminal.shell.environment.get("user") !== "")
window.execute("ls");
2019-10-27 01:22:23 +02:00
});