From 9a1ce2bf52cfd5eeb90be03f2a23374b18bfb811 Mon Sep 17 00:00:00 2001 From: thatben Date: Fri, 14 Feb 2025 11:56:16 +0100 Subject: [PATCH 1/2] wip --- src/main.js | 6 ++++++ src/utils/appdata.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 src/utils/appdata.js diff --git a/src/main.js b/src/main.js index c93c116..ee09762 100644 --- a/src/main.js +++ b/src/main.js @@ -9,6 +9,7 @@ import { uploadFile, downloadFile, showLocalFiles } from './handlers/fileHandler import { checkCodexInstallation, installCodex, uninstallCodex } from './handlers/installationHandlers.js'; import { runCodex, checkNodeStatus } from './handlers/nodeHandlers.js'; import { showInfoMessage } from './utils/messages.js'; +import { getAppDataDir } from "./utils/appdata.js"; async function showNavigationMenu() { console.log('\n') @@ -49,6 +50,10 @@ function handleExit() { process.exit(0); } +function getWorkingDir(commandArgs) { + +} + export async function main() { const commandArgs = parseCommandLineArgs(); if (commandArgs) { @@ -69,6 +74,7 @@ export async function main() { try { while (true) { console.log('\n' + chalk.cyanBright(ASCII_ART)); + console.log(showInfoMessage(`Working directory: ${getAppDataDir()}`)); const { choice } = await inquirer.prompt([ { diff --git a/src/utils/appdata.js b/src/utils/appdata.js new file mode 100644 index 0000000..24b1d99 --- /dev/null +++ b/src/utils/appdata.js @@ -0,0 +1,28 @@ +import path from 'path'; + +export function getAppDataDir() { + return appData("codex-cli"); +} + +function appData(...app) { + let appData; + if (process.platform === 'win32') { + appData = path.join(process.env.APPDATA, ...app); + } else if (process.platform === 'darwin') { + appData = path.join(process.env.HOME, 'Library', 'Application Support', ...app); + } else { + appData = path.join(process.env.HOME, ...prependDot(...app)); + } + return appData; +} + +function prependDot(...app) { + return app.map((item, i) => { + if (i === 0) { + return `.${item}`; + } else { + return item; + } + }); +} + From 2a8888669a33eeb150f67beefbcc96298b67782d Mon Sep 17 00:00:00 2001 From: Ben Date: Mon, 17 Feb 2025 09:39:45 +0100 Subject: [PATCH 2/2] Adds config module --- src/main.js | 3 --- src/services/config.js | 43 ++++++++++++++++++++++++++++++++++++++++++ src/utils/appdata.js | 7 ++++++- 3 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 src/services/config.js diff --git a/src/main.js b/src/main.js index ee09762..80483fa 100644 --- a/src/main.js +++ b/src/main.js @@ -9,7 +9,6 @@ import { uploadFile, downloadFile, showLocalFiles } from './handlers/fileHandler import { checkCodexInstallation, installCodex, uninstallCodex } from './handlers/installationHandlers.js'; import { runCodex, checkNodeStatus } from './handlers/nodeHandlers.js'; import { showInfoMessage } from './utils/messages.js'; -import { getAppDataDir } from "./utils/appdata.js"; async function showNavigationMenu() { console.log('\n') @@ -74,8 +73,6 @@ export async function main() { try { while (true) { console.log('\n' + chalk.cyanBright(ASCII_ART)); - console.log(showInfoMessage(`Working directory: ${getAppDataDir()}`)); - const { choice } = await inquirer.prompt([ { type: 'list', diff --git a/src/services/config.js b/src/services/config.js new file mode 100644 index 0000000..13a6944 --- /dev/null +++ b/src/services/config.js @@ -0,0 +1,43 @@ +import fs from 'fs'; +import path from 'path'; +import { getAppDataDir } from '../utils/appdata.js'; + +const defaultConfig = { + dataDir: "", + storageQuota: 0, + ports: { + discPort: 8090, + listenPort: 8070, + apiPort: 8080 + } +}; + +function getConfigFilename() { + return path.join(getAppDataDir(), "config.json"); +} + +export function saveConfig(config) { + const filePath = getConfigFilename(); + console.log("writing to: " + filePath ); + try { + fs.writeFileSync(filePath, JSON.stringify(config)); + } catch (error) { + console.error(`Failed to save config file to '${filePath}' error: '${error}'.`); + throw error; + } +} + +export function loadConfig() { + const filePath = getConfigFilename(); + console.log("loading from: " + filePath ); + try { + if (!fs.existsSync(filePath)) { + saveConfig(defaultConfig); + return defaultConfig; + } + return JSON.parse(fs.readFileSync(filePath)); + } catch (error) { + console.error(`Failed to load config file from '${filePath}' error: '${error}'.`); + throw error; + } +} diff --git a/src/utils/appdata.js b/src/utils/appdata.js index 24b1d99..14dbcb5 100644 --- a/src/utils/appdata.js +++ b/src/utils/appdata.js @@ -1,7 +1,12 @@ import path from 'path'; +import fs from 'fs'; export function getAppDataDir() { - return appData("codex-cli"); + const dir = appData("codex-cli"); + if (!fs.existsSync(dir)) { + fs.mkdirSync(dir); + } + return dir; } function appData(...app) {