Repository: serverless/serverless-azure-functions
Pull request #198 (2019-07-11 20:31:36)

feat: Add options to base plugin class
Allows base plugin to access options and adds helper function `getOption` for all plugins to use

coveralls: 
[![Coverage Status](https://coveralls.io/builds/24513938/badge)](https://coveralls.io/builds/24513938)

Coverage increased (+7.8%) to 84.169% when pulling **11d4b2eb0321f9dcb9cb1c1e3091f1745ba5b475 on tabarlow/base-plugin-options** into **4208625bc31ebec5efb4971386ab203fea1321f7 on dev**.


Patch:
diff --git a/src/plugins/apim/azureApimFunctionPlugin.ts b/src/plugins/apim/azureApimFunctionPlugin.ts
index da6cec6..ff8010a 100644
--- a/src/plugins/apim/azureApimFunctionPlugin.ts
+++ b/src/plugins/apim/azureApimFunctionPlugin.ts
@@ -5,8 +5,8 @@ import { AzureBasePlugin } from "../azureBasePlugin";
 export class AzureApimFunctionPlugin extends AzureBasePlugin {
   public hooks: { [eventName: string]: Promise<any> };
 
-  public constructor(serverless: Serverless, private options: Serverless.Options) {
-    super(serverless);
+  public constructor(serverless: Serverless, options: Serverless.Options) {
+    super(serverless, options);
     this.hooks = {
       "after:deploy:function:deploy": this.deploy.bind(this)
     };
diff --git a/src/plugins/apim/azureApimServicePlugin.ts b/src/plugins/apim/azureApimServicePlugin.ts
index 92d4739..3aa6e5e 100644
--- a/src/plugins/apim/azureApimServicePlugin.ts
+++ b/src/plugins/apim/azureApimServicePlugin.ts
@@ -5,8 +5,8 @@ import { AzureBasePlugin } from "../azureBasePlugin";
 export class AzureApimServicePlugin extends AzureBasePlugin {
   public hooks: { [eventName: string]: Promise<any> };
 
-  public constructor(serverless: Serverless, private options: Serverless.Options) {
-    super(serverless);
+  public constructor(serverless: Serverless, options: Serverless.Options) {
+    super(serverless, options);
     this.hooks = {
       "after:deploy:deploy": this.deploy.bind(this)
     };
diff --git a/src/plugins/azureBasePlugin.ts b/src/plugins/azureBasePlugin.ts
index 0869bad..e90b584 100644
--- a/src/plugins/azureBasePlugin.ts
+++ b/src/plugins/azureBasePlugin.ts
@@ -1,9 +1,11 @@
 import { Guard } from "../shared/guard";
 import Serverless from "serverless";
+import { Utils } from "../shared/utils";
 
 export abstract class AzureBasePlugin {
   public constructor(
     protected serverless: Serverless,
+    protected options: Serverless.Options,
   ) {
     Guard.null(serverless);
   }
@@ -11,4 +13,8 @@ export abstract class AzureBasePlugin {
   protected log(message: string) {
     this.serverless.cli.log(message);
   }
+
+  protected getOption(key: string, defaultValue?: any): string {
+    return Utils.get(this.options, key, defaultValue);
+  }
 }
diff --git a/src/plugins/deploy/azureDeployPlugin.ts b/src/plugins/deploy/azureDeployPlugin.ts
index 52e451c..945b672 100644
--- a/src/plugins/deploy/azureDeployPlugin.ts
+++ b/src/plugins/deploy/azureDeployPlugin.ts
@@ -4,12 +4,13 @@ import { ResourceService } from "../../services/resourceService";
 import { Utils } from "../../shared/utils";
 import { AzureBasePlugin } from "../azureBasePlugin";
 
+
 export class AzureDeployPlugin extends AzureBasePlugin {
   public hooks: { [eventName: string]: Promise<any> };
   public commands: any;
 
-  public constructor(serverless: Serverless, private options: Serverless.Options) {
-    super(serverless);
+  public constructor(serverless: Serverless, options: Serverless.Options) {
+    super(serverless, options);
 
     this.hooks = {
       "deploy:deploy": this.deploy.bind(this),
diff --git a/src/plugins/func/azureFuncPlugin.ts b/src/plugins/func/azureFuncPlugin.ts
index 09ec9ad..599f6fb 100644
--- a/src/plugins/func/azureFuncPlugin.ts
+++ b/src/plugins/func/azureFuncPlugin.ts
@@ -7,8 +7,8 @@ export class AzureFuncPlugin extends AzureBasePlugin {
   public commands: any;
   private service: FuncService;
 
-  public constructor(serverless: Serverless, private options: Serverless.Options) {
-    super(serverless);
+  public constructor(serverless: Serverless, options: Serverless.Options) {
+    super(serverless, options);
 
     this.hooks = {
       "func:func": this.func.bind(this),
diff --git a/src/plugins/invoke/azureInvokePlugin.ts b/src/plugins/invoke/azureInvokePlugin.ts
index 0905530..4c18936 100644
--- a/src/plugins/invoke/azureInvokePlugin.ts
+++ b/src/plugins/invoke/azureInvokePlugin.ts
@@ -9,8 +9,8 @@ export class AzureInvokePlugin extends AzureBasePlugin {
   public hooks: { [eventName: string]: Promise<any> };
   private commands: ServerlessCommandMap;
   private invokeService: InvokeService;
-  public constructor(serverless: Serverless, private options: Serverless.Options) {
-    super(serverless);
+  public constructor(serverless: Serverless, options: Serverless.Options) {
+    super(serverless, options);
     const path = this.options["path"];
 
     if (path) {
diff --git a/src/plugins/login/azureLoginPlugin.ts b/src/plugins/login/azureLoginPlugin.ts
index d59b049..188086f 100644
--- a/src/plugins/login/azureLoginPlugin.ts
+++ b/src/plugins/login/azureLoginPlugin.ts
@@ -7,8 +7,8 @@ export class AzureLoginPlugin extends AzureBasePlugin {
   private provider: AzureProvider;
   public hooks: { [eventName: string]: Promise<any> };
 
-  public constructor(serverless: Serverless, private options: Serverless.Options) {
-    super(serverless);
+  public constructor(serverless: Serverless, options: Serverless.Options) {
+    super(serverless, options);
     this.provider = (this.serverless.getProvider("azure") as any) as AzureProvider;
 
     this.hooks = {
diff --git a/src/plugins/logs/azureLogsPlugin.ts b/src/plugins/logs/azureLogsPlugin.ts
index 05e7c81..3c4acc8 100644
--- a/src/plugins/logs/azureLogsPlugin.ts
+++ b/src/plugins/logs/azureLogsPlugin.ts
@@ -7,8 +7,8 @@ export class AzureLogsPlugin extends AzureBasePlugin {
 
   private provider: AzureProvider;
 
-  public constructor(serverless: Serverless, private options: Serverless.Options) {
-    super(serverless);
+  public constructor(serverless: Serverless, options: Serverless.Options) {
+    super(serverless, options);
 
     this.provider = (this.serverless.getProvider("azure") as any) as AzureProvider;
 
diff --git a/src/plugins/offline/azureOfflinePlugin.ts b/src/plugins/offline/azureOfflinePlugin.ts
index a3b1ad4..2f5f879 100644
--- a/src/plugins/offline/azureOfflinePlugin.ts
+++ b/src/plugins/offline/azureOfflinePlugin.ts
@@ -7,8 +7,8 @@ export class AzureOfflinePlugin extends AzureBasePlugin {
   public commands: any;
   private offlineService: OfflineService;
 
-  public constructor(serverless: Serverless, private options: Serverless.Options) {
-    super(serverless);
+  public constructor(serverless: Serverless, options: Serverless.Options) {
+    super(serverless, options);
     this.offlineService = new OfflineService(this.serverless, this.options);
 
     this.hooks = {
diff --git a/src/plugins/package/azurePackagePlugin.ts b/src/plugins/package/azurePackagePlugin.ts
index ef317a3..abfb7b9 100644
--- a/src/plugins/package/azurePackagePlugin.ts
+++ b/src/plugins/package/azurePackagePlugin.ts
@@ -10,8 +10,8 @@ export class AzurePackagePlugin extends AzureBasePlugin {
   public provider: AzureProvider;
   public hooks: { [eventName: string]: Promise<any> };
 
-  public constructor(serverless: Serverless) {
-    super(serverless);
+  public constructor(serverless: Serverless, options: Serverless.Options) {
+    super(serverless, options);
     this.hooks = {
       "before:package:setupProviderConfiguration": this.setupProviderConfiguration.bind(this),
       "before:webpack:package:packageModules": this.webpack.bind(this),
diff --git a/src/plugins/remove/azureRemovePlugin.ts b/src/plugins/remove/azureRemovePlugin.ts
index 4eb2928..e4fe885 100644
--- a/src/plugins/remove/azureRemovePlugin.ts
+++ b/src/plugins/remove/azureRemovePlugin.ts
@@ -5,8 +5,8 @@ import { AzureBasePlugin } from "../azureBasePlugin";
 export class AzureRemovePlugin extends AzureBasePlugin {
   public hooks: { [eventName: string]: Promise<any> };
 
-  public constructor(serverless: Serverless, private options: Serverless.Options) {
-    super(serverless);
+  public constructor(serverless: Serverless, options: Serverless.Options) {
+    super(serverless, options);
     this.hooks = {
       "remove:remove": this.remove.bind(this)
     };
diff --git a/src/plugins/rollback/azureRollbackPlugin.ts b/src/plugins/rollback/azureRollbackPlugin.ts
index a3c24e3..438d77b 100644
--- a/src/plugins/rollback/azureRollbackPlugin.ts
+++ b/src/plugins/rollback/azureRollbackPlugin.ts
@@ -1,13 +1,15 @@
 import Serverless from "serverless";
 import { RollbackService } from "../../services/rollbackService";
+import { AzureBasePlugin } from "../azureBasePlugin";
 
 /**
  * Plugin for rolling back Function App Service to previous deployment
  */
-export class AzureRollbackPlugin {
+export class AzureRollbackPlugin extends AzureBasePlugin {
   public hooks: { [eventName: string]: Promise<any> };
 
-  public constructor(private serverless: Serverless, private options: Serverless.Options) {
+  public constructor(serverless: Serverless, options: Serverless.Options) {
+    super(serverless, options);
     this.hooks = {
       "rollback:rollback": this.rollback.bind(this)
     };


Test patch:
diff --git a/src/plugins/azureBasePlugin.test.ts b/src/plugins/azureBasePlugin.test.ts
index 3269408..e3a8d54 100644
--- a/src/plugins/azureBasePlugin.test.ts
+++ b/src/plugins/azureBasePlugin.test.ts
@@ -3,13 +3,17 @@ import Serverless from "serverless";
 import { MockFactory } from "../test/mockFactory";
 
 class MockPlugin extends AzureBasePlugin {
-  public constructor(serverless: Serverless) {
-    super(serverless);
+  public constructor(serverless: Serverless, options: Serverless.Options) {
+    super(serverless, options);
   }
 
   public logMessage(message: string) {
     this.log(message);
   }
+
+  public getServerlessOption(key: string, defaultValue?: any) {
+    return this.getOption(key, defaultValue);
+  }
 }
 
 describe("Azure Base Plugin", () => {
@@ -18,12 +22,22 @@ describe("Azure Base Plugin", () => {
 
   beforeEach(() => {
     sls = MockFactory.createTestServerless();
-    plugin = new MockPlugin(sls);
+    const options = MockFactory.createTestServerlessOptions();
+    options["key1"] = "val1";
+    options["key2"] = "val2";
+    plugin = new MockPlugin(sls, options);
   });
 
 
   it("logs a message", () => {
     plugin.logMessage("test message");
     expect(sls.cli.log).lastCalledWith("test message");
-  })
+  });
+
+  it("gets an option", () => {
+    expect(plugin.getServerlessOption("key1")).toEqual("val1");
+    expect(plugin.getServerlessOption("key2")).toEqual("val2");
+    expect(plugin.getServerlessOption("key3")).toBeUndefined();
+    expect(plugin.getServerlessOption("key3", "myValue")).toEqual("myValue");
+  });
 })
diff --git a/src/plugins/package/azurePackagePlugin.test.ts b/src/plugins/package/azurePackagePlugin.test.ts
index 34ddcd5..f40c307 100644
--- a/src/plugins/package/azurePackagePlugin.test.ts
+++ b/src/plugins/package/azurePackagePlugin.test.ts
@@ -12,8 +12,9 @@ describe("Azure Package Plugin", () => {
   beforeEach(() => {
     jest.resetAllMocks();
     sls = MockFactory.createTestServerless();
+    const options = MockFactory.createTestServerlessOptions();
 
-    plugin = new AzurePackagePlugin(sls);
+    plugin = new AzurePackagePlugin(sls, options);
   });
 
   it("sets creates function bindings before package:setupProviderConfiguration life cycle event", async () => {