diff --git a/src/handlers/installationHandlers.js b/src/handlers/installationHandlers.js index 9aad90a..65f7021 100644 --- a/src/handlers/installationHandlers.js +++ b/src/handlers/installationHandlers.js @@ -9,7 +9,7 @@ import { runCommand } from '../utils/command.js'; import { showErrorMessage, showInfoMessage, showSuccessMessage } from '../utils/messages.js'; import { checkDependencies } from '../services/nodeService.js'; import { saveConfig } from '../services/config.js'; -import { getCodexInstallPath } from '../utils/appdata.js'; +import { getCodexInstallPath, getCodexDataDirDefaultPath, getCodexLogsPath } from '../utils/appdata.js'; const platform = os.platform(); @@ -83,6 +83,8 @@ export async function checkCodexInstallation(config, showNavigationMenu) { async function saveCodexExePathToConfig(config, codexExePath) { config.codexExe = codexExePath; + config.dataDir = getCodexDataDirDefaultPath(); + config.logsDir = getCodexLogsPath(); if (!fs.existsSync(config.codexExe)) { console.log(showErrorMessage(`Codex executable not found in expected path: ${config.codexExe}`)); throw new Error("Exe not found"); diff --git a/src/handlers/nodeHandlers.js b/src/handlers/nodeHandlers.js index 1a74047..ce3073e 100644 --- a/src/handlers/nodeHandlers.js +++ b/src/handlers/nodeHandlers.js @@ -1,7 +1,8 @@ +import path from 'path'; import { createSpinner } from 'nanospinner'; import { runCommand } from '../utils/command.js'; import { showErrorMessage, showInfoMessage, showSuccessMessage } from '../utils/messages.js'; -import { isNodeRunning, isCodexInstalled, logToSupabase, startPeriodicLogging, getWalletAddress, setWalletAddress } from '../services/nodeService.js'; +import { isNodeRunning, isCodexInstalled, startPeriodicLogging, getWalletAddress, setWalletAddress } from '../services/nodeService.js'; import inquirer from 'inquirer'; import boxen from 'boxen'; import chalk from 'chalk'; @@ -27,6 +28,13 @@ async function promptForWalletAddress() { return wallet || null; } +function getCurrentLogFile(config) { + const timestamp = new Date().toISOString() + .replaceAll(":", "-") + .replaceAll(".", "-"); + return path.join(config.logsDir, `codex_${timestamp}.log`); +} + export async function runCodex(config, showNavigationMenu) { const isInstalled = await isCodexInstalled(config); if (!isInstalled) { @@ -65,9 +73,19 @@ export async function runCodex(config, showNavigationMenu) { nat = await runCommand('curl -s https://ip.codex.storage'); } + if (config.dataDir.length < 1) throw new Error("Missing config: dataDir"); + if (config.logsDir.length < 1) throw new Error("Missing config: logsDir"); + const logFilePath = getCurrentLogFile(config); + + console.log(showInfoMessage( + `Data location: ${config.dataDir}\n` + + `Logs: ${logFilePath}` + )); + const executable = config.codexExe; const args = [ - `--data-dir=datadir`, + `--data-dir=${config.dataDir}`, + `--log-file=${logFilePath}`, `--disc-port=${discPort}`, `--listen-addrs=/ip4/0.0.0.0/tcp/${listenPort}`, `--nat=${nat}`, diff --git a/src/services/config.js b/src/services/config.js index 70ae156..7bc2f4a 100644 --- a/src/services/config.js +++ b/src/services/config.js @@ -7,7 +7,8 @@ const defaultConfig = { // TODO: // Save user-selected config options. Use these when starting Codex. - // dataDir: "", + dataDir: "", + logsDir: "" // storageQuota: 0, // ports: { // discPort: 8090, diff --git a/src/utils/appdata.js b/src/utils/appdata.js index b277ac6..2860ec5 100644 --- a/src/utils/appdata.js +++ b/src/utils/appdata.js @@ -2,17 +2,26 @@ import path from 'path'; import fs from 'fs'; export function getAppDataDir() { - return getExists("codex-cli"); + return ensureExists(appData("codex-cli")); } export function getCodexInstallPath() { - return getExists("codex"); + return ensureExists(path.join(appData("codex"), "bin")); } -function getExists(appName) { - const dir = appData(appName); +export function getCodexDataDirDefaultPath() { + // This path does not exist on first startup. That's good: Codex will + // create it with the required access permissions. + return path.join(appData("codex"), "datadir"); +} + +export function getCodexLogsPath() { + return ensureExists(path.join(appData("codex"), "logs")); +} + +function ensureExists(dir) { if (!fs.existsSync(dir)) { - fs.mkdirSync(dir); + fs.mkdirSync(dir, { recursive: true }); } return dir; }