Skip to content

Commit

Permalink
moved shouldSkipPlugin to constructor (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
karopolopoulos authored Aug 21, 2020
1 parent 6a5e9f6 commit 6729eb0
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 51 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "serverless-newrelic-lambda-layers",
"version": "0.1.19",
"version": "0.1.20",
"description": "Serverless plugin for NewRelic APM AWS Lambda layers.",
"main": "dist/index.js",
"files": [
Expand Down
42 changes: 15 additions & 27 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ export default class NewRelicLambdaLayerPlugin {
this.serverless = serverless;
this.options = options;
this.awsProvider = this.serverless.getProvider("aws") as any;
this.hooks = {
"after:deploy:deploy": this.addLogSubscriptions.bind(this),
"after:deploy:function:packageFunction": this.cleanup.bind(this),
"after:package:createDeploymentArtifacts": this.cleanup.bind(this),
"before:deploy:function:packageFunction": this.run.bind(this),
"before:package:createDeploymentArtifacts": this.run.bind(this),
"before:remove:remove": this.removeLogSubscriptions.bind(this)
};
this.hooks = this.shouldSkipPlugin()
? {}
: {
"after:deploy:deploy": this.addLogSubscriptions.bind(this),
"after:deploy:function:packageFunction": this.cleanup.bind(this),
"after:package:createDeploymentArtifacts": this.cleanup.bind(this),
"before:deploy:function:packageFunction": this.run.bind(this),
"before:package:createDeploymentArtifacts": this.run.bind(this),
"before:remove:remove": this.removeLogSubscriptions.bind(this)
};
}

get config() {
Expand Down Expand Up @@ -69,13 +71,6 @@ export default class NewRelicLambdaLayerPlugin {
}

public async run() {
if (this.shouldSkipPlugin()) {
this.serverless.cli.log(
`Skipping plugin serverless-newrelic-lambda-layers for stage ${this.stage}`
);
return;
}

const version = this.serverless.getVersion();
if (semver.lt(version, "1.34.0")) {
this.serverless.cli.log(
Expand Down Expand Up @@ -119,18 +114,10 @@ export default class NewRelicLambdaLayerPlugin {
}

public cleanup() {
if (this.shouldSkipPlugin()) {
return;
}

this.removeNodeHelper();
}

public async addLogSubscriptions() {
if (this.shouldSkipPlugin()) {
return;
}

if (this.autoSubscriptionDisabled) {
this.serverless.cli.log(
"Skipping adding log subscription. Explicitly disabled"
Expand Down Expand Up @@ -175,10 +162,6 @@ export default class NewRelicLambdaLayerPlugin {
}

public async removeLogSubscriptions() {
if (this.shouldSkipPlugin()) {
return;
}

if (this.autoSubscriptionDisabled) {
this.serverless.cli.log(
"Skipping removing log subscription. Explicitly disabled"
Expand Down Expand Up @@ -311,6 +294,11 @@ export default class NewRelicLambdaLayerPlugin {
) {
return false;
}

this.serverless.cli.log(
`Skipping plugin serverless-newrelic-lambda-layers for stage ${this.stage}`
);

return true;
}

Expand Down
6 changes: 3 additions & 3 deletions tests/fixtures/debug-log-level.output.service.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"events": [{ "schedule": "rate(5 minutes)" }],
"handler": "newrelic-wrapper-helper.handler",
"layers": [
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS810:17"
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS810:18"
],
"package": {
"exclude": ["./**", "!newrelic-wrapper-helper.js"],
Expand All @@ -51,7 +51,7 @@
"events": [{ "schedule": "rate(5 minutes)" }],
"handler": "newrelic-lambda-wrapper.handler",
"layers": [
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS10X:19"
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS10X:20"
],
"package": {
"exclude": ["./**", "!newrelic-wrapper-helper.js"],
Expand All @@ -73,7 +73,7 @@
"events": [{ "schedule": "rate(5 minutes)" }],
"handler": "newrelic-lambda-wrapper.handler",
"layers": [
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS12X:17"
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS12X:18"
],
"package": {
"exclude": ["./**", "!newrelic-wrapper-helper.js"],
Expand Down
6 changes: 3 additions & 3 deletions tests/fixtures/debug.output.service.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"events": [{ "schedule": "rate(5 minutes)" }],
"handler": "newrelic-wrapper-helper.handler",
"layers": [
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS810:17"
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS810:18"
],
"package": {
"exclude": ["./**", "!newrelic-wrapper-helper.js"],
Expand All @@ -50,7 +50,7 @@
"events": [{ "schedule": "rate(5 minutes)" }],
"handler": "newrelic-lambda-wrapper.handler",
"layers": [
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS10X:19"
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS10X:20"
],
"package": {
"exclude": ["./**", "!newrelic-wrapper-helper.js"],
Expand All @@ -72,7 +72,7 @@
"events": [{ "schedule": "rate(5 minutes)" }],
"handler": "newrelic-lambda-wrapper.handler",
"layers": [
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS12X:17"
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS12X:18"
],
"package": {
"exclude": ["./**", "!newrelic-wrapper-helper.js"],
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/include.output.service.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"events": [{ "schedule": "rate(5 minutes)" }],
"handler": "newrelic-wrapper-helper.handler",
"layers": [
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS810:17"
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS810:18"
],
"package": {
"exclude": ["./**", "!newrelic-wrapper-helper.js"],
Expand Down
6 changes: 3 additions & 3 deletions tests/fixtures/log-disabled.output.service.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"events": [{ "schedule": "rate(5 minutes)" }],
"handler": "newrelic-wrapper-helper.handler",
"layers": [
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS810:17"
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS810:18"
],
"package": {
"exclude": ["./**", "!newrelic-wrapper-helper.js"],
Expand All @@ -47,7 +47,7 @@
"events": [{ "schedule": "rate(5 minutes)" }],
"handler": "newrelic-lambda-wrapper.handler",
"layers": [
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS10X:19"
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS10X:20"
],
"package": {
"exclude": ["./**", "!newrelic-wrapper-helper.js"],
Expand All @@ -66,7 +66,7 @@
"events": [{ "schedule": "rate(5 minutes)" }],
"handler": "newrelic-lambda-wrapper.handler",
"layers": [
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS12X:17"
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS12X:18"
],
"package": {
"exclude": ["./**", "!newrelic-wrapper-helper.js"],
Expand Down
6 changes: 3 additions & 3 deletions tests/fixtures/log-level.output.service.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"events": [{ "schedule": "rate(5 minutes)" }],
"handler": "newrelic-wrapper-helper.handler",
"layers": [
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS810:17"
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS810:18"
],
"package": {
"exclude": ["./**", "!newrelic-wrapper-helper.js"],
Expand All @@ -50,7 +50,7 @@
"events": [{ "schedule": "rate(5 minutes)" }],
"handler": "newrelic-lambda-wrapper.handler",
"layers": [
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS10X:19"
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS10X:20"
],
"package": {
"exclude": ["./**", "!newrelic-wrapper-helper.js"],
Expand All @@ -72,7 +72,7 @@
"events": [{ "schedule": "rate(5 minutes)" }],
"handler": "newrelic-lambda-wrapper.handler",
"layers": [
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS12X:17"
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS12X:18"
],
"package": {
"exclude": ["./**", "!newrelic-wrapper-helper.js"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"events": [{ "schedule": "rate(5 minutes)" }],
"handler": "newrelic-wrapper-helper.handler",
"layers": [
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS810:17"
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS810:18"
],
"package": {
"exclude": ["./**", "!newrelic-wrapper-helper.js"],
Expand All @@ -53,7 +53,7 @@
"events": [{ "schedule": "rate(5 minutes)" }],
"handler": "newrelic-lambda-wrapper.handler",
"layers": [
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS10X:19"
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS10X:20"
],
"package": {
"exclude": ["./**", "!newrelic-wrapper-helper.js"],
Expand All @@ -75,7 +75,7 @@
"events": [{ "schedule": "rate(5 minutes)" }],
"handler": "newrelic-lambda-wrapper.handler",
"layers": [
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS12X:17"
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS12X:18"
],
"package": {
"exclude": ["./**", "!newrelic-wrapper-helper.js"],
Expand Down
6 changes: 3 additions & 3 deletions tests/fixtures/provider-environment.output.service.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"events": [{ "schedule": "rate(5 minutes)" }],
"handler": "newrelic-wrapper-helper.handler",
"layers": [
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS810:17"
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS810:18"
],
"package": {
"exclude": ["./**", "!newrelic-wrapper-helper.js"],
Expand All @@ -52,7 +52,7 @@
"events": [{ "schedule": "rate(5 minutes)" }],
"handler": "newrelic-lambda-wrapper.handler",
"layers": [
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS10X:19"
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS10X:20"
],
"package": {
"exclude": ["./**", "!newrelic-wrapper-helper.js"],
Expand All @@ -74,7 +74,7 @@
"events": [{ "schedule": "rate(5 minutes)" }],
"handler": "newrelic-lambda-wrapper.handler",
"layers": [
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS12X:17"
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS12X:18"
],
"package": {
"exclude": ["./**", "!newrelic-wrapper-helper.js"],
Expand Down
6 changes: 3 additions & 3 deletions tests/fixtures/stage-included.output.service.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"events": [{ "schedule": "rate(5 minutes)" }],
"handler": "newrelic-wrapper-helper.handler",
"layers": [
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS810:17"
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS810:18"
],
"package": {
"exclude": ["./**", "!newrelic-wrapper-helper.js"],
Expand All @@ -46,7 +46,7 @@
"events": [{ "schedule": "rate(5 minutes)" }],
"handler": "newrelic-lambda-wrapper.handler",
"layers": [
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS10X:19"
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS10X:20"
],
"package": {
"exclude": ["./**", "!newrelic-wrapper-helper.js"],
Expand All @@ -65,7 +65,7 @@
"events": [{ "schedule": "rate(5 minutes)" }],
"handler": "newrelic-lambda-wrapper.handler",
"layers": [
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS12X:17"
"arn:aws:lambda:us-east-1:451483290750:layer:NewRelicNodeJS12X:18"
],
"package": {
"exclude": ["./**", "!newrelic-wrapper-helper.js"],
Expand Down
5 changes: 4 additions & 1 deletion tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ describe("NewRelicLambdaLayerPlugin", () => {
serverless.setProvider("aws", new AwsProvider(serverless, options));
const plugin = new NewRelicLambdaLayerPlugin(serverless, options);

await plugin.run();
try {
await plugin.hooks['before:deploy:function:packageFunction']();
} catch (err) {}


expect(
omit(
Expand Down

0 comments on commit 6729eb0

Please sign in to comment.