@@ -4,7 +4,7 @@ import fs from 'fs-extra';
4
4
import path from 'path' ;
5
5
import { NotFoundError } from '/common/error' ;
6
6
import { GitHubApi } from '/apis' ;
7
- import { createKey , execute , listFiles } from '/common/util' ;
7
+ import { createKey , execute , listDirectories , listFiles } from '/common/util' ;
8
8
import webhook from '/common/webhook' ;
9
9
10
10
const router = express . Router ( ) ;
@@ -38,7 +38,7 @@ const cacheAlgorithm = (categoryName, algorithmName) => {
38
38
const cacheCategory = categoryName => {
39
39
const categoryKey = createKey ( categoryName ) ;
40
40
const categoryPath = getPath ( categoryName ) ;
41
- const algorithms = listFiles ( categoryPath ) . map ( algorithmName => cacheAlgorithm ( categoryName , algorithmName ) ) ;
41
+ const algorithms = listDirectories ( categoryPath ) . map ( algorithmName => cacheAlgorithm ( categoryName , algorithmName ) ) ;
42
42
return {
43
43
key : categoryKey ,
44
44
name : categoryName ,
@@ -82,7 +82,7 @@ const cacheContributors = (files, commitAuthors) => Promise.each(files, file =>
82
82
} ) ;
83
83
84
84
const cacheCategories = ( ) => {
85
- const categories = listFiles ( getPath ( ) ) . map ( cacheCategory ) ;
85
+ const categories = listDirectories ( getPath ( ) ) . map ( cacheCategory ) ;
86
86
87
87
const files = [ ] ;
88
88
categories . forEach ( category => category . algorithms . forEach ( algorithm => files . push ( ...algorithm . files ) ) ) ;
@@ -91,19 +91,19 @@ const cacheCategories = () => {
91
91
return categories ;
92
92
} ;
93
93
94
+ let categories = [ ] ;
94
95
const downloadCategories = ( ) => (
95
96
fs . pathExistsSync ( repoPath ) ?
96
97
execute ( `git fetch && git reset --hard origin/master` , repoPath ) :
97
98
execute ( `git clone https://github.com/algorithm-visualizer/algorithms.git ${ repoPath } ` , __dirname )
98
- ) ;
99
+ ) . then ( ( ) => categories = cacheCategories ( ) ) ;
99
100
100
- let categories = [ ] ;
101
- downloadCategories ( ) . then ( ( ) => categories = cacheCategories ( ) ) ;
101
+ downloadCategories ( ) . catch ( console . error ) ;
102
102
103
103
webhook . on ( 'algorithms' , event => {
104
104
switch ( event ) {
105
105
case 'push' :
106
- downloadCategories ( ) . then ( ( ) => categories = cacheCategories ( ) ) ;
106
+ downloadCategories ( ) . catch ( console . error ) ;
107
107
break ;
108
108
}
109
109
} ) ;
0 commit comments