Skip to content

Commit

Permalink
fix() support passing options (plugins)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Oct 11, 2019
1 parent 9bfa1bf commit 9de7d80
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
29 changes: 21 additions & 8 deletions lib/compiler/plugins-loader.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import * as ts from 'typescript';
import { isObject } from 'util';
import { CLI_ERRORS } from '../ui';

type Transformer = ts.TransformerFactory<any> | ts.CustomTransformerFactory;
type PluginEntry = string | PluginAndOptions;

interface PluginAndOptions {
name: 'string';
options: Record<string, any>;
}

export interface NestCompilerPlugin {
before?: Transformer;
after?: Transformer;
before?: (options?: Record<string, any>) => Transformer;
after?: (options?: Record<string, any>) => Transformer;
}

export interface MultiNestCompilerPlugins {
Expand All @@ -14,18 +21,24 @@ export interface MultiNestCompilerPlugins {
}

export class PluginsLoader {
public load(plugins: string[] = []): MultiNestCompilerPlugins {
const pluginRefs: NestCompilerPlugin[] = (plugins || []).map(name =>
require(name),
public load(plugins: PluginEntry[] = []): MultiNestCompilerPlugins {
const pluginNames = plugins.map(entry =>
isObject(entry) ? (entry as PluginAndOptions).name : (entry as string),
);
const pluginRefs: NestCompilerPlugin[] = pluginNames.map(item =>
require(item),
);
const beforeHooks: Transformer[] = [];
const afterHooks: Transformer[] = [];
pluginRefs.forEach((plugin, index) => {
if (!plugin.before && !plugin.after) {
throw new Error(CLI_ERRORS.WRONG_PLUGIN(plugins[index]));
throw new Error(CLI_ERRORS.WRONG_PLUGIN(pluginNames[index]));
}
plugin.before && beforeHooks.push(plugin.before);
plugin.after && afterHooks.push(plugin.after);
const options = isObject(plugins[index])
? (plugins[index] as PluginAndOptions).options || {}
: {};
plugin.before && beforeHooks.push(plugin.before(options));
plugin.after && afterHooks.push(plugin.after(options));
});
return {
beforeHooks,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nestjs/cli",
"version": "6.10.1",
"version": "6.10.2",
"description": "Nest - modern, fast, powerful node.js web framework (@cli)",
"publishConfig": {
"access": "public"
Expand Down

0 comments on commit 9de7d80

Please sign in to comment.