@@ -121,12 +121,7 @@ export function renameLocalList(
121121) : DbConfig {
122122 const config = cloneDbConfig ( originalConfig ) ;
123123
124- const list = config . databases . local . lists . find (
125- ( l ) => l . name === currentListName ,
126- ) ;
127- if ( ! list ) {
128- throw Error ( `Cannot find list '${ currentListName } ' to rename` ) ;
129- }
124+ const list = getLocalList ( config , currentListName ) ;
130125 list . name = newListName ;
131126
132127 if (
@@ -148,12 +143,7 @@ export function renameRemoteList(
148143) : DbConfig {
149144 const config = cloneDbConfig ( originalConfig ) ;
150145
151- const list = config . databases . remote . repositoryLists . find (
152- ( l ) => l . name === currentListName ,
153- ) ;
154- if ( ! list ) {
155- throw Error ( `Cannot find list '${ currentListName } ' to rename` ) ;
156- }
146+ const list = getRemoteList ( config , currentListName ) ;
157147 list . name = newListName ;
158148
159149 if (
@@ -177,12 +167,7 @@ export function renameLocalDb(
177167 const config = cloneDbConfig ( originalConfig ) ;
178168
179169 if ( parentListName ) {
180- const list = config . databases . local . lists . find (
181- ( l ) => l . name === parentListName ,
182- ) ;
183- if ( ! list ) {
184- throw Error ( `Cannot find parent list '${ parentListName } '` ) ;
185- }
170+ const list = getLocalList ( config , parentListName ) ;
186171 const dbIndex = list . databases . findIndex ( ( db ) => db . name === currentDbName ) ;
187172 if ( dbIndex === - 1 ) {
188173 throw Error (
@@ -210,6 +195,132 @@ export function renameLocalDb(
210195 return config ;
211196}
212197
198+ export function removeLocalList (
199+ originalConfig : DbConfig ,
200+ listName : string ,
201+ ) : DbConfig {
202+ const config = cloneDbConfig ( originalConfig ) ;
203+
204+ config . databases . local . lists = config . databases . local . lists . filter (
205+ ( list ) => list . name !== listName ,
206+ ) ;
207+
208+ if ( config . selected ?. kind === SelectedDbItemKind . LocalUserDefinedList ) {
209+ config . selected = undefined ;
210+ }
211+
212+ if (
213+ config . selected ?. kind === SelectedDbItemKind . LocalDatabase &&
214+ config . selected ?. listName === listName
215+ ) {
216+ config . selected = undefined ;
217+ }
218+
219+ return config ;
220+ }
221+
222+ export function removeRemoteList (
223+ originalConfig : DbConfig ,
224+ listName : string ,
225+ ) : DbConfig {
226+ const config = cloneDbConfig ( originalConfig ) ;
227+
228+ config . databases . remote . repositoryLists =
229+ config . databases . remote . repositoryLists . filter (
230+ ( list ) => list . name !== listName ,
231+ ) ;
232+
233+ if ( config . selected ?. kind === SelectedDbItemKind . RemoteUserDefinedList ) {
234+ config . selected = undefined ;
235+ }
236+
237+ if (
238+ config . selected ?. kind === SelectedDbItemKind . RemoteRepository &&
239+ config . selected ?. listName === listName
240+ ) {
241+ config . selected = undefined ;
242+ }
243+
244+ return config ;
245+ }
246+
247+ export function removeLocalDb (
248+ originalConfig : DbConfig ,
249+ databaseName : string ,
250+ parentListName ?: string ,
251+ ) : DbConfig {
252+ const config = cloneDbConfig ( originalConfig ) ;
253+
254+ if ( parentListName ) {
255+ const parentList = getLocalList ( config , parentListName ) ;
256+ parentList . databases = parentList . databases . filter (
257+ ( db ) => db . name !== databaseName ,
258+ ) ;
259+ } else {
260+ config . databases . local . databases = config . databases . local . databases . filter (
261+ ( db ) => db . name !== databaseName ,
262+ ) ;
263+ }
264+
265+ if (
266+ config . selected ?. kind === SelectedDbItemKind . LocalDatabase &&
267+ config . selected ?. databaseName === databaseName &&
268+ config . selected ?. listName === parentListName
269+ ) {
270+ config . selected = undefined ;
271+ }
272+
273+ return config ;
274+ }
275+
276+ export function removeRemoteRepo (
277+ originalConfig : DbConfig ,
278+ repoFullName : string ,
279+ parentListName ?: string ,
280+ ) : DbConfig {
281+ const config = cloneDbConfig ( originalConfig ) ;
282+
283+ if ( parentListName ) {
284+ const parentList = getRemoteList ( config , parentListName ) ;
285+ parentList . repositories = parentList . repositories . filter (
286+ ( r ) => r !== repoFullName ,
287+ ) ;
288+ } else {
289+ config . databases . remote . repositories =
290+ config . databases . remote . repositories . filter ( ( r ) => r !== repoFullName ) ;
291+ }
292+
293+ if (
294+ config . selected ?. kind === SelectedDbItemKind . RemoteRepository &&
295+ config . selected ?. repositoryName === repoFullName &&
296+ config . selected ?. listName === parentListName
297+ ) {
298+ config . selected = undefined ;
299+ }
300+
301+ return config ;
302+ }
303+
304+ export function removeRemoteOwner (
305+ originalConfig : DbConfig ,
306+ ownerName : string ,
307+ ) : DbConfig {
308+ const config = cloneDbConfig ( originalConfig ) ;
309+
310+ config . databases . remote . owners = config . databases . remote . owners . filter (
311+ ( o ) => o !== ownerName ,
312+ ) ;
313+
314+ if (
315+ config . selected ?. kind === SelectedDbItemKind . RemoteOwner &&
316+ config . selected ?. ownerName === ownerName
317+ ) {
318+ config . selected = undefined ;
319+ }
320+
321+ return config ;
322+ }
323+
213324function cloneDbConfigSelectedItem ( selected : SelectedDbItem ) : SelectedDbItem {
214325 switch ( selected . kind ) {
215326 case SelectedDbItemKind . LocalUserDefinedList :
@@ -246,3 +357,28 @@ function cloneDbConfigSelectedItem(selected: SelectedDbItem): SelectedDbItem {
246357 } ;
247358 }
248359}
360+
361+ function getLocalList ( config : DbConfig , listName : string ) : LocalList {
362+ const list = config . databases . local . lists . find ( ( l ) => l . name === listName ) ;
363+
364+ if ( ! list ) {
365+ throw Error ( `Cannot find local list '${ listName } '` ) ;
366+ }
367+
368+ return list ;
369+ }
370+
371+ function getRemoteList (
372+ config : DbConfig ,
373+ listName : string ,
374+ ) : RemoteRepositoryList {
375+ const list = config . databases . remote . repositoryLists . find (
376+ ( l ) => l . name === listName ,
377+ ) ;
378+
379+ if ( ! list ) {
380+ throw Error ( `Cannot find remote list '${ listName } '` ) ;
381+ }
382+
383+ return list ;
384+ }
0 commit comments