145 lines
3.3 KiB
JavaScript

import { Codex } from "@codex-storage/sdk-js";
import { NodeUploadStategy } from "@codex-storage/sdk-js/node";
export class MainMenu {
constructor(
uiService,
menuLoop,
installMenu,
configMenu,
installer,
processControl,
codexApp,
) {
this.ui = uiService;
this.loop = menuLoop;
this.installMenu = installMenu;
this.configMenu = configMenu;
this.installer = installer;
this.processControl = processControl;
this.codexApp = codexApp;
this.loop.initialize(this.promptMainMenu);
}
show = async () => {
this.ui.showLogo();
await this.loop.showLoop();
};
promptMainMenu = async () => {
if ((await this.processControl.getNumberOfCodexProcesses()) > 0) {
await this.showRunningMenu();
} else {
if (await this.installer.isCodexInstalled()) {
await this.showNotRunningMenu();
} else {
await this.showNotInstalledMenu();
}
}
};
showNotInstalledMenu = async () => {
await this.ui.askMultipleChoice("Codex is not installed", [
{
label: "Install Codex",
action: this.installMenu.show,
},
{
label: "Exit",
action: this.loop.stopLoop,
},
]);
};
showRunningMenu = async () => {
await this.ui.askMultipleChoice("Codex is running", [
{
label: "Open Codex app",
action: this.codexApp.openCodexApp,
},
{
label: "Stop Codex",
action: this.stopCodex,
},
{
label: "DoThing",
action: this.doThing,
},
{
label: "Exit (Codex keeps running)",
action: this.loop.stopLoop,
},
]);
};
showNotRunningMenu = async () => {
await this.ui.askMultipleChoice("Codex is installed but not running", [
{
label: "Start Codex",
action: this.startCodex,
},
{
label: "Edit Codex config",
action: this.configMenu.show,
},
{
label: "Uninstall Codex",
action: this.installMenu.show,
},
{
label: "Exit",
action: this.loop.stopLoop,
},
]);
};
startCodex = async () => {
const spinner = this.ui.createAndStartSpinner("Starting...");
try {
await this.processControl.startCodexProcess();
this.ui.stopSpinnerSuccess(spinner);
} catch (exception) {
this.ui.stopSpinnerError(spinner);
this.ui.showErrorMessage(`Failed to start Codex. "${exception}"`);
}
};
stopCodex = async () => {
const spinner = this.ui.createAndStartSpinner("Stopping...");
try {
await this.processControl.stopCodexProcess();
this.ui.stopSpinnerSuccess(spinner);
} catch (exception) {
this.ui.stopSpinnerError(spinner);
this.ui.showErrorMessage(`Failed to stop Codex. "${exception}"`);
}
};
doThing = async () => {
console.log("A!");
const codex = new Codex("http://localhost:8080");
const data = codex.data;
const stategy = new NodeUploadStategy("Hello World !");
const uploadResponse = data.upload(stategy);
const res = await uploadResponse.result;
if (res.error) {
console.error(res.data);
return;
}
console.info("CID is", res.data);
const cid = res.data;
const result = await data.networkDownloadStream(cid);
console.log("download: " + JSON.stringify(result));
}
}