Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/commands/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { deployStack, parseYaml } from '../stack';
import { deployStack } from '../stack';
import { constructActionContext, logger } from '../common';
import { parseYaml } from '../parser';

export const deploy = async (
stackName: string,
Expand Down
2 changes: 1 addition & 1 deletion src/commands/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { TemplateFormat } from '../types';
import yaml from 'yaml';
import { generateStackTemplate } from '../stack/deploy';
import { constructActionContext, logger } from '../common';
import { parseYaml } from '../stack';
import { parseYaml } from '../parser';

export const template = (
stackName: string,
Expand Down
2 changes: 1 addition & 1 deletion src/commands/validate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { constructActionContext, logger } from '../common';
import { parseYaml } from '../stack';
import { parseYaml } from '../parser';

export const validate = (location: string | undefined, stage: string | undefined) => {
const context = constructActionContext({ location, stage });
Expand Down
27 changes: 27 additions & 0 deletions src/parser/databaseParser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { DatabaseDomain, DatabaseEnum, DatabaseRaw } from '../types';
import { get, isEmpty } from 'lodash';

export const parseDatabase = (databases?: {
[key: string]: DatabaseRaw;
}): Array<DatabaseDomain> | undefined => {
if (isEmpty(databases)) {
return undefined;
}
return Object.entries(databases)?.map(([key, database]) => ({
key: key,
name: database.name,
type: database.type as DatabaseEnum,
version: database.version,
engineMode: database.engine_mode,
security: {
basicAuth: {
password: get(database, 'security.basic_auth.password'),
},
},
cu: database.cu,
storageSize: database.storage_size,
network: database.network && {
public: database.network?.public as boolean,
},
}));
};
13 changes: 13 additions & 0 deletions src/parser/eventParser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { EventDomain, EventRaw } from '../types';

export const parseEvent = (events: { [key: string]: EventRaw }): Array<EventDomain> | undefined => {
if (!events) {
return undefined;
}
return Object.entries(events).map(([key, event]) => ({
key,
name: event.name,
type: event.type,
triggers: event.triggers,
}));
};
20 changes: 20 additions & 0 deletions src/parser/functionParser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { FunctionDomain, FunctionRaw } from '../types';
import { isEmpty } from 'lodash';

export const parseFunction = (functions?: {
[key: string]: FunctionRaw;
}): Array<FunctionDomain> | undefined => {
if (isEmpty(functions)) {
return undefined;
}
return Object.entries(functions).map(([key, func]) => ({
key,
name: func.name,
runtime: func.runtime,
handler: func.handler,
memory: func.memory,
timeout: func.timeout,
environment: func.environment,
code: func.code,
}));
};
39 changes: 39 additions & 0 deletions src/parser/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { existsSync, readFileSync } from 'node:fs';
import { ServerlessIac, ServerlessIacRaw } from '../types';
import { parseFunction } from './functionParser';
import { parseEvent } from './eventParser';
import { parseDatabase } from './databaseParser';
import { parseTag } from './tagParser';
import { parse } from 'yaml';
import { validateYaml } from '../validator';

const validateExistence = (path: string) => {
if (!existsSync(path)) {
throw new Error(`File does not exist at path: ${path}`);

Check warning on line 12 in src/parser/index.ts

View check run for this annotation

Codecov / codecov/patch

src/parser/index.ts#L12

Added line #L12 was not covered by tests
}
};

const transformYaml = (iacJson: ServerlessIacRaw): ServerlessIac => {
return {
service: iacJson.service,
version: iacJson.version,
provider: iacJson.provider,
vars: iacJson.vars,
stages: iacJson.stages,
functions: parseFunction(iacJson.functions),
events: parseEvent(iacJson.events),
databases: parseDatabase(iacJson.databases),
tags: parseTag(iacJson.tags),
};
};

export const parseYaml = (yamlPath: string): ServerlessIac => {
validateExistence(yamlPath);

const yamlContent = readFileSync(yamlPath, 'utf8');
const iacJson = parse(yamlContent) as ServerlessIacRaw;

validateYaml(iacJson);

return transformYaml(iacJson);
};
9 changes: 9 additions & 0 deletions src/parser/tagParser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { TagDomain, Tags } from '../types';
import { isEmpty } from 'lodash';

export const parseTag = (tags?: Tags): Array<TagDomain> => {
const tagList = [{ key: 'iac-provider', value: 'ServerlessInsight' }];
if (isEmpty(tags)) return tagList;

return [...tagList, ...Object.entries(tags).map(([key, value]) => ({ key, value }))];
};
192 changes: 0 additions & 192 deletions src/stack/iacSchema.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/stack/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export { deployStack } from './deploy';
export * from './parse';
export * from './iacSchema';
Loading
Loading