Skip to content

Commit

Permalink
refactor: improve & standlize the logger #5
Browse files Browse the repository at this point in the history
Signed-off-by: seven <[email protected]>
  • Loading branch information
Blankll committed Sep 23, 2024
1 parent a3f3d63 commit bb533b0
Show file tree
Hide file tree
Showing 12 changed files with 219 additions and 36 deletions.
199 changes: 190 additions & 9 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"commander": "^11.1.0",
"i18n": "^0.15.1",
"pino": "^8.17.2",
"pino-pretty": "^11.2.2",
"yaml": "^2.5.1"
},
"devDependencies": {
Expand Down
10 changes: 5 additions & 5 deletions src/commands/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { deployStack, parseYaml } from '../stack';
import { printer, constructActionContext } from '../common';
import { constructActionContext, logger } from '../common';

export const deploy = async (
stackName: string,
options: { location: string; parameters: { [key: string]: string } },
) => {
const context = constructActionContext(options);
printer.info('Validating yaml...');
logger.info('Validating yaml...');
const iac = parseYaml(context.iacLocation);
printer.success('Yaml is valid! 🎉');
logger.info('Yaml is valid! 🎉');

printer.info('Deploying stack...');
logger.info('Deploying stack...');
await deployStack(stackName, iac, context);

printer.success('Stack deployed! 🎉');
logger.info('Stack deployed! 🎉');
};
5 changes: 3 additions & 2 deletions src/commands/validate.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { printer, constructActionContext } from '../common';
import { constructActionContext, logger } from '../common';
import { parseYaml } from '../stack';

export const validate = (location?: string) => {
const context = constructActionContext({ location });
parseYaml(context.iacLocation);
printer.success('Yaml is valid! 🎉');
logger.info('Yaml is valid! 🎉');
logger.debug('Yaml is valid! debug🎉');
};
1 change: 0 additions & 1 deletion src/common/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './printer';
export * from './provider';
export * from './logger';
export * from './getVersion';
Expand Down
4 changes: 4 additions & 0 deletions src/common/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import pino from 'pino';

const logger = pino({
name: 'ServerlessInsight',
level: ['ServerlessInsight', '*'].includes(process.env.DEBUG || '') ? 'debug' : 'info',
transport: {
target: 'pino-pretty',
},
});

export { logger };
9 changes: 0 additions & 9 deletions src/common/printer.ts

This file was deleted.

8 changes: 4 additions & 4 deletions src/common/rosClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import ROS20190910, {
} from '@alicloud/ros20190910';
import { Config } from '@alicloud/openapi-client';
import { ActionContext } from '../types';
import { printer } from './printer';
import { logger } from './logger';

const client = new ROS20190910(
new Config({
Expand Down Expand Up @@ -98,15 +98,15 @@ export const rosStackDeploy = async (
if (stackInfo) {
const { Status: stackStatus } = stackInfo;
if (stackStatus?.indexOf('IN_PROGRESS') >= 0) {
printer.error(`fail to update stack, because stack status is ${stackStatus}`);
logger.error(`fail to update stack, because stack status is ${stackStatus}`);
throw new Error(`fail to update stack, because stack status is ${stackStatus}`);
}

printer.info(`Update stack: ${stackName} deploying... `);
logger.info(`Update stack: ${stackName} deploying... `);
return await updateStack(stackInfo.stackId as string, templateBody, context);
} else {
// create stack
printer.info(`Create stack: ${stackName} deploying... `);
logger.info(`Create stack: ${stackName} deploying... `);
return await createStack(stackName, templateBody, context);
}
};
6 changes: 3 additions & 3 deletions src/stack/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as ros from '@alicloud/ros-cdk-core';
import { ActionContext, ServerlessIac } from '../types';
import { printer, rosStackDeploy } from '../common';
import { logger, rosStackDeploy } from '../common';
import { IacStack } from './iacStack';

const generateStackTemplate = (stackName: string, iac: ServerlessIac, context: ActionContext) => {
Expand All @@ -19,7 +19,7 @@ export const deployStack = async (
context: ActionContext,
) => {
const { template } = generateStackTemplate(stackName, iac, context);
console.log('Generated ROS YAML:', JSON.stringify({ template }));

await rosStackDeploy(stackName, template, context);
printer.info(`Stack deployed! 🎉`);
logger.info(`Stack deployed! 🎉`);
};
6 changes: 5 additions & 1 deletion src/stack/iacSchema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { RawServerlessIac } from '../types';
import Ajv, { ErrorObject } from 'ajv';
import { logger } from '../common';

const ajv = new Ajv({ allowUnionTypes: true, strict: false, allErrors: true });

Expand Down Expand Up @@ -130,7 +131,10 @@ class IacSchemaErrors extends Error {
export const validateYaml = (iacJson: RawServerlessIac) => {
const validate = ajv.compile(schema);
const valid = validate(iacJson);
if (!valid) throw new IacSchemaErrors(validate.errors as Array<ErrorObject>);
if (!valid) {
logger.debug(`Invalid yaml: ${JSON.stringify(validate.errors)}`);
throw new IacSchemaErrors(validate.errors as Array<ErrorObject>);
}

return true;
};
2 changes: 2 additions & 0 deletions tests/fixtures/serverless-insignt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ vars:
stages:
dev:
region: ${vars.region}
prod:
region: cn-shanghai

service: insight-poc

Expand Down
4 changes: 2 additions & 2 deletions tests/validate.test.ts → tests/stack/validate.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { readFileSync } from 'node:fs';
import * as path from 'node:path';
import { parse } from 'yaml';
import { RawServerlessIac } from '../src/types';
import { validateYaml } from '../src/stack';
import { validateYaml } from '../../src/stack';
import { RawServerlessIac } from '../../src/types';

const jsonIac = parse(
readFileSync(path.resolve(__dirname, './fixtures/serverless-insignt.yml'), 'utf8'),
Expand Down

0 comments on commit bb533b0

Please sign in to comment.