Skip to content

Commit

Permalink
feat: preview support
Browse files Browse the repository at this point in the history
Signed-off-by: zxypro1 <[email protected]>
  • Loading branch information
zxypro1 committed Jul 29, 2024
1 parent 7f01775 commit 3ededc4
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 13 deletions.
2 changes: 1 addition & 1 deletion 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.12",
"version": "0.1.4-beta.13",
"description": "a engine lib for serverless-devs",
"main": "lib/index.js",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions packages/load-application/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@serverless-devs/load-application",
"version": "0.0.14-beta.8",
"version": "0.0.14-beta.9",
"description": "load application for serverless-devs",
"main": "lib/index.js",
"scripts": {
Expand All @@ -18,7 +18,7 @@
},
"dependencies": {
"@serverless-cd/debug": "^4.3.4",
"@serverless-devs/art-template": "^4.13.16-beta.17",
"@serverless-devs/art-template": "^4.13.16-beta.18",
"@serverless-devs/credential": "workspace:^",
"@serverless-devs/downloads": "workspace:^",
"@serverless-devs/utils": "workspace:^",
Expand Down
4 changes: 2 additions & 2 deletions 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.9",
"version": "0.0.28-beta.10",
"description": "a parse yaml spec lib for serverless-devs",
"main": "lib/index.js",
"scripts": {
Expand All @@ -22,7 +22,7 @@
},
"dependencies": {
"@serverless-cd/debug": "^4.3.4",
"@serverless-devs/art-template": "^4.13.16-beta.17",
"@serverless-devs/art-template": "^4.13.16-beta.18",
"@serverless-devs/credential": "workspace:^",
"@serverless-devs/utils": "workspace:^",
"chalk": "^4.1.2",
Expand Down
12 changes: 9 additions & 3 deletions packages/parse-spec/src/get-inputs.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
const compile = require('@serverless-devs/art-template/lib/devs-compile');
import { isEmpty } from 'lodash';

export const getInputs = (_inputs: Record<string, any> = {}, context: Record<string, any> = {}) => {
const inputs = typeof _inputs === 'string' ? compile(_inputs, context) : _inputs;
interface IOptions {
argv?: string[];
logger?: any;
isPreview?: boolean;
}

export const getInputs = (_inputs: Record<string, any> = {}, context: Record<string, any> = {}, options: IOptions = {}) => {
const inputs = typeof _inputs === 'string' ? compile(_inputs, context, true, options.isPreview || false) : _inputs;
if (isEmpty(inputs)) return inputs;
function deepCopy(obj: any) {
let result: any = obj?.constructor === Array ? [] : {};
if (typeof obj === 'object') {
for (var i in obj) {
let val = obj[i];
if (typeof val === 'string') {
val = compile(val, context);
val = compile(val, context, true, options.isPreview || false);
}
result[i] = typeof val === 'object' ? deepCopy(val) : val;
}
Expand Down
3 changes: 3 additions & 0 deletions packages/parse-spec/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const debug = isDevsDebugMode() ? require('@serverless-cd/debug')('serverless-de
interface IOptions {
argv?: string[];
logger?: any;
isPreview?: boolean;
}

class ParseSpec {
Expand All @@ -29,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;
this.options.isPreview = this.options.isPreview || false;
this.init(filePath);
debug(`yaml path: ${this.yaml.path}`);
debug(`argv: ${JSON.stringify(this.options.argv)}`);
Expand Down Expand Up @@ -190,6 +192,7 @@ class ParseSpec {
projectName: this.record.projectName,
access: this.record.access,
environment: this.yaml.environment,
isPreview: this.options.isPreview,
};
}
async start(): Promise<ISpec> {
Expand Down
10 changes: 5 additions & 5 deletions packages/parse-spec/src/parse-content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class ParseContent {
},
};
// parse props magic
set(res, 'that.props', getInputs(temp[name].props, res));
set(res, 'that.props', getInputs(temp[name].props, res, this.options));
debug(`getMagicProps: ${JSON.stringify(res)}`);
return res;
}
Expand All @@ -74,7 +74,7 @@ class ParseContent {
const { resources, ...rest } = this.content;
this.content = {
...this.content,
...getInputs(rest, this.getCommonMagic()),
...getInputs(rest, this.getCommonMagic(), this.options),
};
// support resources.info, all steps are needed
const allSteps = [];
Expand All @@ -84,7 +84,7 @@ class ParseContent {
const compile = require('@serverless-devs/art-template/lib/devs-compile');
const component = compile(get(element, 'component'), this.getCommonMagic());
let template = get(this.content.template, get(element, 'extend.name'), {});
template = getInputs(omit(template, get(element, 'extend.ignore', [])), this.getCommonMagic());
template = getInputs(omit(template, get(element, 'extend.ignore', [])), this.getCommonMagic(), this.options);

// 先对env.yaml解析并覆盖
const source = extend2(true, {}, template, element.props); // 修改target为source
Expand All @@ -96,14 +96,14 @@ class ParseContent {
if (filteredEnv && !isEmpty(get(filteredEnv, 'overlays.resources'))) {
filteredEnv.overlays.resources = pickBy(filteredEnv.overlays.resources, (value, key) => key === project);
}
const environment = getInputs(filteredEnv, this.getEnvMagic({ source }));
const environment = getInputs(filteredEnv, this.getEnvMagic({ source }), this.options);
const region = get(environment, 'infraStack.region') ? { region: get(environment, 'infraStack.region') } : {};
debug(`real environment: ${JSON.stringify(environment)}`);
// 覆盖的优先级:resources > components > s.yaml
set(element, 'props', extend2(true, {}, source, get(environment, `overlays.components.${component}`, {}), get(environment, `overlays.resources.${project}`, {}), region));

// 解析s.yaml
const real = getInputs(element, this.getMagicProps({ projectName: project, access, component }));
const real = getInputs(element, this.getMagicProps({ projectName: project, access, component }), this.options);
this.content = {
...this.content,
access,
Expand Down

0 comments on commit 3ededc4

Please sign in to comment.