Skip to content

Commit

Permalink
feat: support --baseline-template, submit diffs to component
Browse files Browse the repository at this point in the history
Signed-off-by: zxypro1 <[email protected]>
  • Loading branch information
zxypro1 committed Aug 8, 2024
1 parent 1720c2d commit d7ccf3d
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/component-interface/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@serverless-devs/component-interface",
"version": "0.0.5",
"version": "0.0.6",
"description": "request for serverless-devs",
"main": "lib/index.js",
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions packages/component-interface/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ export interface IInputs {
cwd: string;
outputs?: Record<string, any>;
output?: Record<string, any>; // 当前步骤输出
diffs?: Record<string, any>[]; // 当 --baseline-template 参数存在时,和基线yaml做diff
}
8 changes: 5 additions & 3 deletions packages/engine/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@serverless-devs/engine",
"version": "0.1.4-beta.15",
"version": "0.1.4-beta.16",
"description": "a engine lib for serverless-devs",
"main": "lib/index.js",
"scripts": {
Expand All @@ -23,21 +23,23 @@
"dependencies": {
"@serverless-cd/debug": "^4.3.4",
"@serverless-devs/credential": "workspace:^",
"@serverless-devs/load-application": "workspace:^",
"@serverless-devs/load-component": "workspace:^",
"@serverless-devs/logger": "workspace:^",
"@serverless-devs/parse-spec": "workspace:^",
"@serverless-devs/utils": "workspace:^",
"@serverless-devs/load-application": "workspace:^",
"@serverless-devs/secret": "workspace:^",
"@serverless-devs/utils": "workspace:^",
"ajv": "^8.12.0",
"chalk": "4.x",
"deep-diff": "^1.0.2",
"flatted": "^3.2.7",
"fs-extra": "^11.1.0",
"lodash": "^4.17.21",
"string-argv": "^0.3.2",
"xstate": "^4.37.2"
},
"devDependencies": {
"@types/deep-diff": "^1.0.5",
"@types/fs-extra": "^11.0.1",
"@types/lodash": "^4.14.195"
}
Expand Down
18 changes: 16 additions & 2 deletions packages/engine/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createMachine, interpret } from 'xstate';
import { isEmpty, get, each, map, isFunction, has, uniqueId, filter, omit, includes, set, isNil, isUndefined, keys, size, cloneDeep, find } from 'lodash';
import { IStepOptions, IRecord, IStatus, IEngineOptions, IContext, IEngineError, STEP_STATUS } from './types';
import { getProcessTime, getCredential, stringify, getAllowFailure } from './utils';
import { IStepOptions, IRecord, IStatus, IEngineOptions, IContext, IEngineError, STEP_STATUS, IDiff } from './types';
import { getProcessTime, getCredential, stringify, getAllowFailure, getDiffs } from './utils';
import ParseSpec, { getInputs, ISpec, IHookType, IStep as IParseStep, IActionLevel } from '@serverless-devs/parse-spec';
import path from 'path';
import chalk from 'chalk';
Expand Down Expand Up @@ -34,13 +34,16 @@ class Engine {
} as IContext;
private record = { status: STEP_STATUS.PENDING, editStatusAble: true } as IRecord;
private spec = {} as ISpec;
private baselineSpec = {} as ISpec;
private glog!: Logger;
private logger!: ILoggerInstance;
private parseSpecInstance!: ParseSpec;
private parseSpecInstanceBaseline!: ParseSpec;
private globalActionInstance!: Actions; // 全局的 action
private actionInstance!: Actions; // 项目的 action
private info: Record<string, any> = {}; // 存储全局变量
private secretManager!: SecretManager; // 敏感参数管理
private diffs: IDiff[] = []; // baseline diff

constructor(private options: IEngineOptions) {
debug('engine start');
Expand Down Expand Up @@ -72,6 +75,16 @@ class Engine {
logger: this.logger,
});
this.spec = await this.parseSpecInstance.start();
// 20240808: Add baselineTemplate, do diff when --baseline-template is set
if (this.spec.baselineTemplate) {
this.logger.debug(`baselineTemplate: ${this.spec.baselineTemplate}`);
this.parseSpecInstanceBaseline = new ParseSpec(get(this.spec, 'baselineTemplate'), {
argv: this.options.args,
logger: this.logger,
});
this.baselineSpec = await this.parseSpecInstanceBaseline.start();
this.diffs = getDiffs(get(this.spec, 'yaml.content'), get(this.baselineSpec, 'yaml.content')) || [];
}
// 初始化行参环境变量 > .env (parse-spec require .env)
each(this.options.env, (value, key) => {
process.env[key] = value;
Expand Down Expand Up @@ -655,6 +668,7 @@ class Engine {
const res = await new Credential({ logger: this.logger }).get(item.access);
return get(res, 'credential', {});
},
diffs: filter(this.diffs, (diff) => { return diff.path?.startsWith(`resources.${item.projectName}`) }),
};
this.recordContext(item, { props: newInputs });
debug(`get props: ${JSON.stringify(result)}`);
Expand Down
5 changes: 5 additions & 0 deletions packages/engine/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { IOptions as ILogConfig } from '@serverless-devs/logger/lib/type';
import Logger, { ILoggerInstance } from '@serverless-devs/logger';
import { AssertionError } from 'assert';
import { DevsError } from '@serverless-devs/utils';
import { Diff } from 'deep-diff';
export interface IEngineOptions {
args?: string[]; //默认 process.argv.slice(2)
template?: string;
Expand All @@ -15,6 +16,10 @@ export interface IEngineOptions {
serverlessDevsVersion?: string;
}

export type IDiff = Omit<Diff<Object, Object>, "path"> & {
path?: string | undefined;
}

export type IStepOptions = IStep & {
logger: ILoggerInstance; // logger实例
instance?: any; //组件实例
Expand Down
12 changes: 12 additions & 0 deletions packages/engine/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,25 @@ import Ajv from 'ajv';
import loadComponent from '@serverless-devs/load-component';
import loadApplication from '@serverless-devs/load-application';
import path from 'path';
import deepDiff from 'deep-diff';

export function getLogPath(filePath: string) {
return `step_${filePath}.log`;
}

export const randomId = () => Math.random().toString(16).slice(2);

export const getDiffs = (leftObject: Object, rightObject: Object) => {
const diffs = deepDiff.diff(leftObject, rightObject);
const formattedDiffs = diffs?.map((diff) => {
return {
...diff,
path: diff.path?.join('.'),
}
});
return formattedDiffs || [];
};

export function getProcessTime(time: number) {
return (Math.round((Date.now() - time) / 10) * 10) / 1000;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/parse-spec/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@serverless-devs/parse-spec",
"version": "0.0.28-beta.11",
"version": "0.0.28-beta.12",
"description": "a parse yaml spec lib for serverless-devs",
"main": "lib/index.js",
"scripts": {
Expand Down
2 changes: 2 additions & 0 deletions packages/parse-spec/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class ParseSpec {
constructor(filePath: string = '', private options: IOptions = {}) {
this.options.argv = this.options.argv || process.argv.slice(2);
this.options.logger = this.options.logger || console;
// ${config()} ${secret()} won't show real value in preview mode
this.options.isPreview = this.options.isPreview || false;
this.init(filePath);
debug(`yaml path: ${this.yaml.path}`);
Expand Down Expand Up @@ -259,6 +260,7 @@ class ParseSpec {
this.record.skipActions = get(argv, 'skip-actions');
this.record.debug = get(argv, 'debug');
this.record.env = get(argv, 'env');
this.record.baselineTemplate = get(argv, 'baseline-template');
if (includes(this.yaml.projectNames, _[0])) {
this.record.projectName = _[0];
this.record.command = _[1];
Expand Down
1 change: 1 addition & 0 deletions packages/parse-spec/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export interface IRecord {
help?: boolean;
debug?: boolean;
env?: string;
baselineTemplate?: string;
}

export enum IOutput {
Expand Down
14 changes: 14 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d7ccf3d

Please sign in to comment.