File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -71,6 +71,22 @@ async function readSettingsRecordAsyncFromPath(
7171 return parseSettingsRecord ( await fs . readFile ( filePath , "utf8" ) ) ;
7272}
7373
74+ function readSettingsBackupSync ( ) : JsonRecord | null {
75+ try {
76+ return readSettingsRecordSyncFromPath ( UNIFIED_SETTINGS_BACKUP_PATH ) ;
77+ } catch {
78+ return null ;
79+ }
80+ }
81+
82+ async function readSettingsBackupAsync ( ) : Promise < JsonRecord | null > {
83+ try {
84+ return await readSettingsRecordAsyncFromPath ( UNIFIED_SETTINGS_BACKUP_PATH ) ;
85+ } catch {
86+ return null ;
87+ }
88+ }
89+
7490function trySnapshotUnifiedSettingsBackupSync ( ) : void {
7591 if ( ! existsSync ( UNIFIED_SETTINGS_PATH ) ) {
7692 return ;
@@ -122,16 +138,14 @@ function readSettingsRecordSync(): JsonRecord | null {
122138 return primaryRecord ;
123139 }
124140 } catch ( error ) {
125- const backupRecord = readSettingsRecordSyncFromPath (
126- UNIFIED_SETTINGS_BACKUP_PATH ,
127- ) ;
141+ const backupRecord = readSettingsBackupSync ( ) ;
128142 if ( backupRecord ) {
129143 return backupRecord ;
130144 }
131145 throw error ;
132146 }
133147
134- return readSettingsRecordSyncFromPath ( UNIFIED_SETTINGS_BACKUP_PATH ) ;
148+ return readSettingsBackupSync ( ) ;
135149}
136150
137151/**
@@ -150,16 +164,14 @@ async function readSettingsRecordAsync(): Promise<JsonRecord | null> {
150164 return primaryRecord ;
151165 }
152166 } catch ( error ) {
153- const backupRecord = await readSettingsRecordAsyncFromPath (
154- UNIFIED_SETTINGS_BACKUP_PATH ,
155- ) ;
167+ const backupRecord = await readSettingsBackupAsync ( ) ;
156168 if ( backupRecord ) {
157169 return backupRecord ;
158170 }
159171 throw error ;
160172 }
161173
162- return readSettingsRecordAsyncFromPath ( UNIFIED_SETTINGS_BACKUP_PATH ) ;
174+ return readSettingsBackupAsync ( ) ;
163175}
164176
165177/**
Original file line number Diff line number Diff line change @@ -71,6 +71,20 @@ describe("unified settings", () => {
7171 expect ( await loadUnifiedDashboardSettings ( ) ) . toBeNull ( ) ;
7272 } ) ;
7373
74+ it ( "returns null sections when both primary and backup settings files are invalid" , async ( ) => {
75+ const {
76+ getUnifiedSettingsPath,
77+ loadUnifiedPluginConfigSync,
78+ loadUnifiedDashboardSettings,
79+ } = await import ( "../lib/unified-settings.js" ) ;
80+
81+ await fs . writeFile ( getUnifiedSettingsPath ( ) , "{ invalid json" , "utf8" ) ;
82+ await fs . writeFile ( `${ getUnifiedSettingsPath ( ) } .bak` , "{ invalid backup" , "utf8" ) ;
83+
84+ expect ( loadUnifiedPluginConfigSync ( ) ) . toBeNull ( ) ;
85+ expect ( await loadUnifiedDashboardSettings ( ) ) . toBeNull ( ) ;
86+ } ) ;
87+
7488 it ( "recovers plugin config from backup when primary settings file is invalid" , async ( ) => {
7589 const {
7690 getUnifiedSettingsPath,
You can’t perform that action at this time.
0 commit comments