Skip to content

Commit

Permalink
feat(bootstrap): Rename addSourceFiles -> addSourceFilesByPaths to ma…
Browse files Browse the repository at this point in the history
…tch ts-morph.
  • Loading branch information
dsherret committed Nov 8, 2019
1 parent 574fe13 commit 439a0d5
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/bootstrap/lib/ts-morph-bootstrap.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export declare class Project {
* @param fileGlobs - File glob or globs to add files based on.
* @returns The matched source files.
*/
addSourceFiles(fileGlobs: string | ReadonlyArray<string>): ts.SourceFile[];
addSourceFilesByPaths(fileGlobs: string | ReadonlyArray<string>): ts.SourceFile[];
/**
* Adds all the source files from the specified tsconfig.json.
*
Expand Down
2 changes: 1 addition & 1 deletion packages/bootstrap/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ts-morph/bootstrap",
"version": "0.1.1",
"version": "0.2.0",
"description": "API for getting quickly set up with the TypeScript Compiler API.",
"keywords": ["typescript", "compiler", "bootstrap"],
"main": "dist/ts-morph-bootstrap.js",
Expand Down
37 changes: 27 additions & 10 deletions packages/bootstrap/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,23 @@ import { Project, ts } from "@ts-morph/bootstrap";
const project = new Project();

// these are typed as ts.SourceFile
const myClassFile = project.createSourceFile("MyClass.ts", "export class MyClass { prop: string; }");
const mainFile = project.createSourceFile("main.ts", "import { MyClass } from './MyClass.ts'");

const program = project.createProgram(); // ts.Program
const typeChecker = program.getTypeChecker(); // ts.TypeChecker
const languageService = project.getLanguageService(); // ts.LanguageService
const moduleResolutionHost = project.getModuleResolutionHost(); // ts.ModuleResolutionHost
const myClassFile = project.createSourceFile(
"MyClass.ts",
"export class MyClass { prop: string; }"
);
const mainFile = project.createSourceFile(
"main.ts",
"import { MyClass } from './MyClass.ts'"
);

// ts.Program
const program = project.createProgram();
// ts.TypeChecker
const typeChecker = program.getTypeChecker();
// ts.LanguageService
const languageService = project.getLanguageService();
// ts.ModuleResolutionHost
const moduleResolutionHost = project.getModuleResolutionHost();
```

## Setup
Expand Down Expand Up @@ -97,7 +107,8 @@ For example:
```ts
import { Project, ts } from "ts-morph";

// this is deno style module resolution (ex. `import { MyClass } from "./MyClass.ts"`)
// This is deno style module resolution.
// Ex. `import { MyClass } from "./MyClass.ts"`;
const project = new Project({
resolutionHost: (moduleResolutionHost, getCompilerOptions) => {
return {
Expand All @@ -106,7 +117,13 @@ const project = new Project({
const resolvedModules: ts.ResolvedModule[] = [];

for (const moduleName of moduleNames.map(removeTsExtension)) {
const result = ts.resolveModuleName(moduleName, containingFile, compilerOptions, moduleResolutionHost);
const result = ts.resolveModuleName(
moduleName,
containingFile,
compilerOptions,
moduleResolutionHost
);

if (result.resolvedModule)
resolvedModules.push(result.resolvedModule);
}
Expand All @@ -128,7 +145,7 @@ const project = new Project({

Use the following methods:

* `const sourceFiles = project.addSourceFiles("**/*.ts");` or provide an array of file globs.
* `const sourceFiles = project.addSourceFilesByPaths("**/*.ts");` or provide an array of file globs.
* `const sourceFile = project.addSourceFileAtPath("src/my-file.ts");` or use `addSourceFileAtPathIfExists(filePath)`
* `const sourceFiles = project.addSourceFilesFromTsConfig("path/to/tsconfig.json")`

Expand Down
2 changes: 1 addition & 1 deletion packages/bootstrap/src/Project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export class Project {
* @param fileGlobs - File glob or globs to add files based on.
* @returns The matched source files.
*/
addSourceFiles(fileGlobs: string | ReadonlyArray<string>): ts.SourceFile[] {
addSourceFilesByPaths(fileGlobs: string | ReadonlyArray<string>): ts.SourceFile[] {
if (typeof fileGlobs === "string")
fileGlobs = [fileGlobs];

Expand Down
4 changes: 2 additions & 2 deletions packages/bootstrap/src/tests/projectTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ describe(nameof(Project), () => {
});
});

describe(nameof<Project>(p => p.addSourceFiles), () => {
describe(nameof<Project>(p => p.addSourceFilesByPaths), () => {
it("should add the source files based on a file glob", () => {
const fileSystem = new InMemoryFileSystemHost();
fileSystem.writeFileSync("/otherFile.ts", "");
Expand All @@ -338,7 +338,7 @@ describe(nameof(Project), () => {
fileSystem.writeFileSync("/test/other/file.ts", "");
const project = new Project({ fileSystem });
expect(project.getSourceFiles()).to.deep.equal([]);
const returnedFiles = project.addSourceFiles("/test/**/*.ts");
const returnedFiles = project.addSourceFilesByPaths("/test/**/*.ts");
const expectedFiles = ["/test/file.ts", "/test/test2/file2.ts", "/test/other/file.ts"].sort();
expect(project.getSourceFiles().map(s => s.fileName).sort()).to.deep.equal(expectedFiles);
expect(returnedFiles.map(s => s.fileName).sort()).to.deep.equal(expectedFiles);
Expand Down

0 comments on commit 439a0d5

Please sign in to comment.