Skip to content

Commit 22375a6

Browse files
authored
refactor: serverlessInsight repo resturcture (#15)
refactor: serverlessInsight repo restructure - [x] type definitions restructure - [x] schema validation restructure - [x] transformer restructure - [x] parse restructure Refs: --------- Signed-off-by: seven <zilisheng1996@gmail.com>
1 parent a8b4023 commit 22375a6

27 files changed

+480
-427
lines changed

src/commands/deploy.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { deployStack, parseYaml } from '../stack';
1+
import { deployStack } from '../stack';
22
import { constructActionContext, logger } from '../common';
3+
import { parseYaml } from '../parser';
34

45
export const deploy = async (
56
stackName: string,

src/commands/template.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { TemplateFormat } from '../types';
22
import yaml from 'yaml';
33
import { generateStackTemplate } from '../stack/deploy';
44
import { constructActionContext, logger } from '../common';
5-
import { parseYaml } from '../stack';
5+
import { parseYaml } from '../parser';
66

77
export const template = (
88
stackName: string,

src/commands/validate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { constructActionContext, logger } from '../common';
2-
import { parseYaml } from '../stack';
2+
import { parseYaml } from '../parser';
33

44
export const validate = (location: string | undefined, stage: string | undefined) => {
55
const context = constructActionContext({ location, stage });

src/parser/databaseParser.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { DatabaseDomain, DatabaseEnum, DatabaseRaw } from '../types';
2+
import { get, isEmpty } from 'lodash';
3+
4+
export const parseDatabase = (databases?: {
5+
[key: string]: DatabaseRaw;
6+
}): Array<DatabaseDomain> | undefined => {
7+
if (isEmpty(databases)) {
8+
return undefined;
9+
}
10+
return Object.entries(databases)?.map(([key, database]) => ({
11+
key: key,
12+
name: database.name,
13+
type: database.type as DatabaseEnum,
14+
version: database.version,
15+
engineMode: database.engine_mode,
16+
security: {
17+
basicAuth: {
18+
password: get(database, 'security.basic_auth.password'),
19+
},
20+
},
21+
cu: database.cu,
22+
storageSize: database.storage_size,
23+
network: database.network && {
24+
public: database.network?.public as boolean,
25+
},
26+
}));
27+
};

src/parser/eventParser.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { EventDomain, EventRaw } from '../types';
2+
3+
export const parseEvent = (events: { [key: string]: EventRaw }): Array<EventDomain> | undefined => {
4+
if (!events) {
5+
return undefined;
6+
}
7+
return Object.entries(events).map(([key, event]) => ({
8+
key,
9+
name: event.name,
10+
type: event.type,
11+
triggers: event.triggers,
12+
}));
13+
};

src/parser/functionParser.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { FunctionDomain, FunctionRaw } from '../types';
2+
import { isEmpty } from 'lodash';
3+
4+
export const parseFunction = (functions?: {
5+
[key: string]: FunctionRaw;
6+
}): Array<FunctionDomain> | undefined => {
7+
if (isEmpty(functions)) {
8+
return undefined;
9+
}
10+
return Object.entries(functions).map(([key, func]) => ({
11+
key,
12+
name: func.name,
13+
runtime: func.runtime,
14+
handler: func.handler,
15+
memory: func.memory,
16+
timeout: func.timeout,
17+
environment: func.environment,
18+
code: func.code,
19+
}));
20+
};

src/parser/index.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { existsSync, readFileSync } from 'node:fs';
2+
import { ServerlessIac, ServerlessIacRaw } from '../types';
3+
import { parseFunction } from './functionParser';
4+
import { parseEvent } from './eventParser';
5+
import { parseDatabase } from './databaseParser';
6+
import { parseTag } from './tagParser';
7+
import { parse } from 'yaml';
8+
import { validateYaml } from '../validator';
9+
10+
const validateExistence = (path: string) => {
11+
if (!existsSync(path)) {
12+
throw new Error(`File does not exist at path: ${path}`);
13+
}
14+
};
15+
16+
const transformYaml = (iacJson: ServerlessIacRaw): ServerlessIac => {
17+
return {
18+
service: iacJson.service,
19+
version: iacJson.version,
20+
provider: iacJson.provider,
21+
vars: iacJson.vars,
22+
stages: iacJson.stages,
23+
functions: parseFunction(iacJson.functions),
24+
events: parseEvent(iacJson.events),
25+
databases: parseDatabase(iacJson.databases),
26+
tags: parseTag(iacJson.tags),
27+
};
28+
};
29+
30+
export const parseYaml = (yamlPath: string): ServerlessIac => {
31+
validateExistence(yamlPath);
32+
33+
const yamlContent = readFileSync(yamlPath, 'utf8');
34+
const iacJson = parse(yamlContent) as ServerlessIacRaw;
35+
36+
validateYaml(iacJson);
37+
38+
return transformYaml(iacJson);
39+
};

src/parser/tagParser.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { TagDomain, Tags } from '../types';
2+
import { isEmpty } from 'lodash';
3+
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 }))];
9+
};

src/stack/iacSchema.ts

Lines changed: 0 additions & 192 deletions
This file was deleted.

src/stack/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
11
export { deployStack } from './deploy';
2-
export * from './parse';
3-
export * from './iacSchema';

0 commit comments

Comments
 (0)