Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promise all #447

Merged
merged 6 commits into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/contexts/language/LanguageContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ export class LanguageContext extends ContextBase {
this.initableInspectors = initableInspectors;
}
async init(): Promise<void> {
for (const initableInspector of this.initableInspectors) {
await initableInspector.init();
}
await Promise.all(this.initableInspectors.map((initableInspector) => initableInspector.init()));
}

get path(): string {
Expand Down
31 changes: 17 additions & 14 deletions src/inspectors/FileInspector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,21 +126,24 @@ export class FileInspector implements IFileInspector {
}
const forbiddenDirNames = ['.git', 'node_modules'];
// debug(dirContents);
for (const entry of dirContents) {
const entryMetadata = await this.getMetadata(`${path}/${entry}`);
if (entryMetadata.type === MetadataType.file) {
if (entryMetadata.name.match(fileName)) {
result.push(entryMetadata);
await Promise.all(
dirContents.map(async (entry) => {
const entryMetadata = await this.getMetadata(`${path}/${entry}`);
if (entryMetadata.type === MetadataType.file) {
if (entryMetadata.name.match(fileName)) {
result.push(entryMetadata);
}
} else if (
options?.shallow !== true &&
entryMetadata.type === MetadataType.dir &&
forbiddenDirNames.indexOf(entryMetadata.name) === -1
) {
const subResult = await this.scanFor(fileName, entryMetadata.path, options);
result = [...result, ...subResult];
}
} else if (
options.shallow !== true &&
entryMetadata.type === MetadataType.dir &&
forbiddenDirNames.indexOf(entryMetadata.name) === -1
) {
const subResult = await this.scanFor(fileName, entryMetadata.path, options);
result = [...result, ...subResult];
}
}
}),
);

return result;
});
}
Expand Down
7 changes: 5 additions & 2 deletions src/scanner/Scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,12 @@ export class Scanner {
*/
private async detectLanguagesAtPaths(context: ScannerContext) {
let languagesAtPaths: LanguageAtPath[] = [];
for (const languageDetector of context.languageDetectors) {
languagesAtPaths = [...languagesAtPaths, ...(await languageDetector.detectLanguage())];
const languageDetectors = await Promise.all(context.languageDetectors.map((languageDetector) => languageDetector.detectLanguage()));

for (const languageDetector of languageDetectors) {
languagesAtPaths = [...languagesAtPaths, ...languageDetector];
}

return languagesAtPaths;
}

Expand Down
13 changes: 7 additions & 6 deletions src/services/FileSystemService.real.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,12 +366,13 @@ describe('FileSystemService - REAL', () => {

const files: string[] = [];

await fileSystemService.flatTraverse(mockFolderPath, (meta) => {
files.push(meta.name);
return false;
});

expect(files.length).toEqual(1);
await fileSystemService
.flatTraverse(mockFolderPath, (meta) => {
files.push(meta.name);
return false;
})
.then(() => fail("promise didn't fail on false return"))
.catch((e) => expect(e).toBe(false));
});

it("throws an error if the root doesn't exist", async () => {
Expand Down
26 changes: 12 additions & 14 deletions src/services/FileSystemService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,17 @@ export class FileSystemService implements IProjectFilesBrowserService {
}

async flatTraverse(path: string, fn: (meta: Metadata) => void | boolean) {
// <typeof fs> is small hack because TS thinks
// that it's different interface by default
const dirContent = await (<typeof fs>this.fileSystem).promises.readdir(path);
for (const cnt of dirContent) {
const absolutePath = nodePath.resolve(path, cnt);
const metadata = await this.getMetadata(absolutePath);

const lambdaResult = fn(metadata);
if (lambdaResult === false) return false;

if (metadata.type === MetadataType.dir) {
await this.flatTraverse(metadata.path, fn);
}
}
const dirContent = await this.readDirectory(path);

await Promise.all(
dirContent.map(async (cnt) => {
const absolutePath = nodePath.resolve(path, cnt);
const metadata = await this.getMetadata(absolutePath);

const lambdaResult = fn(metadata);
if (lambdaResult === false) return Promise.reject(false);
if (metadata.type === MetadataType.dir) return this.flatTraverse(metadata.path, fn);
}),
);
}
}
13 changes: 7 additions & 6 deletions src/services/FileSystemService.virtual.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,12 +373,13 @@ describe('FileSystemService - VIRTUAL', () => {

const files: string[] = [];

await service.flatTraverse(mockFolderPath, (meta) => {
files.push(meta.name);
return false;
});

expect(files.length).toEqual(1);
await service
.flatTraverse(mockFolderPath, (meta) => {
files.push(meta.name);
return false;
})
.then(() => fail("promise didn't fail on false return"))
.catch((e) => expect(e).toBe(false));
});

it("throws an error if the root doesn't exist", async () => {
Expand Down
47 changes: 23 additions & 24 deletions src/services/git/Git.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,36 +224,35 @@ describe('Git', () => {
});

describe('#flatTraverse', () => {
it('returns keys of metadata of all results', async () => {
gitHubNock.getDirectory('mockFolder', ['mockFile.ts'], ['mockSubFolder']);
gitHubNock.getFile('mockFolder/mockFile.ts');
gitHubNock.getDirectory('mockFolder/mockSubFolder', ['mockSubFolderFile.txt'], []);
gitHubNock.getFile('mockFolder/mockSubFolder/mockSubFolderFile.txt');

const files: string[] = [];

await git.flatTraverse('mockFolder', (meta) => {
files.push(meta.name);
describe('#fetch dir and files', () => {
beforeEach(() => {
gitHubNock.getDirectory('mockFolder', ['mockFile.ts'], ['mockSubFolder']);
gitHubNock.getFile('mockFolder/mockFile.ts');
gitHubNock.getDirectory('mockFolder/mockSubFolder', ['mockSubFolderFile.txt'], []);
gitHubNock.getFile('mockFolder/mockSubFolder/mockSubFolderFile.txt');
});

expect(files.length).toEqual(3);
expect(files).toContain('mockFile.ts');
expect(files).toContain('mockSubFolder');
expect(files).toContain('mockSubFolderFile.txt');
});
it('returns keys of metadata of all results', async () => {
const files: string[] = [];

it('stops on false', async () => {
gitHubNock.getDirectory('mockFolder', ['mockFile.ts'], ['mockSubFolder']);
gitHubNock.getFile('mockFolder/mockFile.ts');
await git.flatTraverse('mockFolder', (meta) => {
files.push(meta.name);
});

const files: string[] = [];

await git.flatTraverse('mockFolder', (meta) => {
files.push(meta.name);
return false;
expect(files.length).toEqual(3);
expect(files).toContain('mockFile.ts');
expect(files).toContain('mockSubFolder');
expect(files).toContain('mockSubFolderFile.txt');
});

expect(files.length).toEqual(1);
it('stops on false', async () => {
await git
.flatTraverse('mockFolder', () => {
return false;
})
.then(() => fail("promise didn't fail"))
.catch((e) => expect(e).toBe(false));
});
});

it("throws an error if the root doesn't exist", async () => {
Expand Down
20 changes: 11 additions & 9 deletions src/services/git/Git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,19 @@ export class Git implements IProjectFilesBrowserService {

async flatTraverse(path: string, fn: (meta: Metadata) => void | boolean): Promise<void | boolean> {
const dirContent = await this.readDirectory(path);
for (const cnt of dirContent) {
const absolutePath = nodePath.posix.join(path, cnt);
const metadata = await this.getMetadata(absolutePath);
await Promise.all(
dirContent.map(async (cnt) => {
const absolutePath = nodePath.posix.join(path, cnt);
const metadata = await this.getMetadata(absolutePath);

const lambdaResult = fn(metadata);
if (lambdaResult === false) return false;
const lambdaResult = fn(metadata);
if (lambdaResult === false) return Promise.reject(false);

if (metadata.type === MetadataType.dir) {
await this.flatTraverse(metadata.path, fn);
}
}
if (metadata.type === MetadataType.dir) {
return this.flatTraverse(metadata.path, fn);
}
}),
);
}

async getContributorCount(): Promise<number> {
Expand Down