Skip to content

Commit 1a46da1

Browse files
committed
feat: add validate command
Signed-off-by: seven <zilisheng1996@gmail.com>
1 parent 280ec5e commit 1a46da1

File tree

10 files changed

+68
-41
lines changed

10 files changed

+68
-41
lines changed

package-lock.json

Lines changed: 11 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"@alicloud/ros-cdk-core": "^1.2.0",
5353
"@alicloud/ros-cdk-fc": "^1.2.0",
5454
"ajv": "^8.17.1",
55+
"chalk": "^4.1.2",
5556
"commander": "^11.1.0",
5657
"i18n": "^0.15.1",
5758
"pino": "^8.17.2",
@@ -71,6 +72,6 @@
7172
"prettier": "^3.2.4",
7273
"ts-jest": "^29.1.1",
7374
"ts-node": "^10.9.2",
74-
"typescript": "^5.3.3"
75+
"typescript": "^5.6.2"
7576
}
7677
}

src/commands/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import { Command } from 'commander';
44
import { lang } from '../lang';
5-
import { logger } from '../common/logger';
6-
import { getVersion } from '../common/getVersion';
5+
import { logger, getVersion } from '../common';
6+
import { validate } from './validate';
77

88
const program = new Command();
99

@@ -22,11 +22,11 @@ program
2222
});
2323

2424
program
25-
.command('validate')
25+
.command('validate [location]')
2626
.description('validate serverless Iac yaml')
27-
.action((str) => {
27+
.action((location) => {
2828
logger.debug('log command info');
29-
console.log(`${str} ${lang.__('hello')}`);
29+
validate(location);
3030
});
3131

3232
program.parse();

src/commands/validate.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { parseYaml } from '../iac';
2+
import path from 'node:path';
3+
import { printer } from '../common';
4+
5+
export const validate = (location?: string) => {
6+
const projectRoot = path.resolve(process.cwd());
7+
const yamlPath = location
8+
? path.resolve(projectRoot, location)
9+
: path.resolve(projectRoot, 'serverless-insight.yml');
10+
11+
parseYaml(yamlPath);
12+
printer.success('Yaml is valid! 🎉');
13+
};

src/common/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export * from './printer';
2+
export * from './provider';
3+
export * from './logger';
4+
export * from './getVersion';

src/common/printer.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import chalk from 'chalk';
2+
3+
export const printer = {
4+
success: (message: string) =>
5+
console.log(`${chalk.bgYellow(chalk.black('ServerlessInsight'))}: ${chalk.green(message)}`),
6+
error: (message: string) =>
7+
console.log(`${chalk.bgRed(chalk.black('ServerlessInsight'))}: ${chalk.red(message)}`),
8+
};

src/iac/index.ts

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

src/iac/parse.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,40 @@
11
import { parse } from 'yaml';
2-
import { readFileSync } from 'node:fs';
3-
import { RawServerlessIac } from '../types';
2+
import { existsSync, readFileSync } from 'node:fs';
3+
import { IacFunction, RawServerlessIac, ServerlessIac, Event } from '../types';
4+
import { validateYaml } from './iacSchema';
45

56
const mapToArr = (obj: Record<string, Record<string, unknown> | string>) => {
67
return Object.entries(obj).map(([key, value]) =>
78
typeof value === 'string' ? { [key]: value } : { id: key, ...value },
89
);
910
};
1011

11-
const transformYaml = (iacJson: RawServerlessIac) => {
12+
const validateExistence = (path: string) => {
13+
if (!existsSync(path)) {
14+
throw new Error(`File does not exist at path: ${path}`);
15+
}
16+
};
17+
18+
const transformYaml = (iacJson: RawServerlessIac): ServerlessIac => {
1219
return {
1320
service: iacJson.service,
1421
version: iacJson.version,
1522
provider: iacJson.provider,
1623
vars: iacJson.vars,
1724
stages: iacJson.stages,
18-
functions: mapToArr(iacJson.functions),
19-
events: mapToArr(iacJson.events),
20-
tags: mapToArr(iacJson.tags),
25+
functions: mapToArr(iacJson.functions) as unknown as Array<IacFunction>,
26+
events: mapToArr(iacJson.events) as unknown as Array<Event>,
27+
tags: mapToArr(iacJson.tags) as unknown as Array<string>,
2128
};
2229
};
2330

24-
export const parseYaml = (path: string) => {
25-
// read yaml from path
31+
export const parseYaml = (path: string): ServerlessIac => {
32+
validateExistence(path);
33+
2634
const yamlContent = readFileSync(path, 'utf8');
2735
const iacJson = parse(yamlContent) as RawServerlessIac;
2836

37+
validateYaml(iacJson);
38+
2939
return transformYaml(iacJson);
3040
};

src/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ type FunctionEnvironment = {
1414
NODE_ENV: string;
1515
};
1616

17-
type IacFunction = {
18-
fc_name: string;
17+
export type IacFunction = {
18+
name: string;
1919
runtime: string;
2020
handler: string;
2121
code: string;
@@ -28,7 +28,7 @@ type Functions = {
2828
[key: string]: IacFunction;
2929
};
3030

31-
type Event = {
31+
export type Event = {
3232
type: string;
3333
source: string;
3434
function: string;

tests/validate.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { validateYaml } from '../src/iac/iacSchema';
1+
import { validateYaml } from '../src/iac';
22
import { readFileSync } from 'node:fs';
33
import * as path from 'node:path';
44
import { parse } from 'yaml';
@@ -90,7 +90,7 @@ describe('unit test for validate', () => {
9090
},
9191
},
9292
},
93-
};
93+
} as unknown as RawServerlessIac;
9494
expect(() => validateYaml(invalidYaml)).toThrow('Invalid yaml');
9595
});
9696

0 commit comments

Comments
 (0)