Skip to content

Commit 08f5fbd

Browse files
Feature: generator configuration builder tests (#26)
1 parent d92b76d commit 08f5fbd

File tree

11 files changed

+213
-60
lines changed

11 files changed

+213
-60
lines changed

packages/ts-docs-gen/src/builders/generator-configuration-builder.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,17 @@ import { DefaultPlugins } from "../default-plugins";
1010

1111
// TODO: Add method to read compiler options from tsconfig.
1212
export class GeneratorConfigurationBuilder {
13-
constructor(private projectDirectory: string) { }
13+
constructor(private projectDirectory: string) {
14+
this.configuration.ProjectDirectory = projectDirectory;
15+
}
1416

1517
private configuration: Partial<WorkingGeneratorConfiguration> = {};
1618
private compilerOptions: Partial<ts.CompilerOptions>;
1719

20+
private resolveProjectDirectory(): string {
21+
return this.configuration.ProjectDirectory || this.projectDirectory;
22+
}
23+
1824
public OverrideCompilerOptions(compilerOptions: Partial<ts.CompilerOptions>): this {
1925
this.compilerOptions = {
2026
...this.compilerOptions,
@@ -71,16 +77,18 @@ export class GeneratorConfigurationBuilder {
7177
// Resolve tsconfig
7278
let compilerOptions = this.compilerOptions;
7379
if (compilerOptions == null) {
74-
compilerOptions = await GetCompilerOptions(path.join(this.projectDirectory, "tsconfig.json"));
80+
compilerOptions = await GetCompilerOptions(path.join(this.resolveProjectDirectory(), "tsconfig.json"));
7581
}
7682

7783
// Extractor
7884
const extractor = new Extractor({
7985
CompilerOptions: compilerOptions,
80-
ProjectDirectory: this.projectDirectory
86+
ProjectDirectory: this.resolveProjectDirectory(),
87+
Exclude: this.configuration.Exclude,
88+
OutputPathSeparator: this.configuration.OutputPathSeparator
8189
});
8290

83-
const outputDirectory = this.configuration.OutputDirectory || path.join(this.projectDirectory, "/docs/");
91+
const outputDirectory = this.configuration.OutputDirectory || path.join(this.resolveProjectDirectory(), "/docs/");
8492

8593
return {
8694
PluginManager: pluginManager,

packages/ts-docs-gen/tests/cases/__tests__/__snapshots__/simple-project-4.test.ts.snap

Lines changed: 48 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
{
2-
"compilerOptions": {
3-
"module": "commonjs",
4-
"target": "es6",
5-
"removeComments": false,
6-
"outDir": "dist",
7-
"rootDir": "src",
8-
"inlineSourceMap": true,
9-
"inlineSources": true,
10-
"skipDefaultLibCheck": true,
11-
"declaration": true,
12-
"pretty": true,
13-
"strict": true,
14-
"forceConsistentCasingInFileNames": true,
15-
"lib": [
16-
"es6"
17-
],
18-
"typeRoots": [
19-
"./node_modules/@types"
20-
]
21-
},
22-
"exclude": [
23-
"node_modules",
24-
"dist",
25-
"@types",
26-
"tests",
27-
"examples"
28-
]
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"target": "es6",
5+
"removeComments": false,
6+
"outDir": "dist",
7+
"rootDir": "src",
8+
"inlineSourceMap": true,
9+
"inlineSources": true,
10+
"skipDefaultLibCheck": true,
11+
"declaration": true,
12+
"pretty": true,
13+
"strict": true,
14+
"forceConsistentCasingInFileNames": true,
15+
"lib": [
16+
"es6"
17+
],
18+
"typeRoots": [
19+
"./node_modules/@types"
20+
]
21+
},
22+
"exclude": [
23+
"node_modules",
24+
"dist",
25+
"@types",
26+
"tests",
27+
"examples"
28+
]
2929
}
Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
{
2-
"compilerOptions": {
3-
"module": "commonjs",
4-
"target": "es6",
5-
"removeComments": false,
6-
"outDir": "dist",
7-
"rootDir": "src",
8-
"inlineSourceMap": true,
9-
"inlineSources": true,
10-
"skipDefaultLibCheck": true,
11-
"declaration": true,
12-
"pretty": true,
13-
"strict": true,
14-
"forceConsistentCasingInFileNames": true,
15-
"lib": [
16-
"es6"
17-
],
18-
"typeRoots": [
19-
"./node_modules/@types"
20-
]
21-
},
22-
"exclude": [
23-
"node_modules",
24-
"dist",
25-
"@types",
26-
"tests",
27-
"examples"
28-
]
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"target": "es6",
5+
"removeComments": false,
6+
"outDir": "dist",
7+
"rootDir": "src",
8+
"inlineSourceMap": true,
9+
"inlineSources": true,
10+
"skipDefaultLibCheck": true,
11+
"declaration": true,
12+
"pretty": true,
13+
"strict": true,
14+
"forceConsistentCasingInFileNames": true,
15+
"lib": [
16+
"es6"
17+
],
18+
"typeRoots": [
19+
"./node_modules/@types"
20+
]
21+
},
22+
"exclude": [
23+
"node_modules",
24+
"dist",
25+
"@types",
26+
"tests",
27+
"examples"
28+
]
2929
}

packages/ts-docs-gen/tests/cases/simple-project-3/tsconfig.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,4 @@
2626
"tests",
2727
"examples"
2828
]
29-
}
30-
29+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import * as path from "path";
2+
import { ModuleKind, ScriptTarget } from "typescript";
3+
import { Generator } from "@src/generator";
4+
import { GeneratorConfigurationBuilder } from "@src/builders/generator-configuration-builder";
5+
import { VariablePlugin } from "../simple-project-4/src/variable-plugin";
6+
7+
test("{{caseName}}", async done => {
8+
const projectDirectory = "{{projectDirectory}}";
9+
const testConfig = {{{json testConfig}}};
10+
const entryFiles = testConfig.EntryFiles;
11+
12+
try {
13+
const myCompilerOptions = {
14+
module: ModuleKind.CommonJS,
15+
target: ScriptTarget.ES2015,
16+
removeComments: false,
17+
outDir: "dist",
18+
rootDir: "src",
19+
inlineSourceMap: true,
20+
inlineSources: true,
21+
skipDefaultLibCheck: true,
22+
declaration: true,
23+
pretty: true,
24+
strict: false,
25+
forceConsistentCasingInFileNames: true,
26+
lib: [
27+
"es6",
28+
"dom"
29+
],
30+
typeRoots: [
31+
"../../../../node_modules/@types"
32+
]
33+
};
34+
35+
const configuration = await new GeneratorConfigurationBuilder(projectDirectory)
36+
.AddPlugins([new VariablePlugin])
37+
.OverrideCompilerOptions(myCompilerOptions)
38+
.OverrideConfiguration({
39+
ProjectDirectory: path.join(projectDirectory, "src/project-to-doc/custom"),
40+
Exclude: ["./to-exclude.ts"]
41+
})
42+
.SetOutputDirectory("./docs")
43+
.Build(entryFiles);
44+
45+
const generator = new Generator(configuration);
46+
47+
expect(generator.OutputData).toMatchSnapshot();
48+
done();
49+
} catch (error) {
50+
done.fail(error);
51+
}
52+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const CustomSecretRandomNumber = "5";
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// FIXME: Not implemented in ts-extractor yet. Update snapshot when implemented.
2+
export const ExcludedFileValue = "13";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const SecretRandomNumber = "4";
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { Contracts } from "ts-extractor";
2+
import { BasePlugin } from "@src/abstractions/base-plugin";
3+
import { SupportedApiItemKindType, PluginResult, PluginOptions } from "@src/contracts/plugin";
4+
import { GeneratorHelpers } from "@src/generator-helpers";
5+
import { MarkdownBuilder } from "@simplrjs/markdown";
6+
7+
export class VariablePlugin extends BasePlugin<Contracts.ApiVariableDto> {
8+
public SupportedApiItemKinds(): SupportedApiItemKindType[] {
9+
return [GeneratorHelpers.ApiItemKinds.Variable];
10+
}
11+
12+
public Render(options: PluginOptions<Contracts.ApiVariableDto>): PluginResult<Contracts.ApiVariableDto> {
13+
const heading: string = `My variable ${options.Reference.Alias}`;
14+
15+
const pluginResult: PluginResult<Contracts.ApiVariableDto> = {
16+
...GeneratorHelpers.GetDefaultPluginResultData(),
17+
ApiItem: options.ApiItem,
18+
Reference: options.Reference,
19+
Headings: [
20+
{
21+
ApiItemId: options.Reference.Id,
22+
Heading: heading
23+
}
24+
],
25+
UsedReferences: [options.Reference.Id]
26+
};
27+
28+
pluginResult.Result = new MarkdownBuilder()
29+
.Header(heading, 3)
30+
.EmptyLine()
31+
.Text("Variable plugin result.")
32+
.GetOutput();
33+
34+
return pluginResult;
35+
}
36+
}

0 commit comments

Comments
 (0)