|
| 1 | +import type { AccountStorageV3, FlaggedAccountStorageV1 } from "../storage.js"; |
| 2 | + |
| 3 | +export async function exportAccountsSnapshot(params: { |
| 4 | + resolvedPath: string; |
| 5 | + force: boolean; |
| 6 | + currentStoragePath: string; |
| 7 | + transactionState: |
| 8 | + | { |
| 9 | + active: boolean; |
| 10 | + storagePath: string; |
| 11 | + snapshot: AccountStorageV3 | null; |
| 12 | + } |
| 13 | + | undefined; |
| 14 | + loadAccountsInternal: () => Promise<AccountStorageV3 | null>; |
| 15 | + readCurrentStorage: () => Promise<AccountStorageV3 | null>; |
| 16 | + exportAccountsToFile: (args: { |
| 17 | + resolvedPath: string; |
| 18 | + force: boolean; |
| 19 | + storage: AccountStorageV3 | null; |
| 20 | + beforeCommit?: (resolvedPath: string) => Promise<void> | void; |
| 21 | + logInfo: (message: string, details: Record<string, unknown>) => void; |
| 22 | + }) => Promise<void>; |
| 23 | + beforeCommit?: (resolvedPath: string) => Promise<void> | void; |
| 24 | + logInfo: (message: string, details: Record<string, unknown>) => void; |
| 25 | +}): Promise<void> { |
| 26 | + const storage = |
| 27 | + params.transactionState?.active && |
| 28 | + params.transactionState.storagePath === params.currentStoragePath |
| 29 | + ? params.transactionState.snapshot |
| 30 | + : params.transactionState?.active |
| 31 | + ? await params.loadAccountsInternal() |
| 32 | + : await params.readCurrentStorage(); |
| 33 | + |
| 34 | + await params.exportAccountsToFile({ |
| 35 | + resolvedPath: params.resolvedPath, |
| 36 | + force: params.force, |
| 37 | + storage, |
| 38 | + beforeCommit: params.beforeCommit, |
| 39 | + logInfo: params.logInfo, |
| 40 | + }); |
| 41 | +} |
| 42 | + |
| 43 | +export async function importAccountsSnapshot(params: { |
| 44 | + resolvedPath: string; |
| 45 | + readImportFile: (args: { |
| 46 | + resolvedPath: string; |
| 47 | + normalizeAccountStorage: (value: unknown) => AccountStorageV3 | null; |
| 48 | + }) => Promise<AccountStorageV3>; |
| 49 | + normalizeAccountStorage: (value: unknown) => AccountStorageV3 | null; |
| 50 | + withAccountStorageTransaction: <T>( |
| 51 | + handler: ( |
| 52 | + current: AccountStorageV3 | null, |
| 53 | + persist: (storage: AccountStorageV3) => Promise<void>, |
| 54 | + ) => Promise<T>, |
| 55 | + ) => Promise<T>; |
| 56 | + mergeImportedAccounts: (args: { |
| 57 | + existing: AccountStorageV3 | null; |
| 58 | + imported: AccountStorageV3; |
| 59 | + maxAccounts: number; |
| 60 | + deduplicateAccounts: ( |
| 61 | + accounts: AccountStorageV3["accounts"], |
| 62 | + ) => AccountStorageV3["accounts"]; |
| 63 | + }) => { |
| 64 | + newStorage: AccountStorageV3; |
| 65 | + imported: number; |
| 66 | + total: number; |
| 67 | + skipped: number; |
| 68 | + }; |
| 69 | + maxAccounts: number; |
| 70 | + deduplicateAccounts: ( |
| 71 | + accounts: AccountStorageV3["accounts"], |
| 72 | + ) => AccountStorageV3["accounts"]; |
| 73 | + logInfo: (message: string, details: Record<string, unknown>) => void; |
| 74 | +}): Promise<{ imported: number; total: number; skipped: number }> { |
| 75 | + const normalized = await params.readImportFile({ |
| 76 | + resolvedPath: params.resolvedPath, |
| 77 | + normalizeAccountStorage: params.normalizeAccountStorage, |
| 78 | + }); |
| 79 | + |
| 80 | + const result = await params.withAccountStorageTransaction( |
| 81 | + async (existing, persist) => { |
| 82 | + const merged = params.mergeImportedAccounts({ |
| 83 | + existing, |
| 84 | + imported: normalized, |
| 85 | + maxAccounts: params.maxAccounts, |
| 86 | + deduplicateAccounts: params.deduplicateAccounts, |
| 87 | + }); |
| 88 | + await persist(merged.newStorage); |
| 89 | + return { |
| 90 | + imported: merged.imported, |
| 91 | + total: merged.total, |
| 92 | + skipped: merged.skipped, |
| 93 | + }; |
| 94 | + }, |
| 95 | + ); |
| 96 | + |
| 97 | + params.logInfo("Imported accounts", { |
| 98 | + path: params.resolvedPath, |
| 99 | + imported: result.imported, |
| 100 | + skipped: result.skipped, |
| 101 | + total: result.total, |
| 102 | + }); |
| 103 | + return result; |
| 104 | +} |
| 105 | + |
| 106 | +export async function saveFlaggedAccountsEntry(params: { |
| 107 | + storage: FlaggedAccountStorageV1; |
| 108 | + withStorageLock: <T>(fn: () => Promise<T>) => Promise<T>; |
| 109 | + saveUnlocked: (storage: FlaggedAccountStorageV1) => Promise<void>; |
| 110 | +}): Promise<void> { |
| 111 | + return params.withStorageLock(async () => { |
| 112 | + await params.saveUnlocked(params.storage); |
| 113 | + }); |
| 114 | +} |
| 115 | + |
| 116 | +export async function clearFlaggedAccountsEntry(params: { |
| 117 | + path: string; |
| 118 | + withStorageLock: <T>(fn: () => Promise<T>) => Promise<T>; |
| 119 | + markerPath: string; |
| 120 | + getBackupPaths: () => Promise<string[]>; |
| 121 | + clearFlaggedAccountsOnDisk: (args: { |
| 122 | + path: string; |
| 123 | + markerPath: string; |
| 124 | + backupPaths: string[]; |
| 125 | + logError: (message: string, details: Record<string, unknown>) => void; |
| 126 | + }) => Promise<void>; |
| 127 | + logError: (message: string, details: Record<string, unknown>) => void; |
| 128 | +}): Promise<void> { |
| 129 | + return params.withStorageLock(async () => { |
| 130 | + await params.clearFlaggedAccountsOnDisk({ |
| 131 | + path: params.path, |
| 132 | + markerPath: params.markerPath, |
| 133 | + backupPaths: await params.getBackupPaths(), |
| 134 | + logError: params.logError, |
| 135 | + }); |
| 136 | + }); |
| 137 | +} |
0 commit comments