Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
service_name: travis-ci
4 changes: 4 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@types/* linguist-generated=true
dist/* linguist-generated=true
**/__snapshots__/* linguist-generated=true
**/examples/* linguist-generated=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# exported-const-variables

## Kintamasis

First const variable in the exported items.

```ts
const Kintamasis: "Hello World!";
```

### Type

"Hello World!"

## Kintamasis2

```ts
const Kintamasis2: "World, Hello!";
```

### Type

"World, Hello!"

## Kintamasis3

```ts
const Kintamasis3: "Not imported!";
```

### Type

"Not imported!"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# exported-functions

## function: Foo

## function: Bar
28 changes: 28 additions & 0 deletions packages/ts-docs-gen/examples/simple/docs/api/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[ClassDeclaration-0]: index.md#class-world
[ClassDeclaration-1]: index.md#class-earth
[FunctionDeclaration-0]: exported-functions.md#function-foo
# index

## class: World

## class: Earth

## Hello

```typescript
const Hello: World & Earth;
```

### Type

[World][ClassDeclaration-0] & [Earth][ClassDeclaration-1]

## FooFunc

```typescript
const FooFunc: () => string;
```

### Type

[Foo][FunctionDeclaration-0]
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* First const variable in the exported items.
*/
export const Kintamasis = "Hello World!";
export const Kintamasis2 = "Hello World!";
export const Kintamasis2 = "World, Hello!";
export const Kintamasis3 = "Not imported!";
15 changes: 11 additions & 4 deletions packages/ts-docs-gen/examples/simple/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
// tslint:disable

// import { MyInterface } from "./my-types";
import { Foo } from "./exported-functions";

export class World { }
export class Earth { }

export declare const Hello: World & Earth;

export const FooFunc = Foo;

// export function Foo(): string {
// return "foo";
Expand Down Expand Up @@ -91,9 +98,9 @@
// Type: TType;
// }

export async function GetFoo(): Promise<void> {
return;
}
// export async function GetFoo(): Promise<void> {
// return;
// }

// export interface Bar extends Foo<number>, Boo {
// OtherStuff: string[];
Expand Down
5 changes: 3 additions & 2 deletions packages/ts-docs-gen/src/abstractions/api-item-plugin-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { Contracts } from "ts-extractor";

import { RenderItemOutputDto } from "../contracts/render-item-output-dto";
import { SupportedApiItemKindType, ApiItemKindsAdditional } from "../contracts/supported-api-item-kind-type";
import { PluginData } from "../contracts/plugin-data";

export abstract class ApiItemPluginBase {
export abstract class ApiItemPluginBase<TKind = Contracts.ApiItemDto> {
// TODO: Clarify naming.
protected get SupportKind(): typeof Contracts.ApiItemKinds & typeof ApiItemKindsAdditional {
return Object.assign(Contracts.ApiItemKinds, ApiItemKindsAdditional);
Expand All @@ -15,5 +16,5 @@ export abstract class ApiItemPluginBase {
return true;
}

public abstract Render(item: Contracts.ApiItemDto, getItem: (itemId: string) => RenderItemOutputDto): RenderItemOutputDto;
public abstract Render(data: PluginData<TKind>): RenderItemOutputDto;
}
9 changes: 9 additions & 0 deletions packages/ts-docs-gen/src/abstractions/file-manager-base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Contracts } from "ts-extractor";
import { RenderItemOutputDto } from "../contracts/render-item-output-dto";
import { FileOutputDto } from "../contracts/file-output-dto";

export abstract class FileManagerBaseBase {
public abstract AddItem(entryFile: Contracts.ApiSourceFileDto, item: RenderItemOutputDto, referenceId: string): void;

public abstract ToFilesOutput(): FileOutputDto[];
}
5 changes: 0 additions & 5 deletions packages/ts-docs-gen/src/abstractions/printer-base.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ApiItemPluginBase } from "../abstractions/api-item-plugin-base";
import { GeneratorConfiguration, WorkingGeneratorConfiguration } from "../contracts/generator-configuration";

import { PluginRegistry } from "../registries/plugin-registry";
import { DefaultPlugins } from "../default-plugins";

// TODO: Add method to read compiler options from tsconfig.
export class GeneratorConfigurationBuilder {
Expand Down Expand Up @@ -53,6 +54,11 @@ export class GeneratorConfigurationBuilder {
public async Build(entryFiles: string[]): Promise<GeneratorConfiguration> {
// Register all plugins.
const pluginManager = new PluginRegistry();
// Register default plugins
for (const item of DefaultPlugins) {
pluginManager.Register(item);
}

if (this.configuration.Plugins != null) {
// TODO: Register default plugins.
// Registering given plugins.
Expand Down
4 changes: 4 additions & 0 deletions packages/ts-docs-gen/src/contracts/file-output-dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface FileOutputDto {
FileLocation: string;
Output: string[];
}
9 changes: 9 additions & 0 deletions packages/ts-docs-gen/src/contracts/plugin-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ReferenceTuple } from "./reference-tuple";
import { RenderItemOutputDto } from "./render-item-output-dto";
import { Contracts } from "ts-extractor";

export interface PluginData<TKind = Contracts.ApiItemDto> {
Reference: ReferenceTuple;
ApiItem: TKind;
GetItem(entryFile: Contracts.ApiSourceFileDto, reference: ReferenceTuple): RenderItemOutputDto;
}
4 changes: 4 additions & 0 deletions packages/ts-docs-gen/src/contracts/reference-tuple.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* [referenceId, alias]
*/
export type ReferenceTuple = [string, string];
4 changes: 4 additions & 0 deletions packages/ts-docs-gen/src/contracts/render-item-output-dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { Contracts } from "ts-extractor";

export interface RenderItemOutputDto {
References: string[];
/**
* Heading is used for navigation in documentation. It should be the same in the render output.
*/
Heading: string;
RenderOutput: string[];
ApiItem: Contracts.ApiItemDto;
}
8 changes: 5 additions & 3 deletions packages/ts-docs-gen/src/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ import { Generator } from "./generator";

async function Main(): Promise<void> {
const projectDirectory = path.join(process.cwd(), "./examples/simple/");
const entryFiles = ["./index.ts", "./exported-const-variables.ts", "./exported-functions.ts"];
// const entryFiles = ["./index.ts", "./exported-const-variables.ts", "./exported-functions.ts"];
const entryFiles = ["./index.ts", "./exported-functions.ts"];

const configPromise = new GeneratorConfigurationBuilder(projectDirectory).Build(entryFiles);
const config = await configPromise;

const generator = new Generator(config);
generator.PrintToFiles();
// tslint:disable-next-line:no-console
console.log(generator.OutputData);
// tslint:disable-next-line:no-debugger
debugger;
await generator.WriteToFiles();
}

Main();
5 changes: 5 additions & 0 deletions packages/ts-docs-gen/src/default-plugins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { ApiVariablePlugin } from "./plugins/api-variable-plugin";

export const DefaultPlugins = [
new ApiVariablePlugin()
];
47 changes: 47 additions & 0 deletions packages/ts-docs-gen/src/extractor-helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Contracts, ExtractDto } from "ts-extractor";
import { ReferenceTuple } from "./contracts/reference-tuple";

export namespace ExtractorHelpers {
export function GetReferenceTuples(
extractedData: ExtractDto,
entryFile: Contracts.ApiSourceFileDto,
itemsReference: Contracts.ApiItemReferenceTuple
): ReferenceTuple[] {
let list: ReferenceTuple[] = [];

for (const [alias, references] of itemsReference) {
for (const referenceId of references) {
// Check if item is ExportSpecifier or ExportDeclaration.
const apiItem = extractedData.Registry[referenceId];

switch (apiItem.ApiKind) {
case Contracts.ApiItemKinds.Export: {
const referenceTuples = GetReferenceTuples(extractedData, entryFile, apiItem.Members);
list = list.concat(referenceTuples);
break;
}
case Contracts.ApiItemKinds.ExportSpecifier: {
if (apiItem.ApiItems == null) {
console.warn(`ApiItems are missing in "${apiItem.Name}"?`);
break;
}
const referenceTuples = GetReferenceTuples(extractedData, entryFile, [[apiItem.Name, apiItem.ApiItems]]);
list = list.concat(referenceTuples);
break;
}
default: {
list.push([referenceId, alias]);
}
}
}
}

return list;
}

export function ApiVariableToString(item: Contracts.ApiVariableDto, alias?: string): string {
const name = alias != null ? alias : item.Name;

return `${item.VariableDeclarationType} ${name}: ${item.Type.Text};`;
}
}
81 changes: 81 additions & 0 deletions packages/ts-docs-gen/src/file-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { Contracts } from "ts-extractor";
import { MarkdownGenerator } from "@simplrjs/markdown";
import * as path from "path";

import { FileManagerBaseBase } from "./abstractions/file-manager-base";
import { RenderItemOutputDto } from "./contracts/render-item-output-dto";
import { FileOutputDto } from "./contracts/file-output-dto";
import { Helpers } from "./utils/helpers";

interface OutputData {
RenderOutput: string[];
}

type RenderedItem = Array<RenderItemOutputDto | OutputData>;

export class FileManager extends FileManagerBaseBase {
private filesList: Map<string, RenderedItem> = new Map();
private referenceToFile: Map<string, string> = new Map();

private fileHeader(entryFile: Contracts.ApiSourceFileDto): OutputData {
const heading = path.basename(entryFile.Name, path.extname(entryFile.Name));

const output: string[] = [
MarkdownGenerator.header(`${heading}`, 1)
];

return {
RenderOutput: output
};
}

private getDefaultEntryFile(entryFile: Contracts.ApiSourceFileDto): RenderedItem {
return [
this.fileHeader(entryFile)
];
}

private renderItemIsItemOutputDto(item: RenderItemOutputDto | OutputData): item is RenderItemOutputDto {
return (item as RenderItemOutputDto).ApiItem != null;
}

public AddItem(entryFile: Contracts.ApiSourceFileDto, item: RenderItemOutputDto, referenceId: string): void {
const fileName = path.basename(entryFile.Name, path.extname(entryFile.Name)) + ".md";
const items = this.filesList.get(fileName) || this.getDefaultEntryFile(entryFile);
items.push(item);

// Add reference link.
this.referenceToFile.set(referenceId, `${fileName}#${Helpers.HeadingToAnchor(item.Heading)}`);

this.filesList.set(fileName, items);
}

public ToFilesOutput(): FileOutputDto[] {
const output: FileOutputDto[] = [];

for (const [fileLocation, items] of this.filesList) {
// Link definitions to file location.
const references: string[] = [];
for (const item of items) {
if (this.renderItemIsItemOutputDto(item)) {
item.References
.forEach(referenceId =>
references.push(
MarkdownGenerator.linkDefinition(referenceId, this.referenceToFile.get(referenceId) || "#__error")
)
);
}
}

output.push({
FileLocation: fileLocation,
Output: [
...references,
...Helpers.Flatten(items.map(x => [x.RenderOutput, ""]))
]
});
}

return output;
}
}
Loading