Skip to content

Commit bba9248

Browse files
authored
refactor: serverlessInsight provider format refactor (#23)
refactor: serverlessInsight provider format refactor refactor the provider format,to support specify the region: ```yaml provider: aliyun ``` to ```yaml provider name: alialiyun region: cn-hangzhou ``` Refs: #5 Signed-off-by: seven <zilisheng1996@gmail.com>
1 parent d4cf33e commit bba9248

18 files changed

+90
-64
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);
11+
const iac = parseYaml(context.iacLocation);
1212
logger.info('Yaml is valid! 🎉');
1313

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

src/commands/template.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import { TemplateFormat } from '../types';
22
import yaml from 'yaml';
33
import { generateStackTemplate } from '../stack/deploy';
4-
import { constructActionContext, logger } from '../common';
4+
import { constructActionContext, getIacLocation, logger } from '../common';
55
import { parseYaml } from '../parser';
66

77
export const template = (
88
stackName: string,
99
options: { format: TemplateFormat; location: string; stage: string | undefined },
1010
) => {
11-
const context = constructActionContext({ ...options, stackName });
12-
const iac = parseYaml(context);
11+
const iac = parseYaml(getIacLocation(options.location));
12+
13+
const context = constructActionContext({ ...options, stackName, provider: iac.provider.name });
1314

1415
const { template } = generateStackTemplate(stackName, iac, context);
1516
if (typeof template === 'string') {

src/commands/validate.ts

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

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

src/common/actionContext.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
import { ActionContext } from '../types';
22
import path from 'node:path';
3+
import { ProviderEnum } from './providerEnum';
4+
5+
export const getIacLocation = (location?: string): string => {
6+
const projectRoot = path.resolve(process.cwd());
7+
return location
8+
? path.resolve(projectRoot, location)
9+
: path.resolve(projectRoot, 'serverlessinsight.yml') ||
10+
path.resolve(projectRoot, 'serverlessInsight.yml') ||
11+
path.resolve(projectRoot, 'ServerlessInsight.yml') ||
12+
path.resolve(projectRoot, 'serverless-insight.yml');
13+
};
314

415
export const constructActionContext = (config?: {
516
region?: string;
17+
provider?: string;
618
account?: string;
719
accessKeyId?: string;
820
accessKeySecret?: string;
@@ -20,15 +32,8 @@ export const constructActionContext = (config?: {
2032
accessKeyId: config?.accessKeyId ?? (process.env.ALIYUN_ACCESS_KEY_ID as string),
2133
accessKeySecret: config?.accessKeySecret ?? (process.env.ALIYUN_ACCESS_KEY_SECRET as string),
2234
securityToken: config?.securityToken ?? process.env.ALIYUN_SECURITY_TOKEN,
23-
iacLocation: (() => {
24-
const projectRoot = path.resolve(process.cwd());
25-
return config?.location
26-
? path.resolve(projectRoot, config.location)
27-
: path.resolve(projectRoot, 'serverlessinsight.yml') ||
28-
path.resolve(projectRoot, 'serverlessInsight.yml') ||
29-
path.resolve(projectRoot, 'ServerlessInsight.yml') ||
30-
path.resolve(projectRoot, 'serverless-insight.yml');
31-
})(),
35+
iacLocation: getIacLocation(config?.location),
3236
parameters: Object.entries(config?.parameters ?? {}).map(([key, value]) => ({ key, value })),
37+
provider: config?.provider as ProviderEnum,
3338
};
3439
};

src/common/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export * from './provider';
1+
export * from './providerEnum';
22
export * from './logger';
33
export * from './getVersion';
44
export * from './rosClient';

src/common/provider.ts renamed to src/common/providerEnum.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export enum Provider {
1+
export enum ProviderEnum {
22
HUAWEI = 'huawei',
33
ALIYUN = 'aliyun',
44
// TENCENT = 'TENCENT',

src/parser/index.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { existsSync, readFileSync } from 'node:fs';
2-
import { ActionContext, ServerlessIac, ServerlessIacRaw } from '../types';
2+
import { ServerlessIac, ServerlessIacRaw } from '../types';
33
import { parseFunction } from './functionParser';
44
import { parseEvent } from './eventParser';
55
import { parseDatabase } from './databaseParser';
66
import { parseTag } from './tagParser';
77
import { parse } from 'yaml';
88
import { validateYaml } from '../validator';
9-
import { Provider } from '../common';
109

1110
const validateExistence = (path: string) => {
1211
if (!existsSync(path)) {
@@ -18,7 +17,7 @@ const transformYaml = (iacJson: ServerlessIacRaw): ServerlessIac => {
1817
return {
1918
service: iacJson.service,
2019
version: iacJson.version,
21-
provider: iacJson.provider as Provider,
20+
provider: iacJson.provider,
2221
vars: iacJson.vars,
2322
stages: iacJson.stages,
2423
functions: parseFunction(iacJson.functions),
@@ -28,10 +27,10 @@ const transformYaml = (iacJson: ServerlessIacRaw): ServerlessIac => {
2827
};
2928
};
3029

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

34-
const yamlContent = readFileSync(context.iacLocation, 'utf8');
33+
const yamlContent = readFileSync(iacLocation, 'utf8');
3534
const iacJson = parse(yamlContent) as ServerlessIacRaw;
3635

3736
validateYaml(iacJson);

src/stack/deploy.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as ros from '@alicloud/ros-cdk-core';
22

33
import { ActionContext, ServerlessIac } from '../types';
4-
import { logger, Provider, rosStackDeploy } from '../common';
4+
import { logger, ProviderEnum, rosStackDeploy } from '../common';
55
import { RosStack } from './rosStack';
66
import { RfsStack } from './rfsStack';
77

@@ -48,9 +48,9 @@ export const generateStackTemplate = (
4848
iac: ServerlessIac,
4949
context: ActionContext,
5050
): { template: unknown } => {
51-
if (iac.provider === Provider.ALIYUN) {
51+
if (iac.provider.name === ProviderEnum.ALIYUN) {
5252
return generateRosStackTemplate(stackName, iac, context);
53-
} else if (iac.provider === Provider.HUAWEI) {
53+
} else if (iac.provider.name === ProviderEnum.HUAWEI) {
5454
return generateRfsStackTemplate(stackName, iac, context);
5555
}
5656
return { template: '' };

src/types/domains/context.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import { ProviderEnum } from '../../common';
2+
13
export type ActionContext = {
2-
stage: string;
3-
stackName: string;
44
region: string;
5+
provider: ProviderEnum;
6+
stackName: string;
7+
stage: string;
58
accessKeyId: string;
69
accessKeySecret: string;
710
securityToken?: string;

src/types/domains/provider.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { ProviderEnum } from '../../common';
2+
3+
export type Provider = {
4+
name: ProviderEnum;
5+
region: string;
6+
};

0 commit comments

Comments
 (0)