From 9de7d809a1410ca2d31216b5236dd9cc13d08908 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20My=C5=9Bliwiec?= Date: Fri, 11 Oct 2019 21:59:24 +0200 Subject: [PATCH] fix() support passing options (plugins) --- lib/compiler/plugins-loader.ts | 29 +++++++++++++++++++++-------- package.json | 2 +- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/lib/compiler/plugins-loader.ts b/lib/compiler/plugins-loader.ts index 49df96a42..c2c81c5cb 100644 --- a/lib/compiler/plugins-loader.ts +++ b/lib/compiler/plugins-loader.ts @@ -1,11 +1,18 @@ import * as ts from 'typescript'; +import { isObject } from 'util'; import { CLI_ERRORS } from '../ui'; type Transformer = ts.TransformerFactory | ts.CustomTransformerFactory; +type PluginEntry = string | PluginAndOptions; + +interface PluginAndOptions { + name: 'string'; + options: Record; +} export interface NestCompilerPlugin { - before?: Transformer; - after?: Transformer; + before?: (options?: Record) => Transformer; + after?: (options?: Record) => Transformer; } export interface MultiNestCompilerPlugins { @@ -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, diff --git a/package.json b/package.json index 514a80064..fec7bdcd7 100644 --- a/package.json +++ b/package.json @@ -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"