Skip to content

Commit

Permalink
Support for Serverless Framework v3 (#311)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipehertzer authored Jan 31, 2022
1 parent bda4d99 commit 5777572
Showing 1 changed file with 34 additions and 25 deletions.
59 changes: 34 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@
*/

class ServerlessPlugin {
constructor(serverless, options) {
const fs = require("fs");
const path = require('path');
const filename = path.resolve(__dirname, 'layers.json');
const layers = JSON.parse(fs.readFileSync(filename));
constructor(serverless, options, utils) {
this.serverless = serverless;
this.options = options;
this.utils = utils;
this.provider = this.serverless.getProvider('aws');

this.fs = require('fs');
this.path = require('path');

const filename = this.path.resolve(__dirname, 'layers.json');
const layers = JSON.parse(this.fs.readFileSync(filename));

// Declare `${bref-extra:xxx}` variables
// See https://www.serverless.com/framework/docs/providers/aws/guide/plugins#custom-variable-types
// See https://www.serverless.com/framework/docs/guides/plugins/custom-variables
this.configurationVariablesSources = {
'bref-extra': {
async resolve({address, params, resolveConfigurationProperty, options}) {
Expand All @@ -26,10 +32,10 @@ class ServerlessPlugin {
const region = options.region || await resolveConfigurationProperty(['provider', 'region']);

if (! (address in layers)) {
throw new Error(`Unknown Bref extra layer named "${address}"`);
throw new this.serverless.classes.Error(`Unknown Bref extra layer named "${address}"`);
}
if (! (region in layers[address])) {
throw new Error(`There is no Bref extra layer named "${address}" in region "${region}"`);
throw new this.serverless.classes.Error(`There is no Bref extra layer named "${address}" in region "${region}"`);
}
const version = layers[address][region];
return {
Expand All @@ -39,25 +45,28 @@ class ServerlessPlugin {
}
};

// This is the legacy way of declaring `${bref-extra:xxx}` variables. This has been deprecated in 20210326.
// Override the variable resolver to declare our own variables
const delegate = serverless.variables
.getValueFromSource.bind(serverless.variables);
serverless.variables.getValueFromSource = (variableString) => {
if (variableString.startsWith('bref-extra:')) {
const region = serverless.getProvider('aws').getRegion();
const layerName = variableString.substr('bref-extra:'.length);
if (! (layerName in layers)) {
throw `Unknown Bref extra layer named "${layerName}"`;
}
if (! (region in layers[layerName])) {
throw `There is no Bref extra layer named "${layerName}" in region "${region}"`;
// If we are on Serverless Framework v2, set up the legacy variable resolver
if (!this.utils) {
// This is the legacy way of declaring `${bref-extra:xxx}` variables. This has been deprecated in 20210326.
// Override the variable resolver to declare our own variables
const delegate = this.serverless.variables
.getValueFromSource.bind(this.serverless.variables);
this.serverless.variables.getValueFromSource = (variableString) => {
if (variableString.startsWith('bref-extra:')) {
const region = this.provider.getRegion();
const layerName = variableString.substr('bref-extra:'.length);
if (!(layerName in layers)) {
throw new serverless.classes.Error(`Unknown Bref extra layer named "${layerName}".`);
}
if (!(region in layers[layerName])) {
throw new serverless.classes.Error(`There is no Bref extra layer named "${layerName}" in region "${region}".`);
}
const version = layers[layerName][region];
return `arn:aws:lambda:${region}:403367587399:layer:${layerName}:${version}`;
}
const version = layers[layerName][region];
return `arn:aws:lambda:${region}:403367587399:layer:${layerName}:${version}`;
}

return delegate(variableString);
return delegate(variableString);
}
}
}
}
Expand Down

0 comments on commit 5777572

Please sign in to comment.