Adds config module

This commit is contained in:
Ben 2025-02-17 09:39:45 +01:00
parent 9a1ce2bf52
commit 2a8888669a
No known key found for this signature in database
GPG Key ID: 0F16E812E736C24B
3 changed files with 49 additions and 4 deletions

View File

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

43
src/services/config.js Normal file
View File

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

View File

@ -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) {