Skip to content

Commit

Permalink
fix: use environment variable instead of repeating retrieval functions
Browse files Browse the repository at this point in the history
Signed-off-by: tylerslaton <[email protected]>
  • Loading branch information
tylerslaton committed Sep 10, 2024
1 parent 3dd5794 commit bb9c48c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 38 deletions.
28 changes: 9 additions & 19 deletions actions/appSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ const defaultAppSettings: AppSettings = {
};

export async function getAppSettings(): Promise<AppSettings> {
const location = await AppSettingsLocation();
if (!process.env.GPTSCRIPT_SETTINGS_FILE) {
throw new Error('GPTSCRIPT_SETTINGS_FILE not set');
}

const location = process.env.GPTSCRIPT_SETTINGS_FILE;
if (fs.existsSync(location)) {
const AppSettings = fs.readFileSync(location, 'utf-8');
return JSON.parse(AppSettings) as AppSettings;
Expand All @@ -32,24 +36,10 @@ export async function getAppSettings(): Promise<AppSettings> {
}

export async function setAppSettings(AppSettings: AppSettings) {
const location = await AppSettingsLocation();
fs.writeFileSync(location, JSON.stringify(AppSettings, null, 2));
}

export async function AppSettingsLocation(): Promise<string> {
const homeDir = os.homedir();
let configDir;
if (os.platform() === 'darwin') {
configDir =
process.env.XDG_CONFIG_HOME ||
path.join(homeDir, 'Library', 'Application Support');
} else if (os.platform() === 'win32') {
configDir = path.join(homeDir, 'AppData', 'Local');
} else if (os.platform() === 'linux') {
configDir = process.env.XDG_CONFIG_HOME || path.join(homeDir, '.config');
} else {
throw new Error('Unsupported platform');
if (!process.env.GPTSCRIPT_SETTINGS_FILE) {
throw new Error('GPTSCRIPT_SETTINGS_FILE not set');
}

return path.join(configDir, 'acorn', 'settings.json');
const location = process.env.GPTSCRIPT_SETTINGS_FILE;
fs.writeFileSync(location, JSON.stringify(AppSettings, null, 2));
}
20 changes: 1 addition & 19 deletions server/app.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -121,24 +121,6 @@ export const startAppServer = ({ dev, hostname, port, appDir }) => {
});
};

function appSettingsLocation() {
const homeDir = os.homedir();
let configDir;
if (os.platform() === 'darwin') {
configDir =
process.env.XDG_CONFIG_HOME ||
path.join(homeDir, 'Library', 'Application Support');
} else if (os.platform() === 'win32') {
configDir = path.join(homeDir, 'AppData', 'Local');
} else if (os.platform() === 'linux') {
configDir = process.env.XDG_CONFIG_HOME || path.join(homeDir, '.config');
} else {
throw new Error('Unsupported platform');
}

return path.join(configDir, 'acorn', 'settings.json');
}

const mount = async (
location,
tool,
Expand All @@ -153,7 +135,7 @@ const mount = async (
process.env.WORKSPACE_DIR ?? process.env.GPTSCRIPT_WORKSPACE_DIR;
const THREADS_DIR =
process.env.THREADS_DIR ?? path.join(WORKSPACE_DIR, 'threads');
const settingsLocation = appSettingsLocation();
const settingsLocation = process.env.GPTSCRIPT_SETTINGS_FILE;
const settings = JSON.parse(fs.readFileSync(settingsLocation, 'utf8'));

let script;
Expand Down

0 comments on commit bb9c48c

Please sign in to comment.