Skip to content

Commit

Permalink
feat: Support multiple globs when getting source files.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Jan 7, 2018
1 parent e6be0cf commit bb935d9
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 17 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
"devDependencies": {
"@types/chai": "^3.4.34",
"@types/chokidar": "^1.7.4",
"@types/minimatch": "^3.0.1",
"@types/mocha": "^2.2.33",
"@types/multimatch": "^2.1.2",
"@types/node": "^6.0.48",
"@types/object-assign": "^4.0.30",
"chai": "^3.5.0",
Expand All @@ -60,7 +60,7 @@
"dependencies": {
"code-block-writer": "^6.2.0",
"globby": "^6.1.0",
"minimatch": "^3.0.4",
"multimatch": "^2.1.0",
"object-assign": "^4.1.1",
"typescript": "^2.4.x"
},
Expand Down
31 changes: 24 additions & 7 deletions src/TsSimpleAst.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as ts from "typescript";
import {Minimatch} from "minimatch";
import * as multimatch from "multimatch";
import * as errors from "./errors";
import * as compiler from "./compiler";
import * as factories from "./factories";
Expand Down Expand Up @@ -282,18 +282,35 @@ export class TsSimpleAst {
* Gets all the source files contained in the compiler wrapper.
* @param globPattern - Glob pattern for filtering out the source files.
*/
getSourceFiles(globPattern?: string): compiler.SourceFile[] {
getSourceFiles(): compiler.SourceFile[];
/**
* Gets all the source files contained in the compiler wrapper that match a pattern.
* @param globPattern - Glob pattern for filtering out the source files.
*/
getSourceFiles(globPattern: string): compiler.SourceFile[];
/**
* Gets all the source files contained in the compiler wrapper that match the passed in patterns.
* @param globPatterns - Glob patterns for filtering out the source files.
*/
getSourceFiles(globPatterns: string[]): compiler.SourceFile[];
getSourceFiles(globPatterns?: string | string[]): compiler.SourceFile[] {
const {compilerFactory} = this.global;
const sourceFiles = this.global.compilerFactory.getSourceFilesByDirectoryDepth();
if (typeof globPattern === "string")
if (typeof globPatterns === "string" || globPatterns instanceof Array)
return ArrayUtils.from(getFilteredSourceFiles());
else
return ArrayUtils.from(sourceFiles);

function* getFilteredSourceFiles() {
const mm = new Minimatch(globPattern!, { matchBase: true });
for (const sourceFile of sourceFiles) {
if (mm.match(sourceFile.getFilePath()))
yield sourceFile;
const sourceFilePaths = Array.from(getSourceFilePaths());
const matchedPaths = multimatch(sourceFilePaths, globPatterns!);

for (const matchedPath of matchedPaths)
yield compilerFactory.getSourceFileFromFilePath(matchedPath)!;

function* getSourceFilePaths() {
for (const sourceFile of sourceFiles)
yield sourceFile.getFilePath();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ export * from "./ComputedPropertyName";
export * from "./Expression";
export * from "./Identifier";
export * from "./Node";
export * from "./object";
export * from "./PropertyAccessExpression";
export * from "./QualifiedName";
export * from "./Scope";
export * from "./Signature";
export * from "./Symbol";
export * from "./SyntaxList";
export * from "./object";
28 changes: 21 additions & 7 deletions src/tests/tsSimpleAstTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,21 +507,35 @@ describe(nameof(TsSimpleAst), () => {
]);
});

it("should be able to do a file glob", () => {
describe("globbing", () => {
const ast = new TsSimpleAst({ useVirtualFileSystem: true });
ast.createSourceFile("file.ts", "");
ast.createSourceFile("src/file.ts", "");
ast.createSourceFile("src/test/file1.ts", "");
ast.createSourceFile("src/test/file1.d.ts", "");
ast.createSourceFile("src/test/file2.ts", "");
ast.createSourceFile("src/test/file3.ts", "");
ast.createSourceFile("src/test/file3.js", "");
ast.createSourceFile("src/test/folder/file.ts", "");
expect(ast.getSourceFiles("**/src/test/**/*.ts").map(s => s.getFilePath())).to.deep.equal([
"/src/test/file1.ts",
"/src/test/file2.ts",
"/src/test/file3.ts",
"/src/test/folder/file.ts"
]);

it("should be able to do a file glob", () => {
expect(ast.getSourceFiles("**/src/test/**/*.ts").map(s => s.getFilePath())).to.deep.equal([
"/src/test/file1.d.ts",
"/src/test/file1.ts",
"/src/test/file2.ts",
"/src/test/file3.ts",
"/src/test/folder/file.ts"
]);
});

it("should be able to do a file glob with multiple patterns", () => {
expect(ast.getSourceFiles(["**/src/test/**/*.ts", "!**/*.d.ts"]).map(s => s.getFilePath())).to.deep.equal([
"/src/test/file1.ts",
"/src/test/file2.ts",
"/src/test/file3.ts",
"/src/test/folder/file.ts"
]);
});
});
});

Expand Down

0 comments on commit bb935d9

Please sign in to comment.