2025-05-26 10:46:02 +02:00
|
|
|
import { Codex } from "@codex-storage/sdk-js";
|
|
|
|
|
import { NodeUploadStategy } from "@codex-storage/sdk-js/node";
|
|
|
|
|
|
2025-02-25 13:59:26 +01:00
|
|
|
export class MainMenu {
|
2025-04-14 14:31:22 +02:00
|
|
|
constructor(
|
|
|
|
|
uiService,
|
|
|
|
|
menuLoop,
|
|
|
|
|
installMenu,
|
|
|
|
|
configMenu,
|
|
|
|
|
installer,
|
|
|
|
|
processControl,
|
2025-04-15 15:54:46 +02:00
|
|
|
codexApp,
|
2025-04-14 14:31:22 +02:00
|
|
|
) {
|
2025-02-25 13:59:26 +01:00
|
|
|
this.ui = uiService;
|
2025-04-01 14:09:20 +02:00
|
|
|
this.loop = menuLoop;
|
2025-03-07 13:14:32 +01:00
|
|
|
this.installMenu = installMenu;
|
2025-03-31 15:02:37 +02:00
|
|
|
this.configMenu = configMenu;
|
2025-04-14 14:31:22 +02:00
|
|
|
this.installer = installer;
|
|
|
|
|
this.processControl = processControl;
|
2025-04-15 15:54:46 +02:00
|
|
|
this.codexApp = codexApp;
|
2025-04-01 14:09:20 +02:00
|
|
|
|
|
|
|
|
this.loop.initialize(this.promptMainMenu);
|
2025-02-25 13:59:26 +01:00
|
|
|
}
|
|
|
|
|
|
2025-02-25 14:37:28 +01:00
|
|
|
show = async () => {
|
|
|
|
|
this.ui.showLogo();
|
2025-03-07 13:14:32 +01:00
|
|
|
|
2025-04-01 14:09:20 +02:00
|
|
|
await this.loop.showLoop();
|
2025-02-25 15:25:14 +01:00
|
|
|
};
|
|
|
|
|
|
2025-03-07 13:14:32 +01:00
|
|
|
promptMainMenu = async () => {
|
2025-04-15 14:01:44 +02:00
|
|
|
if ((await this.processControl.getNumberOfCodexProcesses()) > 0) {
|
2025-04-14 14:31:22 +02:00
|
|
|
await this.showRunningMenu();
|
|
|
|
|
} else {
|
|
|
|
|
if (await this.installer.isCodexInstalled()) {
|
2025-04-15 14:01:44 +02:00
|
|
|
await this.showNotRunningMenu();
|
2025-04-14 14:31:22 +02:00
|
|
|
} else {
|
|
|
|
|
await this.showNotInstalledMenu();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
showNotInstalledMenu = async () => {
|
|
|
|
|
await this.ui.askMultipleChoice("Codex is not installed", [
|
2025-03-07 13:14:32 +01:00
|
|
|
{
|
2025-04-14 14:31:22 +02:00
|
|
|
label: "Install Codex",
|
2025-03-07 13:14:32 +01:00
|
|
|
action: this.installMenu.show,
|
|
|
|
|
},
|
2025-03-31 15:02:37 +02:00
|
|
|
{
|
2025-04-14 14:31:22 +02:00
|
|
|
label: "Exit",
|
|
|
|
|
action: this.loop.stopLoop,
|
|
|
|
|
},
|
|
|
|
|
]);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
showRunningMenu = async () => {
|
|
|
|
|
await this.ui.askMultipleChoice("Codex is running", [
|
|
|
|
|
{
|
|
|
|
|
label: "Open Codex app",
|
2025-04-15 15:54:46 +02:00
|
|
|
action: this.codexApp.openCodexApp,
|
2025-04-14 14:31:22 +02:00
|
|
|
},
|
2025-04-15 14:01:44 +02:00
|
|
|
{
|
|
|
|
|
label: "Stop Codex",
|
2025-04-16 10:18:38 +02:00
|
|
|
action: this.stopCodex,
|
2025-04-15 14:01:44 +02:00
|
|
|
},
|
2025-05-26 10:46:02 +02:00
|
|
|
{
|
|
|
|
|
label: "DoThing",
|
|
|
|
|
action: this.doThing,
|
|
|
|
|
},
|
2025-04-14 14:31:22 +02:00
|
|
|
{
|
2025-05-26 09:37:59 +02:00
|
|
|
label: "Exit (Codex keeps running)",
|
2025-04-14 14:31:22 +02:00
|
|
|
action: this.loop.stopLoop,
|
|
|
|
|
},
|
|
|
|
|
]);
|
|
|
|
|
};
|
|
|
|
|
|
2025-04-15 14:01:44 +02:00
|
|
|
showNotRunningMenu = async () => {
|
2025-04-16 09:42:11 +02:00
|
|
|
await this.ui.askMultipleChoice("Codex is installed but not running", [
|
2025-04-14 14:31:22 +02:00
|
|
|
{
|
|
|
|
|
label: "Start Codex",
|
2025-04-16 10:18:38 +02:00
|
|
|
action: this.startCodex,
|
2025-04-14 14:31:22 +02:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: "Edit Codex config",
|
2025-03-31 15:02:37 +02:00
|
|
|
action: this.configMenu.show,
|
|
|
|
|
},
|
2025-04-14 14:31:22 +02:00
|
|
|
{
|
|
|
|
|
label: "Uninstall Codex",
|
|
|
|
|
action: this.installMenu.show,
|
|
|
|
|
},
|
2025-02-25 14:37:28 +01:00
|
|
|
{
|
2025-03-07 13:14:32 +01:00
|
|
|
label: "Exit",
|
2025-04-01 14:09:20 +02:00
|
|
|
action: this.loop.stopLoop,
|
2025-02-25 14:37:28 +01:00
|
|
|
},
|
2025-03-07 13:14:32 +01:00
|
|
|
]);
|
2025-02-25 13:59:26 +01:00
|
|
|
};
|
2025-04-16 10:18:38 +02:00
|
|
|
|
|
|
|
|
startCodex = async () => {
|
2025-04-21 15:52:58 +02:00
|
|
|
const spinner = this.ui.createAndStartSpinner("Starting...");
|
2025-04-16 10:18:38 +02:00
|
|
|
try {
|
|
|
|
|
await this.processControl.startCodexProcess();
|
2025-04-21 15:52:58 +02:00
|
|
|
this.ui.stopSpinnerSuccess(spinner);
|
2025-04-16 10:18:38 +02:00
|
|
|
} catch (exception) {
|
2025-04-21 15:52:58 +02:00
|
|
|
this.ui.stopSpinnerError(spinner);
|
2025-04-16 10:18:38 +02:00
|
|
|
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}"`);
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-05-26 10:46:02 +02:00
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
|
|
}
|
2025-02-25 13:59:26 +01:00
|
|
|
}
|