From 5777572f7f389d12b4c1b2585698621d2c9bbd31 Mon Sep 17 00:00:00 2001 From: Felipe Hertzer Date: Tue, 1 Feb 2022 03:30:34 +1100 Subject: [PATCH] Support for Serverless Framework v3 (#311) --- index.js | 59 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 25 deletions(-) diff --git a/index.js b/index.js index 347ec2ef..f339c121 100644 --- a/index.js +++ b/index.js @@ -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}) { @@ -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 { @@ -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); + } } } }