@@ -45,6 +45,10 @@ import {
4545 type AccountIdentityRef ,
4646 toAccountIdentityRef ,
4747} from "./storage/identity.js" ;
48+ import {
49+ loadNormalizedStorageFromPathOrNull ,
50+ mergeStorageForMigration ,
51+ } from "./storage/migration-helpers.js" ;
4852import {
4953 type AccountMetadataV1 ,
5054 type AccountMetadataV3 ,
@@ -747,9 +751,13 @@ async function migrateLegacyProjectStorageIfNeeded(
747751 let migrated = false ;
748752
749753 for ( const legacyPath of existingCandidatePaths ) {
750- const legacyStorage = await loadNormalizedStorageFromPath (
754+ const legacyStorage = await loadNormalizedStorageFromPathOrNull (
751755 legacyPath ,
752756 "legacy account storage" ,
757+ {
758+ loadAccountsFromPath,
759+ logWarn : ( message , meta ) => log . warn ( message , meta ) ,
760+ } ,
753761 ) ;
754762 if ( ! legacyStorage ) {
755763 continue ;
@@ -758,6 +766,7 @@ async function migrateLegacyProjectStorageIfNeeded(
758766 const mergedStorage = mergeStorageForMigration (
759767 targetStorage ,
760768 legacyStorage ,
769+ { normalizeAccountStorage } ,
761770 ) ;
762771 const fallbackStorage = targetStorage ?? legacyStorage ;
763772
@@ -813,45 +822,10 @@ async function loadNormalizedStorageFromPath(
813822 path : string ,
814823 label : string ,
815824) : Promise < AccountStorageV3 | null > {
816- try {
817- const { normalized, schemaErrors } = await loadAccountsFromPath ( path ) ;
818- if ( schemaErrors . length > 0 ) {
819- log . warn ( `${ label } schema validation warnings` , {
820- path,
821- errors : schemaErrors . slice ( 0 , 5 ) ,
822- } ) ;
823- }
824- return normalized ;
825- } catch ( error ) {
826- const code = ( error as NodeJS . ErrnoException ) . code ;
827- if ( code !== "ENOENT" ) {
828- log . warn ( `Failed to load ${ label } ` , {
829- path,
830- error : String ( error ) ,
831- } ) ;
832- }
833- return null ;
834- }
835- }
836-
837- function mergeStorageForMigration (
838- current : AccountStorageV3 | null ,
839- incoming : AccountStorageV3 ,
840- ) : AccountStorageV3 {
841- if ( ! current ) {
842- return incoming ;
843- }
844-
845- const merged = normalizeAccountStorage ( {
846- version : 3 ,
847- activeIndex : current . activeIndex ,
848- activeIndexByFamily : current . activeIndexByFamily ,
849- accounts : [ ...current . accounts , ...incoming . accounts ] ,
825+ return loadNormalizedStorageFromPathOrNull ( path , label , {
826+ loadAccountsFromPath,
827+ logWarn : ( message , meta ) => log . warn ( message , meta ) ,
850828 } ) ;
851- if ( ! merged ) {
852- return current ;
853- }
854- return merged ;
855829}
856830
857831function selectNewestAccount < T extends AccountLike > (
0 commit comments