Sets dataDir and logDir in config. Defaults to user-accessible folder.

This commit is contained in:
thatben 2025-02-18 14:26:27 +01:00
parent e97493c183
commit ba2aae04a9
No known key found for this signature in database
GPG Key ID: 62C543548433D43E
4 changed files with 39 additions and 9 deletions

View File

@ -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");

View File

@ -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}`,

View File

@ -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,

View File

@ -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;
}