@@ -4,7 +4,7 @@ import fs from 'fs-extra';
44import path from 'path' ;
55import { NotFoundError } from '/common/error' ;
66import { GitHubApi } from '/apis' ;
7- import { createKey , execute , listFiles } from '/common/util' ;
7+ import { createKey , execute , listDirectories , listFiles } from '/common/util' ;
88import webhook from '/common/webhook' ;
99
1010const router = express . Router ( ) ;
@@ -38,7 +38,7 @@ const cacheAlgorithm = (categoryName, algorithmName) => {
3838const cacheCategory = categoryName => {
3939 const categoryKey = createKey ( categoryName ) ;
4040 const categoryPath = getPath ( categoryName ) ;
41- const algorithms = listFiles ( categoryPath ) . map ( algorithmName => cacheAlgorithm ( categoryName , algorithmName ) ) ;
41+ const algorithms = listDirectories ( categoryPath ) . map ( algorithmName => cacheAlgorithm ( categoryName , algorithmName ) ) ;
4242 return {
4343 key : categoryKey ,
4444 name : categoryName ,
@@ -82,7 +82,7 @@ const cacheContributors = (files, commitAuthors) => Promise.each(files, file =>
8282} ) ;
8383
8484const cacheCategories = ( ) => {
85- const categories = listFiles ( getPath ( ) ) . map ( cacheCategory ) ;
85+ const categories = listDirectories ( getPath ( ) ) . map ( cacheCategory ) ;
8686
8787 const files = [ ] ;
8888 categories . forEach ( category => category . algorithms . forEach ( algorithm => files . push ( ...algorithm . files ) ) ) ;
@@ -91,19 +91,19 @@ const cacheCategories = () => {
9191 return categories ;
9292} ;
9393
94+ let categories = [ ] ;
9495const downloadCategories = ( ) => (
9596 fs . pathExistsSync ( repoPath ) ?
9697 execute ( `git fetch && git reset --hard origin/master` , repoPath ) :
9798 execute ( `git clone https://github.com/algorithm-visualizer/algorithms.git ${ repoPath } ` , __dirname )
98- ) ;
99+ ) . then ( ( ) => categories = cacheCategories ( ) ) ;
99100
100- let categories = [ ] ;
101- downloadCategories ( ) . then ( ( ) => categories = cacheCategories ( ) ) ;
101+ downloadCategories ( ) . catch ( console . error ) ;
102102
103103webhook . on ( 'algorithms' , event => {
104104 switch ( event ) {
105105 case 'push' :
106- downloadCategories ( ) . then ( ( ) => categories = cacheCategories ( ) ) ;
106+ downloadCategories ( ) . catch ( console . error ) ;
107107 break ;
108108 }
109109} ) ;
0 commit comments