-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce async SourceFileProvider interface (#97)
* Introduce async SourceFileProvider interface Adds a common interface that in future changes will be implemented with a faster alternate that avoids scanning all files in the program during initialization As part of this work, I rework consumers of TypeScriptProgram to be asyncronous, and add runWithConcurrentLimit to fence check in parallel. This is needed because if we were to check all fences in parallel we would hit the system RLIMIT by opening all fences simultaneously. And while I'm in there, I added a progress bar to give user feedback while performing fence checks. This does not display while performing provider initialization, but this is less of a problem against the (yet to come) newer implementation that doesn't perform full typescript program initialization. * Update runner.ts * Update Options.ts * Update getImportsFromFile.ts * Prefer ?. for control flow in runWithConcurrentLimit Co-authored-by: Maxwell Huang-Hobbs <mahuangh@microsoft.com> Co-authored-by: Scott Mikula <mikula@gmail.com>
- Loading branch information
1 parent
8e7799d
commit eb572f4
Showing
14 changed files
with
169 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export interface SourceFileProvider { | ||
getSourceFiles(searchRoots?: string[]): Promise<string[]> | string[]; | ||
getImportsForFile(filePath: string): Promise<string[]> | string[]; | ||
resolveImportFromFile( | ||
importer: string, | ||
importSpecifier: string | ||
): Promise<string | undefined> | string | undefined; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,21 @@ | ||
import TypeScriptProgram from '../core/TypeScriptProgram'; | ||
import NormalizedPath from '../types/NormalizedPath'; | ||
import ImportRecord from '../core/ImportRecord'; | ||
import { SourceFileProvider } from '../core/SourceFileProvider'; | ||
|
||
export default function getImportsFromFile(filePath: NormalizedPath, tsProgram: TypeScriptProgram) { | ||
const importedFiles = tsProgram.getImportsForFile(filePath); | ||
return importedFiles | ||
.map(importInfo => new ImportRecord(importInfo.fileName, filePath, tsProgram)) | ||
.filter(importRecord => importRecord.filePath); | ||
export default async function getImportsFromFile( | ||
sourceFilePath: NormalizedPath, | ||
sourceFileProvider: SourceFileProvider | ||
) { | ||
const rawImports = await sourceFileProvider.getImportsForFile(sourceFilePath); | ||
const resolvedImports = await Promise.all( | ||
rawImports.map( | ||
async rawImport => | ||
new ImportRecord( | ||
rawImport, | ||
await sourceFileProvider.resolveImportFromFile(sourceFilePath, rawImport) | ||
) | ||
) | ||
); | ||
|
||
return resolvedImports.filter(importRecord => importRecord.filePath); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import * as CliProgress from 'cli-progress'; | ||
|
||
export async function runWithConcurrentLimit<I>( | ||
maxBatchSize: number, | ||
inputs: I[], | ||
cb: (input: I) => Promise<void>, | ||
progress: boolean | ||
): Promise<void> { | ||
const i = [...inputs]; | ||
let completedJobs = 0; | ||
const initialWorkingSet = i.splice(0, maxBatchSize); | ||
|
||
let progressBar: CliProgress.SingleBar | undefined; | ||
if (progress) { | ||
progressBar = new CliProgress.SingleBar( | ||
{ | ||
etaBuffer: maxBatchSize, | ||
}, | ||
CliProgress.Presets.shades_grey | ||
); | ||
progressBar.start(inputs.length, 0); | ||
} | ||
|
||
const queueNext = (): Promise<void> | void => { | ||
const next = i.shift(); | ||
completedJobs += 1; | ||
progressBar?.update(completedJobs); | ||
if (next) { | ||
return cb(next).then(queueNext); | ||
} | ||
}; | ||
await Promise.all(initialWorkingSet.map(i => cb(i).then(queueNext))); | ||
|
||
progressBar?.stop(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters