Skip to content

Commit e33b209

Browse files
committed
feat: add deploy command
Signed-off-by: seven <[email protected]>
1 parent 1a46da1 commit e33b209

File tree

11 files changed

+110
-18
lines changed

11 files changed

+110
-18
lines changed

src/commands/deploy.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { deployStack } from '../stack';
2+
import { printer } from '../common';
3+
import { parseYaml } from '../iac';
4+
5+
export const deploy = (location?: string) => {
6+
printer.info('Validating yaml...');
7+
const iac = parseYaml(location);
8+
printer.success('Yaml is valid! 🎉');
9+
10+
printer.info('Deploying stack...');
11+
deployStack(iac);
12+
printer.success('Stack deployed! 🎉');
13+
};

src/commands/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Command } from 'commander';
44
import { lang } from '../lang';
55
import { logger, getVersion } from '../common';
66
import { validate } from './validate';
7+
import { deploy } from './deploy';
78

89
const program = new Command();
910

@@ -29,4 +30,12 @@ program
2930
validate(location);
3031
});
3132

33+
program
34+
.command('deploy [location]')
35+
.description('deploy serverless Iac yaml')
36+
.action((location) => {
37+
logger.debug('log command info');
38+
deploy(location);
39+
});
40+
3241
program.parse();

src/commands/validate.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
import { parseYaml } from '../iac';
2-
import path from 'node:path';
32
import { printer } from '../common';
43

54
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);
5+
parseYaml(location);
126
printer.success('Yaml is valid! 🎉');
137
};

src/common/printer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import chalk from 'chalk';
22

33
export const printer = {
44
success: (message: string) =>
5-
console.log(`${chalk.bgYellow(chalk.black('ServerlessInsight'))}: ${chalk.green(message)}`),
5+
console.log(`${chalk.blue('ServerlessInsight')}: ${chalk.green(message)}`),
6+
info: (message: string) => console.log(`${chalk.blue('ServerlessInsight')}: ${message}`),
67
error: (message: string) =>
78
console.log(`${chalk.bgRed(chalk.black('ServerlessInsight'))}: ${chalk.red(message)}`),
89
};

src/iac/iacSchema.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ class IacSchemaErrors extends Error {
120120
type: error.keyword,
121121
}));
122122
}
123+
124+
public get errors() {
125+
return this.schemaErrors;
126+
}
123127
}
124128

125129
export const validateYaml = (iacJson: RawServerlessIac) => {

src/iac/parse.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { parse } from 'yaml';
22
import { existsSync, readFileSync } from 'node:fs';
33
import { IacFunction, RawServerlessIac, ServerlessIac, Event } from '../types';
44
import { validateYaml } from './iacSchema';
5+
import path from 'node:path';
56

67
const mapToArr = (obj: Record<string, Record<string, unknown> | string>) => {
78
return Object.entries(obj).map(([key, value]) =>
@@ -28,10 +29,15 @@ const transformYaml = (iacJson: RawServerlessIac): ServerlessIac => {
2829
};
2930
};
3031

31-
export const parseYaml = (path: string): ServerlessIac => {
32-
validateExistence(path);
32+
export const parseYaml = (location?: string): ServerlessIac => {
33+
const projectRoot = path.resolve(process.cwd());
34+
const yamlPath = location
35+
? path.resolve(projectRoot, location)
36+
: path.resolve(projectRoot, 'serverless-insight.yml');
3337

34-
const yamlContent = readFileSync(path, 'utf8');
38+
validateExistence(yamlPath);
39+
40+
const yamlContent = readFileSync(yamlPath, 'utf8');
3541
const iacJson = parse(yamlContent) as RawServerlessIac;
3642

3743
validateYaml(iacJson);

src/iac/resource.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// import * as ros from '@alicloud/ros-cdk-core';
2+
// import * as fc from '@alicloud/ros-cdk-fc';
3+
//
4+
// export class IacStack extends ros.Stack {
5+
// constructor(scope: ros.Construct, id: string, props?: ros.StackProps) {
6+
// super(scope, id, props);
7+
// new ros.RosInfo(this, ros.RosInfo.description, 'This is the simple ros cdk app example.');
8+
//
9+
// const functionCompute = new fc.RosFunction(
10+
// this,
11+
// 'MyFunction',
12+
// {
13+
// functionName: 'my-function',
14+
// serviceName: service.attrServiceName,
15+
// handler: 'index.handler',
16+
// runtime: 'nodejs14',
17+
// code: {
18+
// zipFile:
19+
// 'exports.handler = function(event, context, callback) { callback(null, "Hello World"); }',
20+
// },
21+
// },
22+
// false,
23+
// );
24+
// console.log(functionCompute);
25+
// }
26+
// }
27+
//
28+
// export const defineResource = (name: string, type: string, properties: Record<string, any>) => {};

src/stack/deploy.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import * as ros from '@alicloud/ros-cdk-core';
2+
import * as fc from '@alicloud/ros-cdk-fc';
3+
import { ServerlessIac } from '../types';
4+
5+
export class IacStack extends ros.Stack {
6+
constructor(scope: ros.Construct, iac: ServerlessIac, props?: ros.StackProps) {
7+
super(scope, iac.service, props);
8+
new ros.RosInfo(this, ros.RosInfo.description, 'This is the simple ros cdk app example.');
9+
iac.functions.map(
10+
(fnc) =>
11+
new fc.RosFunction(
12+
this,
13+
fnc.name,
14+
{
15+
functionName: fnc.name,
16+
serviceName: `${fnc.name}-service`,
17+
handler: fnc.handler,
18+
runtime: fnc.runtime,
19+
memorySize: fnc.memory,
20+
timeout: fnc.timeout,
21+
environmentVariables: fnc.environment,
22+
code: {
23+
zipFile:
24+
'exports.handler = function(event, context, callback) { callback(null, "Hello World"); }',
25+
},
26+
},
27+
false,
28+
),
29+
);
30+
}
31+
}
32+
33+
export const deployStack = (iac: ServerlessIac) => {
34+
console.log(`Deploying stack... ${JSON.stringify(iac)}`);
35+
const app = new ros.App();
36+
new IacStack(app, iac);
37+
app.synth();
38+
};

src/stack/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { deployStack } from './deploy';

src/types.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,16 @@ type Stages = {
1010
[key: string]: Stage;
1111
};
1212

13-
type FunctionEnvironment = {
14-
NODE_ENV: string;
15-
};
16-
1713
export type IacFunction = {
1814
name: string;
1915
runtime: string;
2016
handler: string;
21-
code: string;
17+
zip: string;
2218
memory: number;
2319
timeout: number;
24-
environment: FunctionEnvironment;
20+
environment: {
21+
[key: string]: string;
22+
};
2523
};
2624

2725
type Functions = {

0 commit comments

Comments
 (0)