diff --git a/src/commands/wallet.ts b/src/commands/wallet.ts index d5336db..4c44c10 100644 --- a/src/commands/wallet.ts +++ b/src/commands/wallet.ts @@ -5,6 +5,7 @@ import fs from "fs-extra"; import crypto from "crypto"; import { loadWallets } from "../utils/index.js"; import { walletFilePath } from "../utils/constants.js"; +import path from "path"; type InquirerAnswers = { action?: string; @@ -16,6 +17,7 @@ type InquirerAnswers = { setCurrentWallet?: boolean; confirmDelete?: boolean; newWalletName?: string; + backupPath?: string; }; export async function walletCommand() { @@ -47,6 +49,7 @@ export async function walletCommand() { "šŸ” List saved wallets", "šŸ” Switch wallet", "šŸ“ Update wallet name", + "šŸ“‚ Backup wallet data", "āŒ Delete wallet", ], }, @@ -481,6 +484,27 @@ export async function walletCommand() { writeWalletData(walletFilePath, walletsData); } + + if (action === "šŸ“‚ Backup wallet data") { + const backupPathQuestion: any = [ + { + type: "input", + name: "backupPath", + message: "šŸ’¾ Enter the path where you want to save the backup:", + }, + ]; + + const { backupPath } = await inquirer.prompt( + backupPathQuestion + ); + + if (!backupPath) { + console.log(chalk.red("āš ļø Backup path is required!")); + return; + } + + await backupCommand(backupPath); + } } catch (error: any) { console.error( chalk.red("āŒ Error creating or managing wallets:"), @@ -489,14 +513,10 @@ export async function walletCommand() { } } -async function writeWalletData(walletFilePath: string, walletsData: any) { +async function writeWalletData(filePath: string, data: any) { try { - fs.writeFileSync( - walletFilePath, - JSON.stringify(walletsData, null, 2), - "utf8" - ); - console.log(chalk.green(`šŸ’¾ Changes saved at ${walletFilePath}`)); + fs.writeFileSync(filePath, JSON.stringify(data, null, 2), "utf8"); + console.log(chalk.green(`šŸ’¾ Changes saved at ${filePath}`)); } catch (error: any) { console.error( chalk.red("āŒ Error saving wallet data:"), @@ -504,3 +524,43 @@ async function writeWalletData(walletFilePath: string, walletsData: any) { ); } } + +async function backupCommand(backupPath: string) { + try { + if (!fs.existsSync(walletFilePath)) { + console.log(chalk.red("šŸš« No saved wallet found. Please create a wallet first.")); + return; + } + + if (!backupPath) { + console.log(chalk.red("āš ļø Please provide a valid file path for backup.")); + return; + } + + let absoluteBackupPath = path.resolve(backupPath); + const backupDir = path.dirname(absoluteBackupPath); + + if (fs.existsSync(absoluteBackupPath) && fs.lstatSync(absoluteBackupPath).isDirectory()) { + absoluteBackupPath = path.join(absoluteBackupPath, 'wallet_backup.json'); + console.log(chalk.yellow(`āš ļø Provided a directory. Using default file name: wallet_backup.json`)); + } + + if (!fs.existsSync(backupDir)) { + fs.mkdirSync(backupDir, { recursive: true }); + console.log(chalk.green(`šŸ“‚ Created backup directory: ${backupDir}`)); + } + + const walletData = JSON.parse(fs.readFileSync(walletFilePath, "utf8")); + + writeWalletData(absoluteBackupPath, walletData); + console.log( + chalk.green("āœ… Wallet backup created successfully!"), + chalk.green(`\nšŸ’¾ Backup saved successfully at: ${absoluteBackupPath}`) + ); + } catch (error: any) { + console.error( + chalk.red("šŸšØ Error during wallet backup:"), + chalk.yellow(error.message) + ); + } +} \ No newline at end of file