Skip to content

Commit

Permalink
BREAKING CHANGE: Plugins are now passed Application directly
Browse files Browse the repository at this point in the history
This removes some tech debt, dropping one more `@Component` decorator instance.
  • Loading branch information
Gerrit0 committed May 1, 2021
1 parent 4c3ba35 commit 22df574
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 238 deletions.
12 changes: 0 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions package.json
Expand Up @@ -38,13 +38,11 @@
"@types/marked": "^2.0.2",
"@types/minimatch": "3.0.4",
"@types/mocha": "^8.2.2",
"@types/mockery": "^1.4.29",
"@types/node": "^15.0.1",
"@typescript-eslint/eslint-plugin": "^4.22.0",
"@typescript-eslint/parser": "^4.22.0",
"eslint": "^7.25.0",
"mocha": "^8.3.2",
"mockery": "^2.1.0",
"nyc": "^15.1.0",
"prettier": "^2.2.1",
"typescript": "^4.2.4"
Expand Down
34 changes: 20 additions & 14 deletions src/lib/application.ts
Expand Up @@ -10,9 +10,11 @@ import {
Logger,
ConsoleLogger,
CallbackLogger,
PluginHost,
loadPlugins,
normalizePath,
writeFile,
discoverNpmPlugins,
NeverIfInternal,
} from "./utils/index";
import { createMinimatch } from "./utils/paths";

Expand Down Expand Up @@ -78,8 +80,6 @@ export class Application extends ChildableComponent<

options: Options;

plugins: PluginHost;

@BindOption("logger")
loggerType!: string | Function;

Expand Down Expand Up @@ -114,7 +114,6 @@ export class Application extends ChildableComponent<
this.serializer = new Serializer();
this.converter = this.addComponent<Converter>("converter", Converter);
this.renderer = this.addComponent<Renderer>("renderer", Renderer);
this.plugins = this.addComponent("plugins", PluginHost);
}

/**
Expand Down Expand Up @@ -142,7 +141,11 @@ export class Application extends ChildableComponent<
}
this.logger.level = this.options.getValue("logLevel");

this.plugins.load();
let plugins = this.options.getValue("plugin");
if (plugins.length === 0) {
plugins = discoverNpmPlugins(this);
}
loadPlugins(this, this.options.getValue("plugin"));

this.options.reset();
for (const [key, val] of Object.entries(options)) {
Expand All @@ -158,8 +161,11 @@ export class Application extends ChildableComponent<
/**
* Return the application / root component instance.
*/
get application(): Application {
return this;
get application(): NeverIfInternal<Application> {
this.logger.deprecated(
"Application.application is deprecated. Plugins are now passed the application instance when loaded."
);
return this as never;
}

/**
Expand Down Expand Up @@ -204,9 +210,9 @@ export class Application extends ChildableComponent<

const programs = [
ts.createProgram({
rootNames: this.application.options.getFileNames(),
options: this.application.options.getCompilerOptions(),
projectReferences: this.application.options.getProjectReferences(),
rootNames: this.options.getFileNames(),
options: this.options.getCompilerOptions(),
projectReferences: this.options.getProjectReferences(),
}),
];

Expand Down Expand Up @@ -238,7 +244,7 @@ export class Application extends ChildableComponent<
return;
}

if (this.application.options.getValue("emit")) {
if (this.options.getValue("emit")) {
for (const program of programs) {
program.emit();
}
Expand Down Expand Up @@ -284,7 +290,7 @@ export class Application extends ChildableComponent<

// Doing this is considerably more complicated, we'd need to manage an array of programs, not convert until all programs
// have reported in the first time... just error out for now. I'm not convinced anyone will actually notice.
if (this.application.options.getFileNames().length === 0) {
if (this.options.getFileNames().length === 0) {
this.logger.error(
"The provided tsconfig file looks like a solution style tsconfig, which is not supported in watch mode."
);
Expand All @@ -308,7 +314,7 @@ export class Application extends ChildableComponent<

const host = ts.createWatchCompilerHost(
tsconfigFile,
{ noEmit: !this.application.options.getValue("emit") },
{ noEmit: !this.options.getValue("emit") },
ts.sys,
ts.createEmitAndSemanticDiagnosticsBuilderProgram,
(diagnostic) => this.logger.diagnostic(diagnostic),
Expand Down Expand Up @@ -401,7 +407,7 @@ export class Application extends ChildableComponent<
end: eventData,
});

const space = this.application.options.getValue("pretty") ? "\t" : "";
const space = this.options.getValue("pretty") ? "\t" : "";
await writeFile(out, JSON.stringify(ser, null, space));
this.logger.info(`JSON written to ${out}`);
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/converter/types.ts
Expand Up @@ -861,8 +861,8 @@ const tupleConverter: TypeConverter<ts.TupleTypeNode, ts.TupleTypeReference> = {
);
return new TupleType(elements);
},
convertType(context, type) {
const types = type.typeArguments?.slice(0, type.target.fixedLength);
convertType(context, type, node) {
const types = type.typeArguments?.slice(0, node.elements.length);
let elements = types?.map((type) => convertType(context, type));

if (type.target.labeledElementDeclarations) {
Expand Down
14 changes: 11 additions & 3 deletions src/lib/utils/component.ts
Expand Up @@ -172,9 +172,17 @@ export abstract class AbstractComponent<O extends ComponentHost>
* Return the application / root component instance.
*/
get application(): Application {
return this._componentOwner === DUMMY_APPLICATION_OWNER
? ((this as any) as Application)
: this._componentOwner.application;
if (this._componentOwner === DUMMY_APPLICATION_OWNER) {
return (this as any) as Application;
}
// Temporary hack, Application.application is going away.
if (
this._componentOwner instanceof AbstractComponent &&
this._componentOwner._componentOwner === DUMMY_APPLICATION_OWNER
) {
return (this._componentOwner as any) as Application;
}
return this._componentOwner.application;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/lib/utils/fs.ts
Expand Up @@ -140,7 +140,8 @@ export async function remove(target: string) {
// Since v14.14
if (fsp.rm) {
await fsp.rm(target, { recursive: true, force: true });
} else {
} else if (fs.existsSync(target)) {
// Ew. We shouldn't need the exists check... Can't wait for Node 14.
await fsp.rmdir(target, { recursive: true });
}
}
2 changes: 1 addition & 1 deletion src/lib/utils/index.ts
Expand Up @@ -22,4 +22,4 @@ export {
remove,
} from "./fs";
export { Logger, LogLevel, ConsoleLogger, CallbackLogger } from "./loggers";
export { PluginHost } from "./plugins";
export { loadPlugins, discoverNpmPlugins } from "./plugins";

0 comments on commit 22df574

Please sign in to comment.