From ac4c68816eea0efab67c73c4be3c2e6899ce493d Mon Sep 17 00:00:00 2001 From: Gerrit Birkeland Date: Thu, 5 Nov 2020 20:38:55 -0700 Subject: [PATCH] fix: Support for specify a directory as an entry point --- src/lib/application.ts | 9 ++++++--- src/lib/converter/context.ts | 6 +++--- src/lib/converter/converter.ts | 14 ++++++-------- src/lib/converter/nodes/block.ts | 18 +++++------------- 4 files changed, 20 insertions(+), 27 deletions(-) diff --git a/src/lib/application.ts b/src/lib/application.ts index c94b8c229..a960b867d 100644 --- a/src/lib/application.ts +++ b/src/lib/application.ts @@ -22,6 +22,7 @@ import { PluginHost, writeFile, readFile, + normalizePath, } from "./utils/index"; import { createMinimatch } from "./utils/paths"; @@ -216,7 +217,9 @@ export class Application extends ChildableComponent< ); } - const result = this.converter.convert(); + const result = this.converter.convert( + this.expandInputFiles(this.entryPoints) + ); if (result instanceof ProjectReflection) { return result; @@ -353,9 +356,9 @@ export class Application extends ChildableComponent< add(Path.join(file, next), false); }); } else if (supportedFileRegex.test(file)) { - files.push(file); + files.push(normalizePath(file)); } else if (includeJson && file.endsWith(".json")) { - files.push(file); + files.push(normalizePath(file)); } } diff --git a/src/lib/converter/context.ts b/src/lib/converter/context.ts index f47a9a1c1..d7f04dc70 100644 --- a/src/lib/converter/context.ts +++ b/src/lib/converter/context.ts @@ -27,7 +27,7 @@ export class Context { /** * A list of all files that have been passed to the TypeScript compiler. */ - fileNames: readonly string[]; + entryPoints: readonly string[]; /** * The TypeChecker instance returned by the TypeScript compiler. @@ -120,7 +120,7 @@ export class Context { program: ts.Program ) { this.converter = converter; - this.fileNames = fileNames; + this.entryPoints = fileNames; this.checker = checker; this.program = program; this.visitStack = []; @@ -417,7 +417,7 @@ export class Context { * @internal */ isExternalFile(fileName: string) { - let isExternal = !this.fileNames.includes(fileName); + let isExternal = !this.entryPoints.includes(fileName); if (!isExternal && this.externalPattern) { isExternal = this.externalPattern.some((mm) => mm.match(fileName)); } diff --git a/src/lib/converter/converter.ts b/src/lib/converter/converter.ts index 93b87b601..86d626d7a 100644 --- a/src/lib/converter/converter.ts +++ b/src/lib/converter/converter.ts @@ -205,18 +205,15 @@ export class Converter extends ChildableComponent< * * @param fileNames Array of the file names that should be compiled. */ - convert(): ProjectReflection | readonly ts.Diagnostic[] { + convert( + entryPoints: string[] + ): ProjectReflection | readonly ts.Diagnostic[] { const program = ts.createProgram( this.application.options.getFileNames(), this.application.options.getCompilerOptions() ); const checker = program.getTypeChecker(); - const context = new Context( - this, - program.getRootFileNames(), - checker, - program - ); + const context = new Context(this, entryPoints, checker, program); this.trigger(Converter.EVENT_BEGIN, context); @@ -316,7 +313,8 @@ export class Converter extends ChildableComponent< const needsSecondPass: ts.SourceFile[] = []; context.inFirstPass = true; - for (const entry of this.application.entryPoints) { + + for (const entry of context.entryPoints) { if (entry.endsWith(".json")) continue; const sourceFile = context.program.getSourceFile( entry.replace(/\\/g, "/") diff --git a/src/lib/converter/nodes/block.ts b/src/lib/converter/nodes/block.ts index 5aa3831f9..9ff77d7a1 100644 --- a/src/lib/converter/nodes/block.ts +++ b/src/lib/converter/nodes/block.ts @@ -6,7 +6,7 @@ import { Context } from "../context"; import { Component, ConverterNodeComponent } from "../components"; import { Converter } from "../converter"; import { getCommonDirectory } from "../../utils/fs"; -import { relative, resolve } from "path"; +import { relative } from "path"; @Component({ name: "node:block" }) export class BlockConverter extends ConverterNodeComponent< @@ -21,16 +21,12 @@ export class BlockConverter extends ConverterNodeComponent< ]; // Created in initialize - private entryPoints!: string[]; private baseDir!: string; initialize() { super.initialize(); - this.owner.on(Converter.EVENT_BEGIN, () => { - this.entryPoints = this.application.options - .getValue("entryPoints") - .map((path) => this.normalizeFileName(resolve(path))); - this.baseDir = getCommonDirectory(this.entryPoints); + this.owner.on(Converter.EVENT_BEGIN, (context: Context) => { + this.baseDir = getCommonDirectory(context.entryPoints); }); } @@ -74,12 +70,12 @@ export class BlockConverter extends ConverterNodeComponent< let result: Reflection | undefined = context.scope; context.withSourceFile(node, () => { - if (this.isEntryPoint(node.fileName)) { + if (context.entryPoints.includes(node.fileName)) { const symbol = context.checker.getSymbolAtLocation(node) ?? node.symbol; if (context.inFirstPass) { - if (this.entryPoints.length === 1) { + if (context.entryPoints.length === 1) { result = context.project; context.project.registerReflection(result, symbol); } else { @@ -190,10 +186,6 @@ export class BlockConverter extends ConverterNodeComponent< ); } - private isEntryPoint(fileName: string) { - return this.entryPoints.includes(fileName); - } - private normalizeFileName(fileName: string) { return fileName.replace(/\\/g, "/"); }