Skip to content

feat: add 'replace_output' props to plugin, allowing plugins to modif… #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
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.6-beta.3",
"version": "0.1.6-beta.9",
"description": "a engine lib for serverless-devs",
"main": "lib/index.js",
"scripts": {
Expand Down
6 changes: 5 additions & 1 deletion packages/engine/src/actions/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IAction, IActionLevel, IActionType, IAllowFailure, IComponentAction, IHookType, IPluginAction, IRunAction, getInputs } from '@serverless-devs/parse-spec';
import { isEmpty, filter, includes, set, get } from 'lodash';
import { isEmpty, filter, includes, set, get, cloneDeep } from 'lodash';
import * as utils from '@serverless-devs/utils';
import { DevsError, ETrackerType } from '@serverless-devs/utils';
import fs from 'fs-extra';
Expand Down Expand Up @@ -248,6 +248,10 @@ You can still use them now, but we suggest to modify them.`)
const inputs = isEmpty(this.record.pluginOutput) ? this.inputs : this.record.pluginOutput;
// Execute the plugin with the determined inputs and provided arguments.
this.record.pluginOutput = await instance(inputs, hook.args, this.logger);
// If prop 'replace_output' is true, replace the record's step output with the plugin output.
if (hook.replace_output) {
this.record.step.output = cloneDeep(this.record.pluginOutput);
}
} catch (e) {
const error = e as Error;
// Check if the failure is allowed based on the record's allowFailure setting.
Expand Down
47 changes: 38 additions & 9 deletions packages/engine/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,19 +393,48 @@ class Engine {
const projectInfo = get(this.info, item.projectName);
if (!projectInfo || isEmpty(projectInfo) || isNil(projectInfo)) {
this.logger.write(`${chalk.gray(`execute info command of [${item.projectName}]...`)}`);
let args = cloneDeep(this.options.args);
if (args) {
args[0] = 'info';
}
// 20250520: support action for ${resources.xx.info.xx}
const newParseSpecInstance = new ParseSpec(get(this.spec, 'yaml.path'), {
argv: args,
logger: this.logger,
});
newParseSpecInstance.start();
const newAction = newParseSpecInstance.parseActions(item.actions, IActionLevel.PROJECT);
const newActionInstance = new Actions(newAction, {
hookLevel: IActionLevel.PROJECT,
projectName: item.projectName,
logger: item.logger,
skipActions: this.spec.skipActions,
});
newActionInstance.setValue('magic', await this.getFilterContext(item));
newActionInstance.setValue('step', item);
newActionInstance.setValue('command', 'info');
const newInputs = await this.getProps(item);
newActionInstance.setValue('componentProps', newInputs);
const pluginResult = await this.actionInstance?.start(IHookType.PRE, newInputs) || {};
const spec = cloneDeep(this.spec);
spec['command'] = 'info';
// 20240912 when -f, don't throw error
const { f, force } = parseArgv(this.options.args);
let res = {}
if (f || force) {
try {
res = await this.doSrc(item, {}, spec);
} catch(e) {
this.logger.warn(get(e, 'data'));
// 20250521: remove previous logic
let res: any = {}
try {
res = await this.doSrc(item, pluginResult, spec);
set(newInputs, 'output', res);
const pluginSuccessResult = await newActionInstance?.start(IHookType.SUCCESS, newInputs);
if (!isEmpty(pluginSuccessResult)) {
res = get(pluginSuccessResult, 'step.output');
}
} else {
res = await this.doSrc(item, {}, spec);
} catch (e) {
this.logger.warn(get(e, 'data'));
res = get(await newActionInstance?.start(IHookType.FAIL, newInputs), 'step.output') || {};
}
const pluginCompleteResult = await newActionInstance?.start(IHookType.COMPLETE, newInputs);
if (!isEmpty(pluginCompleteResult)) {
res = get(pluginCompleteResult, 'step.output');
}
set(this.info, item.projectName, res);
this.logger.write(`${chalk.gray(`[${item.projectName}] info command executed.`)}`);
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.16-beta.1",
"version": "0.0.16-beta.3",
"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.24",
"@serverless-devs/art-template": "^4.13.16-beta.39",
"@serverless-devs/credential": "workspace:^",
"@serverless-devs/downloads": "workspace:^",
"@serverless-devs/secret": "workspace:^",
Expand Down
5 changes: 5 additions & 0 deletions packages/load-application/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ export const getDefaultValue = (value: any) => {
return replace(value, RANDOM_PATTERN, randomId());
};

export const getNumberDefaultValue = (value: any) => {
if (typeof value !== 'number') return;
return value;
};

export const getSecretManager = () => {
const secretManager = SecretManager.getInstance();
return secretManager;
Expand Down
12 changes: 11 additions & 1 deletion packages/load-application/src/v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { isEmpty, includes, split, get, has, set, sortBy, map, concat, keys, sta
import axios from 'axios';
import parse from './parse';
import { IOptions } from './types';
import { getInputs, getUrlWithLatest, getUrlWithVersion, getAllCredential, getDefaultValue, getSecretManager } from './utils';
import { getInputs, getUrlWithLatest, getUrlWithVersion, getAllCredential, getDefaultValue, getSecretManager, getNumberDefaultValue } from './utils';
import YAML from 'yaml';
import inquirer from 'inquirer';
import chalk from 'chalk';
Expand Down Expand Up @@ -338,6 +338,16 @@ class LoadApplication {
default: getDefaultValue(item.default),
validate,
});
} else if (item.type === 'number') {
// number类型
promptList.push({
type: 'input',
message: item.title,
name,
prefix,
default: getNumberDefaultValue(item.default),
validate,
});
}
}
return { promptList, tmpResult };
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.29",
"version": "0.0.30-beta.3",
"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.24",
"@serverless-devs/art-template": "^4.13.16-beta.39",
"@serverless-devs/credential": "workspace:^",
"@serverless-devs/utils": "workspace:^",
"chalk": "^4.1.2",
Expand Down
4 changes: 2 additions & 2 deletions packages/parse-spec/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ class ParseSpec {
const code = get(projects, `${i}.props.code`);
if (code && isString(code)) {
const codePath = utils.getAbsolutePath(get(projects, `${i}.props.code`, ''));
expand(dotenv.config({ path: path.join(codePath, '.env') }));
expand(dotenv.config({ path: path.join(codePath, '.env'), override: true }));
}
}

expand(dotenv.config({ path: path.join(path.dirname(this.yaml.path), '.env') }));
expand(dotenv.config({ path: path.join(path.dirname(this.yaml.path), '.env'), override: true }));
}
private async doExtend() {
// this.yaml = { path: '' }
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 @@ -41,6 +41,7 @@ export interface IPluginAction {
level: `${IActionLevel}`;
projectName: string;
allow_failure?: boolean | IAllowFailure;
replace_output?: boolean;
}
export interface IComponentAction {
hookType: `${IHookType}`;
Expand Down
4 changes: 2 additions & 2 deletions packages/registry/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@serverless-devs/registry",
"version": "0.0.12-beta.5",
"version": "0.0.12-beta.7",
"description": "request for serverless-devs",
"main": "lib/index.js",
"scripts": {
Expand All @@ -23,7 +23,7 @@
"dependencies": {
"@serverless-devs/utils": "workspace:^",
"@serverless-devs/zip": "workspace:^",
"@serverless-devs/art-template": "4.13.16-beta.38",
"@serverless-devs/art-template": "4.13.16-beta.39",
"ajv": "^8.12.0",
"chalk": "^5.3.0",
"js-yaml": "^4.1.0",
Expand Down
18 changes: 0 additions & 18 deletions packages/registry/src/actions/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,6 @@ export const publishSchema = {
"type": "array",
"items": {
"type": "string",
"enum": [
"阿里云",
"腾讯云",
"华为云",
"百度智能云",
"AWS",
"Azure",
"Google Cloud Platform",
"专有云",
"其它",
"火山引擎",
"Alibaba Cloud",
"Tencent Cloud",
"Huawei Cloud",
"Baidu Cloud",
"Private Cloud",
"Others"
]
}
},
"Version": {
Expand Down
Loading