-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
do not require plugins to use default exports
- Loading branch information
Showing
3 changed files
with
22 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters