Skip to content

Commit 51925b7

Browse files
authored
Merge pull request #580 from Theodo-UK/serverless-config-fixes
added tests and functionality for default serverless config stackname format
2 parents b38f2b8 + 80659e4 commit 51925b7

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

__test__/util/serverlessConfigParser.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ functions:
3232
authorizerId:
3333
Ref: ApiGatewayAuthorizer
3434
`;
35+
const TEST_YAML_DEFAULT_STACKNAME = `
36+
service: test-\${opt:testArg1}
37+
38+
provider:
39+
name: aws
40+
runtime: nodejs10.x
41+
region: eu-west-1
42+
stage: \${opt:stage, 'dev'}
43+
`;
3544

3645
// eslint-disable-next-line no-template-curly-in-string
3746
const STACK_NAME_OPT = "test-${opt:testArg1}";
@@ -145,4 +154,11 @@ describe("Serverless Config Parsing", () => {
145154
expect(SLS.getStackName("dev")).toBe(null);
146155
expect(SLS.getRegion()).toBe(null);
147156
});
157+
158+
it(`should parse the stack name from a serverless config file when a default
159+
value for 'service' (not service.name) is used`, () => {
160+
const SLS = setupConfigParser(TEST_YAML_DEFAULT_STACKNAME);
161+
const stage = "test";
162+
expect(SLS.getStackName(stage)).toBe("test-backend-test");
163+
});
148164
});

src/services/severlessConfigParser/serverlessConfigParser.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,25 @@ class ServerlessConfigParser {
2222

2323
if (!this.config) return;
2424

25+
if (this.isNameNotInService()) {
26+
const name = this.config.service;
27+
this.config.service = { name };
28+
}
29+
2530
this.config.service.name = replaceStacknameOpt(
2631
this.config.service.name,
2732
options
2833
);
2934
}
3035

36+
isNameNotInService() {
37+
return (
38+
(typeof this.config.service === "object" &&
39+
!("name" in this.config.service)) ||
40+
!(typeof this.config.service === "object")
41+
);
42+
}
43+
3144
getFunctionConfig(functionName) {
3245
if (this.config && this.config.functions) {
3346
return this.config.functions[functionName];

0 commit comments

Comments
 (0)