refactor(codex): give the CODEX_HOME lookup its own module
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* @file Centralized Codex home directory path management. Resolves the Codex
|
||||
* home directory, project-level Codex surfaces, and project roots. Supports a
|
||||
* custom root via the CODEX_HOME environment variable so the dashboard can
|
||||
* track non-standard Codex installations.
|
||||
* @author Nguyễn Ngọc Trí Vĩ <vinnt@smartgift.vn>
|
||||
*/
|
||||
|
||||
const path = require("node:path");
|
||||
const os = require("node:os");
|
||||
|
||||
/**
|
||||
* The Codex home directory. Resolves to $CODEX_HOME if set, else
|
||||
* ~/.codex. Always reads process.env at call time so tests can inject.
|
||||
*
|
||||
* @returns {string} Absolute path to the Codex home directory.
|
||||
*/
|
||||
function getCodexHome() {
|
||||
return process.env.CODEX_HOME || path.join(os.homedir(), ".codex");
|
||||
}
|
||||
|
||||
/**
|
||||
* The directory where Codex stores per-project surfaces (rollout journals,
|
||||
* SQLite mirrors, etc.) for a project at the given working directory.
|
||||
*
|
||||
* @param {string} cwd Absolute path to a project root.
|
||||
* @returns {string} Absolute path to <cwd>/.codex.
|
||||
*/
|
||||
function getProjectCodexDir(cwd) {
|
||||
return path.join(cwd, ".codex");
|
||||
}
|
||||
|
||||
/**
|
||||
* The project root. For now, this is just an identity function — cwd is
|
||||
* the project root. Exists as a contract for consistency with claude-home.js
|
||||
* and to anchor the path-resolution hierarchy.
|
||||
*
|
||||
* @param {string} cwd Absolute path to a project root (where AGENTS.md lives).
|
||||
* @returns {string} The same cwd.
|
||||
*/
|
||||
function getProjectRoot(cwd) {
|
||||
return cwd;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getCodexHome,
|
||||
getProjectCodexDir,
|
||||
getProjectRoot,
|
||||
};
|
||||
@@ -33,6 +33,7 @@
|
||||
const fs = require("node:fs");
|
||||
const path = require("node:path");
|
||||
const os = require("node:os");
|
||||
const { getCodexHome } = require("./codex-home");
|
||||
|
||||
// `*** Update File: <path>` lines inside an apply_patch heredoc.
|
||||
const PATCH_FILE_RE = /^\*\*\* (Add|Update|Delete) File: (.+)$/gm;
|
||||
@@ -98,10 +99,6 @@ function eventsFromPayload(payload) {
|
||||
return [{ tool_name: "Bash", tool_input: { command: commandFromPayload(payload, text) } }];
|
||||
}
|
||||
|
||||
function codexHome() {
|
||||
return process.env.CODEX_HOME || path.join(os.homedir(), ".codex");
|
||||
}
|
||||
|
||||
/** Every rollout journal under sessions/, newest mtime first, capped at MAX_FILES. */
|
||||
function rolloutFiles() {
|
||||
const out = [];
|
||||
@@ -126,7 +123,7 @@ function rolloutFiles() {
|
||||
}
|
||||
}
|
||||
};
|
||||
walk(path.join(codexHome(), "sessions"), 0);
|
||||
walk(path.join(getCodexHome(), "sessions"), 0);
|
||||
return out.sort((a, b) => b.mtime - a.mtime).slice(0, MAX_FILES);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user