-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
54 lines (44 loc) · 1.36 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
import {readFileSync} from 'node:fs'
import path from 'node:path'
import cloudformationSchema from '@serverless/utils/cloudformation-schema.js'
import yaml from 'js-yaml'
const developmentStages = new Set([
'local',
'development',
'dev',
])
export default class EnvironmentStageConfigServerlessPlugin {
constructor(serverless, options) {
this.serverless = serverless
this.options = options
this.stage = options.stage ?? serverless.service.provider.stage
this.configurationVariablesSources = {
esc: {
resolve: this.resolveConfigVariable.bind(this),
},
}
if (!developmentStages.has(this.stage)) {
const stageConfigYaml = readFileSync(path.join(this.serverless.serviceDir, `serverless.env.${this.stage}.yml`))
this.stageVariables = yaml.load(stageConfigYaml, {
schema: cloudformationSchema,
})
}
}
async resolveConfigVariable({address}) {
if (this.stageVariables) {
if (address in this.stageVariables) {
return {
value: this.stageVariables[address],
}
}
this.serverless.cli.log(
`env-stage-config: WARNING: the ${address} variable is not defined in serverless.env.${this.stage}.yml, defaulting to \${env:${address}, null}.`,
null,
{color: 'orange'},
)
}
return {
value: `\${env:${address}, null}`,
}
}
}