Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions path/src/hooks/useAccounts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// complete code
import fs from 'fs';
import path from 'path';
import { v4 as uuidv4 } from 'uuid';

interface Account {
id: string;
name: string;
token: string;
}

const accountsPath = path.join(process.env.HOME, '.codex-switcher', 'accounts.json');

function loadAccounts(): Promise<Account[]> {
return new Promise((resolve, reject) => {
fs.readFile(accountsPath, 'utf8', (err, data) => {
if (err) {
if (err.code === 'ENOENT') {
// Handle the case where the file does not exist
resolve([]);
} else {
reject(err);
}
} else {
try {
const accounts = JSON.parse(data);
resolve(accounts);
} catch (e) {
// Handle the case where the file is not a valid JSON
console.error(`Error parsing accounts file: ${e}`);
resolve([]);
}
}
});
});
}

function saveAccounts(accounts: Account[]): Promise<void> {
return new Promise((resolve, reject) => {
const accountsJson = JSON.stringify(accounts);
fs.writeFile(accountsPath, accountsJson, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}

function getAccounts(): Promise<Account[]> {
return loadAccounts();
}

function updateAccounts(accounts: Account[]): Promise<void> {
return saveAccounts(accounts);
}

// Example usage:
getAccounts().then((accounts) => {
console.log(accounts);
updateAccounts(accounts).then(() => {
console.log('Accounts updated');
}).catch((err) => {
console.error('Error updating accounts:', err);
});
});
46 changes: 46 additions & 0 deletions path/src/lib/accounts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# complete code
import os
import json
import fs
from fs import open_file
from fs import error as fs_error

class Accounts:
def __init__(self):
self.accounts_path = os.path.join(os.path.expanduser('~'), '.codex-switcher', 'accounts.json')

def load_accounts(self) -> list:
"""
Load accounts from the accounts.json file.

Returns:
A list of dictionaries representing the accounts.
"""
try:
with open_file(self.accounts_path, 'r') as f:
return json.load(f)
except fs_error.FileNotFoundError:
# Handle the case where the file does not exist
return []
except json.JSONDecodeError as e:
# Handle the case where the file is not a valid JSON
print(f"Error parsing accounts file: {e}")
return []
except Exception as e:
# Handle any other exceptions
print(f"Error loading accounts: {e}")
return []

def save_accounts(self, accounts: list) -> None:
"""
Save accounts to the accounts.json file.

Args:
accounts: A list of dictionaries representing the accounts.
"""
try:
with open_file(self.accounts_path, 'w') as f:
json.dump(accounts, f)
except Exception as e:
# Handle any exceptions
print(f"Error saving accounts: {e}")
47 changes: 47 additions & 0 deletions path/src/lib/accounts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// complete code
import fs from 'fs';
import path from 'path';

interface Account {
id: string;
name: string;
token: string;
}

class Accounts {
private accountsPath: string;

constructor() {
this.accountsPath = path.join(process.env.HOME, '.codex-switcher', 'accounts.json');
}

async loadAccounts(): Promise<Account[]> {
try {
const data = await fs.promises.readFile(this.accountsPath, 'utf8');
return JSON.parse(data);
} catch (err) {
if (err.code === 'ENOENT') {
// Handle the case where the file does not exist
return [];
} else {
throw err;
}
}
}

async saveAccounts(accounts: Account[]): Promise<void> {
const accountsJson = JSON.stringify(accounts);
await fs.promises.writeFile(this.accountsPath, accountsJson);
}
}

// Example usage:
const accounts = new Accounts();
accounts.loadAccounts().then((accounts) => {
console.log(accounts);
accounts.saveAccounts(accounts).then(() => {
console.log('Accounts updated');
}).catch((err) => {
console.error('Error updating accounts:', err);
});
});