Skip to content

Commit

Permalink
feat: new command template (#13)
Browse files Browse the repository at this point in the history
feat: new command template

- add new command template to print stack ros template with json/yaml
- unit test

Refs: #5

---------

Signed-off-by: seven <zilisheng1996@gmail.com>
  • Loading branch information
Blankll authored Dec 3, 2024
1 parent af91ea7 commit fa937ec
Show file tree
Hide file tree
Showing 8 changed files with 360 additions and 4 deletions.
12 changes: 11 additions & 1 deletion src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { lang } from '../lang';
import { logger, getVersion } from '../common';
import { validate } from './validate';
import { deploy } from './deploy';
import { template } from './template';

const program = new Command();

Expand Down Expand Up @@ -48,8 +49,17 @@ program
{},
)
.action(async (stackName, { file, parameter, stage }) => {
logger.debug('log command info');
await deploy(stackName, { location: file, parameters: parameter, stage });
});

program
.command('template <stackName>')
.description('print ROS template')
.option('-f, --file <path>', 'specify the yaml file')
.option('-s, --stage <stage>', 'specify the stage')
.option('-t, --format <type>', 'output content type (JSON or YAML)', 'JSON')
.action((stackName, { format, file, stage }) => {
template(stackName, { format, location: file, stage });
});

program.parse();
21 changes: 21 additions & 0 deletions src/commands/template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { TemplateFormat } from '../types';
import yaml from 'yaml';
import { generateStackTemplate } from '../stack/deploy';
import { constructActionContext, logger } from '../common';
import { parseYaml } from '../stack';

export const template = (
stackName: string,
options: { format: TemplateFormat; location: string; stage: string | undefined },
) => {
const context = constructActionContext({ ...options, stackName });
const iac = parseYaml(context.iacLocation);
const { template } = generateStackTemplate(stackName, iac, context);

const output =
options.format === TemplateFormat.JSON
? JSON.stringify(template, null, 2)
: yaml.stringify(template);

logger.info(`\n${output}`);
};
2 changes: 1 addition & 1 deletion src/common/actionContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const constructActionContext = (config?: {
iacLocation: (() => {
const projectRoot = path.resolve(process.cwd());
return config?.location
? path.resolve(projectRoot, config?.location)
? path.resolve(projectRoot, config.location)
: path.resolve(projectRoot, 'serverlessinsight.yml') ||
path.resolve(projectRoot, 'serverlessInsight.yml') ||
path.resolve(projectRoot, 'ServerlessInsight.yml') ||
Expand Down
6 changes: 5 additions & 1 deletion src/stack/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { ActionContext, ServerlessIac } from '../types';
import { logger, rosStackDeploy } from '../common';
import { IacStack } from './iacStack';

const generateStackTemplate = (stackName: string, iac: ServerlessIac, context: ActionContext) => {
export const generateStackTemplate = (
stackName: string,
iac: ServerlessIac,
context: ActionContext,
) => {
const app = new ros.App();
new IacStack(app, iac, context);

Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,8 @@ export type ActionContext = {
parameters?: Array<{ key: string; value: string }>;
tags?: Array<{ key: string; value: string }>;
};

export enum TemplateFormat {
YAML = 'YAML',
JSON = 'JSON',
}
35 changes: 35 additions & 0 deletions tests/commands/template.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { template } from '../../src/commands/template';
import { TemplateFormat } from '../../src/types';
import { jsonTemplate, yamlTemplate } from '../fixtures/templateFixture';

const mockedLogger = jest.fn();
jest.mock('../../src/common/logger', () => ({
logger: { info: (...args: unknown[]) => mockedLogger(...args), debug: jest.fn() },
}));
const stackName = 'printTemplateStack';
const location = 'tests/fixtures/serverless-insight.yml';

describe('Unit test for template command', () => {
beforeEach(() => {
mockedLogger.mockRestore();
});
it('should print the template in JSON format by default', () => {
const options = {
format: TemplateFormat.JSON,
location,
stage: undefined,
};

template(stackName, options);

expect(mockedLogger).toHaveBeenCalledWith(`\n${JSON.stringify(jsonTemplate, null, 2)}`);
});

it('should print the template in YAML format when specified', () => {
const options = { format: TemplateFormat.YAML, location, stage: undefined };

template(stackName, options);

expect(mockedLogger).toHaveBeenCalledWith(yamlTemplate);
});
});
2 changes: 1 addition & 1 deletion tests/fixtures/serverless-insight.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ functions:
name: insight-poc-fn
runtime: nodejs18
handler: ${vars.handler}
code: artifacts/artifact.zip
code: tests/fixtures/artifacts/artifact.zip
memory: 512
timeout: 10
environment:
Expand Down
Loading

0 comments on commit fa937ec

Please sign in to comment.