diff --git a/src/main.js b/src/main.js index c93c116..80483fa 100644 --- a/src/main.js +++ b/src/main.js @@ -49,6 +49,10 @@ function handleExit() { process.exit(0); } +function getWorkingDir(commandArgs) { + +} + export async function main() { const commandArgs = parseCommandLineArgs(); if (commandArgs) { @@ -69,7 +73,6 @@ export async function main() { try { while (true) { console.log('\n' + chalk.cyanBright(ASCII_ART)); - 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 new file mode 100644 index 0000000..14dbcb5 --- /dev/null +++ b/src/utils/appdata.js @@ -0,0 +1,33 @@ +import path from 'path'; +import fs from 'fs'; + +export function getAppDataDir() { + const dir = appData("codex-cli"); + if (!fs.existsSync(dir)) { + fs.mkdirSync(dir); + } + return dir; +} + +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; + } + }); +} +