Skip to content

Commit 95b8bce

Browse files
committed
refactor: evaluate tags in tagParser rather than iacStack
Signed-off-by: seven <[email protected]>
1 parent 1753652 commit 95b8bce

File tree

7 files changed

+30
-21
lines changed

7 files changed

+30
-21
lines changed

src/commands/deploy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export const deploy = async (
88
) => {
99
const context = constructActionContext({ ...options, stackName });
1010
logger.info('Validating yaml...');
11-
const iac = parseYaml(context.iacLocation);
11+
const iac = parseYaml(context);
1212
logger.info('Yaml is valid! 🎉');
1313

1414
logger.info('Deploying stack...');

src/commands/template.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export const template = (
99
options: { format: TemplateFormat; location: string; stage: string | undefined },
1010
) => {
1111
const context = constructActionContext({ ...options, stackName });
12-
const iac = parseYaml(context.iacLocation);
12+
const iac = parseYaml(context);
1313
const { template } = generateStackTemplate(stackName, iac, context);
1414

1515
const output =

src/commands/validate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { parseYaml } from '../parser';
33

44
export const validate = (location: string | undefined, stage: string | undefined) => {
55
const context = constructActionContext({ location, stage });
6-
parseYaml(context.iacLocation);
6+
parseYaml(context);
77
logger.info('Yaml is valid! 🎉');
88
logger.debug('Yaml is valid! debug🎉');
99
};

src/parser/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { existsSync, readFileSync } from 'node:fs';
2-
import { ServerlessIac, ServerlessIacRaw } from '../types';
2+
import { ActionContext, ServerlessIac, ServerlessIacRaw } from '../types';
33
import { parseFunction } from './functionParser';
44
import { parseEvent } from './eventParser';
55
import { parseDatabase } from './databaseParser';
@@ -13,7 +13,7 @@ const validateExistence = (path: string) => {
1313
}
1414
};
1515

16-
const transformYaml = (iacJson: ServerlessIacRaw): ServerlessIac => {
16+
const transformYaml = (iacJson: ServerlessIacRaw, context: ActionContext): ServerlessIac => {
1717
return {
1818
service: iacJson.service,
1919
version: iacJson.version,
@@ -23,17 +23,17 @@ const transformYaml = (iacJson: ServerlessIacRaw): ServerlessIac => {
2323
functions: parseFunction(iacJson.functions),
2424
events: parseEvent(iacJson.events),
2525
databases: parseDatabase(iacJson.databases),
26-
tags: parseTag(iacJson.tags),
26+
tags: parseTag(iacJson.tags, context),
2727
};
2828
};
2929

30-
export const parseYaml = (yamlPath: string): ServerlessIac => {
31-
validateExistence(yamlPath);
30+
export const parseYaml = (context: ActionContext): ServerlessIac => {
31+
validateExistence(context.iacLocation);
3232

33-
const yamlContent = readFileSync(yamlPath, 'utf8');
33+
const yamlContent = readFileSync(context.iacLocation, 'utf8');
3434
const iacJson = parse(yamlContent) as ServerlessIacRaw;
3535

3636
validateYaml(iacJson);
3737

38-
return transformYaml(iacJson);
38+
return transformYaml(iacJson, context);
3939
};

src/parser/tagParser.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
import { TagDomain, Tags } from '../types';
2-
import { isEmpty } from 'lodash';
1+
import { ActionContext, TagDomain, Tags } from '../types';
2+
import { replaceReference } from '../common';
33

4-
export const parseTag = (tags?: Tags): Array<TagDomain> => {
5-
const tagList = [{ key: 'iac-provider', value: 'ServerlessInsight' }];
6-
if (isEmpty(tags)) return tagList;
7-
8-
return [...tagList, ...Object.entries(tags).map(([key, value]) => ({ key, value }))];
4+
export const parseTag = (tags: Tags | undefined, context: ActionContext): Array<TagDomain> => {
5+
return [
6+
{ key: 'iac-provider', value: 'ServerlessInsight' },
7+
...Object.entries(tags ?? {}).map(([key, value]) => ({
8+
key: replaceReference(key, context),
9+
value: replaceReference(value, context),
10+
})),
11+
];
912
};

src/stack/iacStack.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class IacStack extends ros.Stack {
3030
super(scope, replaceReference(iac.service, context), {
3131
stackName: context.stackName,
3232
tags: iac.tags?.reduce((acc: { [key: string]: string }, tag) => {
33-
acc[tag.key] = replaceReference(tag.value, context);
33+
acc[tag.key] = tag.value;
3434
return acc;
3535
}, {}),
3636
});

tests/parser/parse.test.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@ import { parseYaml } from '../../src/parser';
33

44
describe('unit test for parse', () => {
55
describe('domain - databases', () => {
6-
const yamlPath = path.resolve(__dirname, '../fixtures/serverless-insight-es.yml');
7-
6+
const defaultContext = {
7+
iacLocation: path.resolve(__dirname, '../fixtures/serverless-insight-es.yml'),
8+
accessKeyId: 'xxx',
9+
accessKeySecret: 'xxx',
10+
region: 'cn-chengdu',
11+
stackName: 'insight-es-poc-test',
12+
stage: 'test',
13+
};
814
it('should pass databases from yaml to domain instance when the yaml is valid', () => {
9-
const databaseDomain = parseYaml(yamlPath);
15+
const databaseDomain = parseYaml(defaultContext);
1016
expect(databaseDomain).toEqual({
1117
service: 'insight-es-poc',
1218
version: '0.0.1',

0 commit comments

Comments
 (0)