forked from brefphp/extra-php-extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (57 loc) · 2.96 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
'use strict';
/**
* This file declares a plugin for the Serverless framework.
*
* This lets us define variables and helpers to simplify creating PHP applications.
*/
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));
// Declare `${bref-extra:xxx}` variables
// See https://www.serverless.com/framework/docs/providers/aws/guide/plugins#custom-variable-types
this.configurationVariablesSources = {
'bref-extra': {
async resolve({address, params, resolveConfigurationProperty, options}) {
// `address` and `params` reflect values configured with a variable: ${bref-extra(param1, param2):address}
// `options` is CLI options
// `resolveConfigurationProperty` allows to access other configuration properties,
// and guarantees to return a fully resolved form (even if property is configured with variables)
const region = options.region || await resolveConfigurationProperty(['provider', 'region']);
if (! (address in layers)) {
throw new 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}"`);
}
const version = layers[address][region];
return {
value: `arn:aws:lambda:${region}:403367587399:layer:${address}:${version}`,
}
}
}
};
// 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}"`;
}
const version = layers[layerName][region];
return `arn:aws:lambda:${region}:403367587399:layer:${layerName}:${version}`;
}
return delegate(variableString);
}
}
}
module.exports = ServerlessPlugin;