Skip to content

Commit

Permalink
fix: Support for specify a directory as an entry point
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit0 committed Nov 6, 2020
1 parent ea604ad commit ac4c688
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 27 deletions.
9 changes: 6 additions & 3 deletions src/lib/application.ts
Expand Up @@ -22,6 +22,7 @@ import {
PluginHost,
writeFile,
readFile,
normalizePath,
} from "./utils/index";
import { createMinimatch } from "./utils/paths";

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/lib/converter/context.ts
Expand Up @@ -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.
Expand Down Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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));
}
Expand Down
14 changes: 6 additions & 8 deletions src/lib/converter/converter.ts
Expand Up @@ -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);

Expand Down Expand Up @@ -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, "/")
Expand Down
18 changes: 5 additions & 13 deletions src/lib/converter/nodes/block.ts
Expand Up @@ -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<
Expand All @@ -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);
});
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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, "/");
}
Expand Down

0 comments on commit ac4c688

Please sign in to comment.