Skip to content

Commit cd1d30d

Browse files
committed
List directories instead of files when caching algorithms
1 parent 73517b1 commit cd1d30d

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

src/backend/common/util.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Promise from 'bluebird';
22
import child_process from 'child_process';
3+
import path from 'path';
34
import fs from 'fs-extra';
45

56
const execute = (command, cwd, { stdout = process.stdout, stderr = process.stderr } = {}) => new Promise((resolve, reject) => {
@@ -22,11 +23,17 @@ const spawn = (command, args, cwd, { stdout = process.stdout, stderr = process.s
2223

2324
const createKey = name => name.toLowerCase().replace(/ /g, '-');
2425

26+
const isDirectory = dirPath => fs.lstatSync(dirPath).isDirectory();
27+
2528
const listFiles = dirPath => fs.readdirSync(dirPath).filter(fileName => !fileName.startsWith('.'));
2629

30+
const listDirectories = dirPath => listFiles(dirPath).filter(fileName => isDirectory(path.resolve(dirPath, fileName)));
31+
2732
export {
2833
execute,
2934
spawn,
3035
createKey,
36+
isDirectory,
3137
listFiles,
38+
listDirectories,
3239
};

src/backend/controllers/algorithms.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import fs from 'fs-extra';
44
import path from 'path';
55
import { NotFoundError } from '/common/error';
66
import { GitHubApi } from '/apis';
7-
import { createKey, execute, listFiles } from '/common/util';
7+
import { createKey, execute, listDirectories, listFiles } from '/common/util';
88
import webhook from '/common/webhook';
99

1010
const router = express.Router();
@@ -38,7 +38,7 @@ const cacheAlgorithm = (categoryName, algorithmName) => {
3838
const 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

8484
const 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 = [];
9495
const 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

103103
webhook.on('algorithms', event => {
104104
switch (event) {
105105
case 'push':
106-
downloadCategories().then(() => categories = cacheCategories());
106+
downloadCategories().catch(console.error);
107107
break;
108108
}
109109
});

src/backend/controllers/tracers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ const buildRelease = release => (
3232
execute(`git clone https://github.com/algorithm-visualizer/tracers.git ${repoPath}`, __dirname)
3333
).then(() => execute(`git reset --hard ${release.target_commitish} && npm install && npm run build && ./bin/build`, repoPath));
3434

35-
GitHubApi.getLatestRelease('algorithm-visualizer', 'tracers').then(buildRelease);
35+
GitHubApi.getLatestRelease('algorithm-visualizer', 'tracers').then(buildRelease).catch(console.error);
3636

3737
webhook.on('tracers', (event, data) => {
3838
switch (event) {
3939
case 'release':
40-
buildRelease(data.release);
40+
buildRelease(data.release).catch(console.error);
4141
break;
4242
}
4343
});

0 commit comments

Comments
 (0)