Skip to content

Commit 22df574

Browse files
committed
BREAKING CHANGE: Plugins are now passed Application directly
This removes some tech debt, dropping one more `@Component` decorator instance.
1 parent 4c3ba35 commit 22df574

File tree

9 files changed

+143
-238
lines changed

9 files changed

+143
-238
lines changed

package-lock.json

Lines changed: 0 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,11 @@
3838
"@types/marked": "^2.0.2",
3939
"@types/minimatch": "3.0.4",
4040
"@types/mocha": "^8.2.2",
41-
"@types/mockery": "^1.4.29",
4241
"@types/node": "^15.0.1",
4342
"@typescript-eslint/eslint-plugin": "^4.22.0",
4443
"@typescript-eslint/parser": "^4.22.0",
4544
"eslint": "^7.25.0",
4645
"mocha": "^8.3.2",
47-
"mockery": "^2.1.0",
4846
"nyc": "^15.1.0",
4947
"prettier": "^2.2.1",
5048
"typescript": "^4.2.4"

src/lib/application.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import {
1010
Logger,
1111
ConsoleLogger,
1212
CallbackLogger,
13-
PluginHost,
13+
loadPlugins,
1414
normalizePath,
1515
writeFile,
16+
discoverNpmPlugins,
17+
NeverIfInternal,
1618
} from "./utils/index";
1719
import { createMinimatch } from "./utils/paths";
1820

@@ -78,8 +80,6 @@ export class Application extends ChildableComponent<
7880

7981
options: Options;
8082

81-
plugins: PluginHost;
82-
8383
@BindOption("logger")
8484
loggerType!: string | Function;
8585

@@ -114,7 +114,6 @@ export class Application extends ChildableComponent<
114114
this.serializer = new Serializer();
115115
this.converter = this.addComponent<Converter>("converter", Converter);
116116
this.renderer = this.addComponent<Renderer>("renderer", Renderer);
117-
this.plugins = this.addComponent("plugins", PluginHost);
118117
}
119118

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

145-
this.plugins.load();
144+
let plugins = this.options.getValue("plugin");
145+
if (plugins.length === 0) {
146+
plugins = discoverNpmPlugins(this);
147+
}
148+
loadPlugins(this, this.options.getValue("plugin"));
146149

147150
this.options.reset();
148151
for (const [key, val] of Object.entries(options)) {
@@ -158,8 +161,11 @@ export class Application extends ChildableComponent<
158161
/**
159162
* Return the application / root component instance.
160163
*/
161-
get application(): Application {
162-
return this;
164+
get application(): NeverIfInternal<Application> {
165+
this.logger.deprecated(
166+
"Application.application is deprecated. Plugins are now passed the application instance when loaded."
167+
);
168+
return this as never;
163169
}
164170

165171
/**
@@ -204,9 +210,9 @@ export class Application extends ChildableComponent<
204210

205211
const programs = [
206212
ts.createProgram({
207-
rootNames: this.application.options.getFileNames(),
208-
options: this.application.options.getCompilerOptions(),
209-
projectReferences: this.application.options.getProjectReferences(),
213+
rootNames: this.options.getFileNames(),
214+
options: this.options.getCompilerOptions(),
215+
projectReferences: this.options.getProjectReferences(),
210216
}),
211217
];
212218

@@ -238,7 +244,7 @@ export class Application extends ChildableComponent<
238244
return;
239245
}
240246

241-
if (this.application.options.getValue("emit")) {
247+
if (this.options.getValue("emit")) {
242248
for (const program of programs) {
243249
program.emit();
244250
}
@@ -284,7 +290,7 @@ export class Application extends ChildableComponent<
284290

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

309315
const host = ts.createWatchCompilerHost(
310316
tsconfigFile,
311-
{ noEmit: !this.application.options.getValue("emit") },
317+
{ noEmit: !this.options.getValue("emit") },
312318
ts.sys,
313319
ts.createEmitAndSemanticDiagnosticsBuilderProgram,
314320
(diagnostic) => this.logger.diagnostic(diagnostic),
@@ -401,7 +407,7 @@ export class Application extends ChildableComponent<
401407
end: eventData,
402408
});
403409

404-
const space = this.application.options.getValue("pretty") ? "\t" : "";
410+
const space = this.options.getValue("pretty") ? "\t" : "";
405411
await writeFile(out, JSON.stringify(ser, null, space));
406412
this.logger.info(`JSON written to ${out}`);
407413
}

src/lib/converter/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -861,8 +861,8 @@ const tupleConverter: TypeConverter<ts.TupleTypeNode, ts.TupleTypeReference> = {
861861
);
862862
return new TupleType(elements);
863863
},
864-
convertType(context, type) {
865-
const types = type.typeArguments?.slice(0, type.target.fixedLength);
864+
convertType(context, type, node) {
865+
const types = type.typeArguments?.slice(0, node.elements.length);
866866
let elements = types?.map((type) => convertType(context, type));
867867

868868
if (type.target.labeledElementDeclarations) {

src/lib/utils/component.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,17 @@ export abstract class AbstractComponent<O extends ComponentHost>
172172
* Return the application / root component instance.
173173
*/
174174
get application(): Application {
175-
return this._componentOwner === DUMMY_APPLICATION_OWNER
176-
? ((this as any) as Application)
177-
: this._componentOwner.application;
175+
if (this._componentOwner === DUMMY_APPLICATION_OWNER) {
176+
return (this as any) as Application;
177+
}
178+
// Temporary hack, Application.application is going away.
179+
if (
180+
this._componentOwner instanceof AbstractComponent &&
181+
this._componentOwner._componentOwner === DUMMY_APPLICATION_OWNER
182+
) {
183+
return (this._componentOwner as any) as Application;
184+
}
185+
return this._componentOwner.application;
178186
}
179187

180188
/**

src/lib/utils/fs.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ export async function remove(target: string) {
140140
// Since v14.14
141141
if (fsp.rm) {
142142
await fsp.rm(target, { recursive: true, force: true });
143-
} else {
143+
} else if (fs.existsSync(target)) {
144+
// Ew. We shouldn't need the exists check... Can't wait for Node 14.
144145
await fsp.rmdir(target, { recursive: true });
145146
}
146147
}

src/lib/utils/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ export {
2222
remove,
2323
} from "./fs";
2424
export { Logger, LogLevel, ConsoleLogger, CallbackLogger } from "./loggers";
25-
export { PluginHost } from "./plugins";
25+
export { loadPlugins, discoverNpmPlugins } from "./plugins";

0 commit comments

Comments
 (0)