From 1061a3d269b25609b1315fa644e434cc212c4e5b Mon Sep 17 00:00:00 2001 From: Bogdan Luca Date: Fri, 17 Aug 2018 20:13:50 +0300 Subject: [PATCH] getGitReposInFolder: fix async folder filtering --- src/adapter/repository/git.ts | 3 ++- src/common/helpers.ts | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/adapter/repository/git.ts b/src/adapter/repository/git.ts index aec8e6c1..c0f5e1b8 100644 --- a/src/adapter/repository/git.ts +++ b/src/adapter/repository/git.ts @@ -13,6 +13,7 @@ import { IFileStatParser, ILogParser } from '../parsers/types'; import { ITEM_ENTRY_SEPARATOR, LOG_ENTRY_SEPARATOR, LOG_FORMAT_ARGS } from './constants'; import { GitOriginType } from './index'; import { IGitArgsService } from './types'; +import { asyncFilter } from '../../common/helpers'; @injectable() export class Git implements IGitService { @@ -426,7 +427,7 @@ export class Git implements IGitService { .map(item => path.join(dir, item)); filteredItems.push(dir); - const folders = await Promise.all(filteredItems.filter(async item => (await fs.stat(item)).isDirectory())); + const folders = await asyncFilter(filteredItems, async item => (await fs.stat(item)).isDirectory()); const gitRootArgs = this.gitArgsService.getGitRootArgs(); const gitRoots = (await Promise.all(folders.map(async item => { try { diff --git a/src/common/helpers.ts b/src/common/helpers.ts index 2f623ffb..7ee4b7ff 100644 --- a/src/common/helpers.ts +++ b/src/common/helpers.ts @@ -23,3 +23,11 @@ export function formatDate(date: Date) { const dateOptions = { weekday: 'short', day: 'numeric', month: 'short', year: 'numeric', hour: 'numeric', minute: 'numeric' }; return date.toLocaleString(lang, dateOptions); } + +export async function asyncFilter(arr: T[], callback): Promise { + return ( + await Promise.all(arr.map( + async item => (await callback(item)) ? item : undefined) + ) + ).filter(i => i !== undefined) as T[]; +}