155 lines
5.3 KiB
JavaScript
Raw Normal View History

#!/usr/bin/env node
import inquirer from 'inquirer';
import chalk from 'chalk';
import boxen from 'boxen';
import { ASCII_ART } from './constants/ascii.js';
import { handleCommandLineOperation, parseCommandLineArgs } from './cli/commandParser.js';
import { uploadFile, downloadFile, showLocalFiles } from './handlers/fileHandlers.js';
2025-02-21 10:07:52 +01:00
import { installCodex, uninstallCodex } from './handlers/installationHandlers.js';
import { runCodex, checkNodeStatus } from './handlers/nodeHandlers.js';
2025-01-10 16:26:22 +00:00
import { showInfoMessage } from './utils/messages.js';
2025-02-17 12:23:48 +01:00
import { loadConfig } from './services/config.js';
2025-02-19 14:56:22 +01:00
import { showConfigMenu } from './configmenu.js';
import { openCodexApp } from './services/codexapp.js';
async function showNavigationMenu() {
console.log('\n')
const { choice } = await inquirer.prompt([
{
type: 'list',
name: 'choice',
message: 'What would you like to do?',
choices: [
'1. Back to main menu',
'2. Exit'
],
pageSize: 2,
loop: true
}
]);
switch (choice.split('.')[0]) {
case '1':
return main();
case '2':
handleExit();
}
}
function handleExit() {
console.log(boxen(
chalk.cyanBright('👋 Thank you for using Codex Storage CLI! Goodbye!'),
{
padding: 1,
margin: 1,
borderStyle: 'round',
borderColor: 'cyan',
title: '👋 GOODBYE',
titleAlignment: 'center'
}
));
process.exit(0);
}
export async function main() {
const commandArgs = parseCommandLineArgs();
if (commandArgs) {
switch (commandArgs.command) {
case 'upload':
await uploadFile(commandArgs.value, handleCommandLineOperation, showNavigationMenu);
return;
case 'download':
await downloadFile(commandArgs.value, handleCommandLineOperation, showNavigationMenu);
return;
}
}
process.on('SIGINT', handleExit);
process.on('SIGTERM', handleExit);
process.on('SIGQUIT', handleExit);
try {
2025-02-17 12:23:48 +01:00
const config = loadConfig();
while (true) {
console.log('\n' + chalk.cyanBright(ASCII_ART));
const { choice } = await inquirer.prompt([
{
type: 'list',
name: 'choice',
message: 'Select an option:',
choices: [
'1. Download and install Codex',
2025-02-24 15:49:19 -07:00
'2. Run Codex node',
'3. Check node status',
'4. Edit Codex configuration',
'5. Open Codex App',
'6. Upload a file',
'7. Download a file',
'8. Show local data',
'9. Uninstall Codex node',
'10. Submit feedback',
'11. Exit'
],
pageSize: 11,
loop: true
}
]).catch(() => {
handleExit();
2025-02-19 14:56:22 +01:00
return;
});
2025-02-19 14:56:22 +01:00
switch (choice.split('.')[0]) {
case '1':
const installed = await installCodex(config, showNavigationMenu);
if (installed) {
await showConfigMenu(config);
}
break;
case '2':
2025-02-17 12:35:54 +01:00
await runCodex(config, showNavigationMenu);
return;
2025-02-24 15:49:19 -07:00
case '3':
await checkNodeStatus(config, showNavigationMenu);
break;
2025-02-19 14:56:22 +01:00
case '4':
2025-02-24 15:49:19 -07:00
await showConfigMenu(config);
break;
2025-02-19 14:56:22 +01:00
case '5':
2025-02-24 15:49:19 -07:00
openCodexApp(config);
break;
2025-02-19 14:56:22 +01:00
case '6':
await uploadFile(config, null, handleCommandLineOperation, showNavigationMenu);
break;
2025-02-19 14:56:22 +01:00
case '7':
await downloadFile(config, null, handleCommandLineOperation, showNavigationMenu);
break;
2025-02-19 14:56:22 +01:00
case '8':
await showLocalFiles(config, showNavigationMenu);
2025-01-10 16:26:22 +00:00
break;
2025-02-19 14:56:22 +01:00
case '9':
await uninstallCodex(config, showNavigationMenu);
break;
case '10':
2025-01-10 16:26:22 +00:00
const { exec } = await import('child_process');
2025-02-24 15:49:19 -07:00
const url = 'https://tally.so/r/w2DlXb';
2025-01-10 16:26:22 +00:00
const command = process.platform === 'win32' ? `start ${url}` : process.platform === 'darwin' ? `open ${url}` : `xdg-open ${url}`;
exec(command);
console.log(showInfoMessage('Opening feedback form in your browser...'));
break;
case '11':
2025-02-19 14:56:22 +01:00
handleExit();
return;
}
console.log('\n');
}
} catch (error) {
if (error.message.includes('ExitPromptError')) {
handleExit();
} else {
console.error(chalk.red('An error occurred:', error.message));
handleExit();
}
}
}