Skip to content

Commit

Permalink
do not require plugins to use default exports
Browse files Browse the repository at this point in the history
  • Loading branch information
krzkaczor committed Aug 28, 2018
1 parent 8b995f4 commit 345323b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion example/ts-gen-plugins/typechain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface TOptions {
output?: string;
}

export default class Typechain extends TsGeneratorPlugin {
export class Typechain extends TsGeneratorPlugin {
name = "Typechain";

private readonly runtimePathAbs: string;
Expand Down
21 changes: 20 additions & 1 deletion src/plugins/loadPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
import { resolvePlugin } from "./resolvePlugin";
import { TDeps } from "../deps";
import { TContext, TsGeneratorPlugin } from "./types";
import { Dictionary } from "../stl";

export function loadPlugin(deps: TDeps, ctx: TContext): TsGeneratorPlugin {
const pluginPath = resolvePlugin(deps, ctx.rawConfig.generator, ctx.cwd);
const PluginCtr = require(pluginPath).default as { new (ctx: TContext): TsGeneratorPlugin };
const PluginModule = require(pluginPath);

const moduleExportsCount = Object.keys(PluginModule).length;
if (moduleExportsCount !== 1) {
throw new Error(
`Loading plugin ${
ctx.rawConfig.generator
} failed. Plugin module has to export exactly one entity. Found ${moduleExportsCount} instead`,
);
}

const PluginCtr = getFirstKey<{ new (ctx: TContext): TsGeneratorPlugin }>(PluginModule);

return new PluginCtr(ctx);
}

export function getFirstKey<T>(object: Dictionary<T>): T {
for (const k of Object.keys(object)) {
return object[k];
}
throw new Error("Any key missing!");
}
2 changes: 1 addition & 1 deletion test/integration/ts-gen-plugins/dummy-json/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { join } from "path";

const generateType = (name: string, type: string): string => `export const ${name}: ${type}`;

export default class JsonPlugin extends TsGeneratorPlugin {
export class JsonPlugin extends TsGeneratorPlugin {
name = "JsonPlugin";

transformFile({ path, contents }: TFileDesc): TFileDesc {
Expand Down

0 comments on commit 345323b

Please sign in to comment.