1- import { Command } from "commander" ;
2- import { logger } from "@logger" ;
1+ import * as process from "process" ;
2+
3+ import { DEFAULT_COLLECTION_CONFIGS_BASE_DIR , stringParser } from "@cmd/collection/common" ;
34import {
5+ addCollectionItems ,
46 deleteCollections ,
7+ insertCollection ,
58 listCollectionItems ,
69 listCollections ,
710 removeCollectionItems ,
8- updateCollection , addCollectionItems , insertCollection
11+ updateCollection
912} from "@db/collections" ;
10- import { loadCollectionConfigs } from "@configs" ;
11- import * as process from "node:process" ;
12- import { findReposByNames } from "@db/github_repos" ;
13- import { DEFAULT_COLLECTION_CONFIGS_BASE_DIR , stringParser } from "@cmd/collection/common" ;
13+
14+ import { Command } from "commander" ;
1415import { deleteCacheByCollectionId } from "@db/cache" ;
16+ import { findReposByNames } from "@db/github_repos" ;
17+ import { loadCollectionConfigs } from "@configs" ;
18+ import { logger } from "@logger" ;
1519
1620export function initReloadCollectionCommand ( collectionCmd : Command ) {
1721 collectionCmd
@@ -38,7 +42,7 @@ export async function syncCollection(args: any) {
3842
3943 const oldCollectionIds = new Set ( collections . map ( ( c ) => c . id ) ) ;
4044 const newCollectionIds = new Set ( configsMap . keys ( ) ) ;
41- const collectionIdsToDelete = Array . from ( oldCollectionIds . difference ( newCollectionIds ) ) ;
45+ const collectionIdsToDelete = Array . from ( oldCollectionIds ) . filter ( id => ! newCollectionIds . has ( id ) ) ;
4246
4347 // Remove non-exists collections from database.
4448 if ( collectionIdsToDelete . length > 0 ) {
@@ -50,7 +54,7 @@ export async function syncCollection(args: any) {
5054 for ( const config of configsMap . values ( ) ) {
5155 const { id : collectionId , name : collectionName , items : collectionRepos } = config ;
5256
53- if ( ! newCollectionIds . has ( collectionId ) ) {
57+ if ( ! oldCollectionIds . has ( collectionId ) ) {
5458 // Add new collection from config to database.
5559 await insertCollection ( {
5660 id : collectionId ,
@@ -96,21 +100,21 @@ export async function syncCollectionItems(collectionId: number, collectionName:
96100 const newRepoNames = new Set ( collectionRepos ) ;
97101
98102 // Remove non-exists items from collection.
99- const reposToRemove = oldRepoNames . difference ( newRepoNames ) ;
103+ const reposToRemove = new Set ( Array . from ( oldRepoNames ) . filter ( name => ! newRepoNames . has ( name ) ) ) ;
100104 if ( reposToRemove . size > 0 ) {
101105 const repoNames = Array . from ( reposToRemove ) ;
102106 await removeCollectionItems ( collectionId , repoNames ) ;
103107 logger . info ( `✅ Collection [${ collectionName } ](id: ${ collectionId } ): remove repos ${ repoNames . join ( ',' ) } from database.` ) ;
104108 }
105109
106110 // Add collection items.
107- const reposToAdd = newRepoNames . difference ( oldRepoNames ) ;
111+ const reposToAdd = new Set ( Array . from ( newRepoNames ) . filter ( name => ! oldRepoNames . has ( name ) ) ) ;
108112 if ( reposToAdd . size === 0 ) {
109113 logger . debug ( `Collection [${ collectionName } ](id: ${ collectionId } ): no new repos need to added.` )
110114 return reposToRemove . size ;
111115 }
112116
113- const repoNames = Array . from ( reposToAdd . keys ( ) ) ;
117+ const repoNames = Array . from ( reposToAdd ) ;
114118 const repos = await findReposByNames ( repoNames ) ;
115119 if ( repos . length < reposToAdd . size ) {
116120 const diffRepos = repoNames . filter ( name => ! repos . some ( r => r . repo_name === name ) ) ;
0 commit comments