From 38693f294dcacc7ce054f5472fea0782709902a9 Mon Sep 17 00:00:00 2001 From: zxypro1 <1018995004@qq.com> Date: Wed, 20 Mar 2024 00:12:17 +0800 Subject: [PATCH 01/17] feat: verify when exec custom commands Signed-off-by: zxypro1 <1018995004@qq.com> --- packages/engine/package.json | 2 +- packages/engine/src/index.ts | 49 +++++++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/packages/engine/package.json b/packages/engine/package.json index b03270b..6114b6c 100644 --- a/packages/engine/package.json +++ b/packages/engine/package.json @@ -1,6 +1,6 @@ { "name": "@serverless-devs/engine", - "version": "0.1.2-beta.4", + "version": "0.1.2-beta.5", "description": "a engine lib for serverless-devs", "main": "lib/index.js", "scripts": { diff --git a/packages/engine/src/index.ts b/packages/engine/src/index.ts index 7dab95f..35b72a6 100644 --- a/packages/engine/src/index.ts +++ b/packages/engine/src/index.ts @@ -12,6 +12,7 @@ import Logger, { ILoggerInstance } from '@serverless-devs/logger'; import { DevsError, ETrackerType, emoji, getAbsolutePath, getRootHome, getUserAgent, traceid } from '@serverless-devs/utils'; import { EXIT_CODE } from './constants'; import assert from 'assert'; +import Ajv from 'ajv'; export * from './types'; export { verify, preview } from './utils'; @@ -91,7 +92,7 @@ class Engine { return this.context; } const { steps: _steps, yaml, command, access = yaml.access } = this.spec; - this.logger.write(`${emoji('⌛')} Steps for [${command}] of [${get(this.spec, 'yaml.appName')}]\n${chalk.gray('====================')}`); + this.logger.write(chalk.gray(`${emoji('⌛')} Steps for [${command}] of [${get(this.spec, 'yaml.appName')}]\n${chalk.gray('====================')}`)); // 初始化全局的 action this.globalActionInstance = new Actions(yaml.actions, { hookLevel: IActionLevel.GLOBAL, @@ -206,14 +207,54 @@ class Engine { */ private async validate() { const { steps, command, projectName } = this.spec; + let errorsList: any[] = []; + const ajv = new Ajv({ allErrors: true }); + assert(!isEmpty(steps), 'Step is required'); for (const step of steps) { - const instance = await loadComponent(step.component, { engineLogger: this.logger }); + const instance = await loadComponent(step.component, { logger: this.logger, engineLogger: this.logger }); if (projectName && keys(get(instance, 'commands')).includes(projectName)) { - assert(!projectName, `The name of the project [${projectName}] overlaps with a command, please change it's name`); + throw new DevsError(`The name of the project [${projectName}] overlaps with a command, please change it's name.`, { + exitCode: EXIT_CODE.DEVS, + trackerType: ETrackerType.parseException, + prefix: `[${projectName}] failed to [${command}]:` + }); + } + // schema validation + if (get(this.spec, 'yaml.use3x') && get(this.spec, 'yaml.content.validation')) { + const schema = await this.getSchemaByInstance(instance); + if (isEmpty(schema)) continue; + const validate = ajv.compile(JSON.parse(schema)); + if (!validate(step.props)) { + const errors = validate.errors; + if (!errors) continue; + for (const j of errors) { + j.instancePath = step.projectName + '/props' + j.instancePath; + if (j.keyword === 'enum') { + j.message = j.message + ': ' + j.params.allowedValues.join(', '); + } + } + errorsList = errorsList.concat(errors); + } } } - assert(!isEmpty(steps), 'Step is required'); assert(command, 'Command is required'); + if (!isEmpty(errorsList)) { + throw new DevsError(`${ajv.errorsText(errorsList, { dataVar: '', separator: '\n' })}`, { + exitCode: EXIT_CODE.DEVS, + trackerType: ETrackerType.parseException, + prefix: 'Function props validation error:' + }); + } + } + + /** + * Get schema by existing instance, avoid loading components. + * @param instance loadComponent instance + * @param logger Logger + */ + private getSchemaByInstance(instance: any) { + if (!instance || !instance.getSchema) return null; + return instance.getSchema(); } /** From 092d68517d8b269afc01656c5dcb0fb1a671b0c7 Mon Sep 17 00:00:00 2001 From: zxypro1 <1018995004@qq.com> Date: Wed, 20 Mar 2024 10:34:36 +0800 Subject: [PATCH 02/17] fix ci Signed-off-by: zxypro1 <1018995004@qq.com> --- packages/engine/__tests__/index.test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/engine/__tests__/index.test.ts b/packages/engine/__tests__/index.test.ts index 91b39c1..90f2d07 100644 --- a/packages/engine/__tests__/index.test.ts +++ b/packages/engine/__tests__/index.test.ts @@ -3,6 +3,7 @@ import Engine from '../src'; import path from 'path'; import { AssertionError } from 'assert'; import { get } from 'lodash'; +import { DevsError } from '@serverless-devs/utils'; test('指定 template 不存在', async () => { const engine = new Engine({ @@ -330,7 +331,7 @@ test('validate projectName', async () => { }); const context = await engine.start(); console.log(context); - expect(get(context, 'error[0]')).toBeInstanceOf(AssertionError); + expect(get(context, 'error[0]')).toBeInstanceOf(DevsError); expect(get(context, 'error[0].message')).toBe(`The name of the project [deploy] overlaps with a command, please change it's name`); expect(get(context, 'error[0].code')).toBe('ERR_ASSERTION'); }); From 9b85228d4b03f4387a270b0d1703bc939ac9f32a Mon Sep 17 00:00:00 2001 From: zxypro1 <1018995004@qq.com> Date: Wed, 20 Mar 2024 14:35:32 +0800 Subject: [PATCH 03/17] feat: use default env wouldn't block the process Signed-off-by: zxypro1 <1018995004@qq.com> --- packages/engine/src/actions/index.ts | 2 +- packages/engine/src/index.ts | 4 ++-- packages/parse-spec/src/index.ts | 29 +++++++++++----------------- 3 files changed, 14 insertions(+), 21 deletions(-) diff --git a/packages/engine/src/actions/index.ts b/packages/engine/src/actions/index.ts index 3cc013d..10c6033 100644 --- a/packages/engine/src/actions/index.ts +++ b/packages/engine/src/actions/index.ts @@ -325,7 +325,7 @@ You can still use them now, but we suggest to modify them.`) exitCode: EXIT_CODE.DEVS, prefix: `${this.record.lable} ${hook.hookType}-action failed to [${this.record.command}]:`, tips: `Please check the component ${componentName} has the ${command} command. Serverless Devs documents:${chalk.underline( - 'https://github.com/Serverless-Devs/Serverless-Devs/blob/master/docs/zh/command', + 'https://manual.serverless-devs.com/', )}`, trackerType: ETrackerType.parseException, }); diff --git a/packages/engine/src/index.ts b/packages/engine/src/index.ts index 35b72a6..71322ec 100644 --- a/packages/engine/src/index.ts +++ b/packages/engine/src/index.ts @@ -640,7 +640,7 @@ class Engine { throw new DevsError(`The [${command}] command was not found.`, { exitCode: EXIT_CODE.DEVS, tips: `Please check the component ${item.component} has the ${command} command. Serverless Devs documents:${chalk.underline( - 'https://github.com/Serverless-Devs/Serverless-Devs/blob/master/docs/zh/command', + 'https://manual.serverless-devs.com/', )}`, prefix: `[${item.projectName}] failed to [${command}]:`, trackerType: ETrackerType.parseException, @@ -670,7 +670,7 @@ class Engine { // 方法不存在,进行警告,但是并不会报错,最终的exit code为0; this.logger.tips( `The [${command}] command was not found.`, - `Please check the component ${item.component} has the ${command} command. Serverless Devs documents:https://github.com/Serverless-Devs/Serverless-Devs/blob/master/docs/zh/command`, + `Please check the component ${item.component} has the ${command} command. Serverless Devs documents:https://manual.serverless-devs.com/`, ); } diff --git a/packages/parse-spec/src/index.ts b/packages/parse-spec/src/index.ts index 8d4da1e..ff65aa4 100644 --- a/packages/parse-spec/src/index.ts +++ b/packages/parse-spec/src/index.ts @@ -128,39 +128,32 @@ class ParseSpec { if (has(this.yaml.content, ENVIRONMENT_KEY)) { const envPath: string = utils.getAbsolutePath(get(this.yaml.content, ENVIRONMENT_KEY), path.dirname(this.yaml.path)); const envYamlContent = utils.getYamlContent(envPath); + // 若存在环境变量,默认项目为devsProject + const devsProject = process.env.ALIYUN_DEVS_REMOTE_PROJECT_NAME; + const project = devsProject ? devsProject : get(this.yaml.content, 'name'); // env.yaml is not exist if (isEmpty(envYamlContent)) { - throw new DevsError(`Environment file [${envPath}] is not found`, { - tips: 'You can create a new environment file by running `s env init`', - trackerType: ETrackerType.parseException, - }); + this.options.logger.warn(`Environment file [${envPath}] is not found, run without environment.`); + return {}; } // default-env.json is not exist if (!fs.existsSync(ENVIRONMENT_FILE_PATH)) { - throw new DevsError('Default env is not found', { - tips: 'You can set a default environment by running `s env default`', - trackerType: ETrackerType.parseException, - }); + this.options.logger.warn(`Default env config file [${ENVIRONMENT_FILE_PATH}] is not found, run without environment.`); + return {}; } const { environments } = envYamlContent; - // 若存在环境变量,默认项目为devsProject - const devsProject = process.env.ALIYUN_DEVS_REMOTE_PROJECT_NAME; - const project = devsProject ? devsProject : get(this.yaml.content, 'name'); const defaultEnvContent = require(ENVIRONMENT_FILE_PATH); const defaultEnv = get(find(defaultEnvContent, { project: project }), 'default'); // project is not found in default-env.json if (!defaultEnv) { - throw new DevsError('Default env is not found', { - tips: 'You can set a default environment by running `s env default`', - trackerType: ETrackerType.parseException, - }); + this.options.logger.warn(`Default env is not set, run without environment.`); + return {}; } const environment = find(environments, item => item.name === defaultEnv); // default env is not found in env.yaml if (isEmpty(environment)) { - throw new DevsError(`Default env [${defaultEnv}] was not found`, { - trackerType: ETrackerType.parseException, - }); + this.options.logger.warn(`Default env [${defaultEnv}] is not found, run without environment.`); + return {}; } return { project, environment }; } From 431f7880c2a1defbd992d0724475e4b3c4f8457e Mon Sep 17 00:00:00 2001 From: zxypro1 <1018995004@qq.com> Date: Wed, 20 Mar 2024 14:41:55 +0800 Subject: [PATCH 04/17] publish: engine@0.1.2-beta.6, parse-spec@0.0.26-beta.2 Signed-off-by: zxypro1 <1018995004@qq.com> --- packages/engine/package.json | 2 +- packages/parse-spec/package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/engine/package.json b/packages/engine/package.json index 6114b6c..6b32852 100644 --- a/packages/engine/package.json +++ b/packages/engine/package.json @@ -1,6 +1,6 @@ { "name": "@serverless-devs/engine", - "version": "0.1.2-beta.5", + "version": "0.1.2-beta.6", "description": "a engine lib for serverless-devs", "main": "lib/index.js", "scripts": { diff --git a/packages/parse-spec/package.json b/packages/parse-spec/package.json index 2df081e..9773277 100644 --- a/packages/parse-spec/package.json +++ b/packages/parse-spec/package.json @@ -1,6 +1,6 @@ { "name": "@serverless-devs/parse-spec", - "version": "0.0.26-beta.1", + "version": "0.0.26-beta.2", "description": "a parse yaml spec lib for serverless-devs", "main": "lib/index.js", "scripts": { From e61a76cfd308af692ba50d80a75a6632dfb440af Mon Sep 17 00:00:00 2001 From: zxypro1 <1018995004@qq.com> Date: Thu, 21 Mar 2024 14:34:57 +0800 Subject: [PATCH 05/17] feat: specify env wouldn't block command process Signed-off-by: zxypro1 <1018995004@qq.com> --- packages/engine/src/actions/index.ts | 21 +++++++++--------- packages/engine/src/index.ts | 2 +- packages/logger/src/engine-logger/index.ts | 25 ++++++++++++++++++++++ packages/parse-spec/src/index.ts | 19 +++++++--------- 4 files changed, 45 insertions(+), 22 deletions(-) diff --git a/packages/engine/src/actions/index.ts b/packages/engine/src/actions/index.ts index 10c6033..ec7a336 100644 --- a/packages/engine/src/actions/index.ts +++ b/packages/engine/src/actions/index.ts @@ -18,7 +18,7 @@ interface IRecord { magic: Record; // 记录魔法变量 componentProps: Record; // 记录组件的inputs pluginOutput: Record; // 记录plugin的outputs - lable: string; // 记录执行的label + label: string; // 记录执行的label step: IStepOptions; // 记录当前step allowFailure: boolean | IAllowFailure; // step allow_failure > action allow_failure command: string; // 记录当前执行的command @@ -106,8 +106,8 @@ You can still use them now, but we suggest to modify them.`) const hooks = filter(this.actions, item => item.hookType === hookType); if (isEmpty(hooks)) return {}; this.record.startTime = Date.now(); - this.record.lable = this.option.hookLevel === IActionLevel.PROJECT ? `[${this.option.projectName}]` : IActionLevel.GLOBAL; - this.logger.debug(`Start executing the ${hookType}-action in ${this.record.lable}`); + this.record.label = this.option.hookLevel === IActionLevel.PROJECT ? `[${this.option.projectName}]` : IActionLevel.GLOBAL; + this.logger.debug(`Start executing the ${hookType}-action in ${this.record.label}`); // 确保 hooks 中的变量均为解析过后的真实值 const newHooks = getInputs(hooks, this.record.magic); // post-action应获取componentProps, 先清空pluginOutput @@ -128,7 +128,7 @@ You can still use them now, but we suggest to modify them.`) await this.component(hook); } } - this.logger.debug(`The ${hookType}-action successfully to execute in ${this.record.lable}`); + this.logger.debug(`The ${hookType}-action successfully to execute in ${this.record.label}`); if (this.option.hookLevel === IActionLevel.GLOBAL) { this.logger.write(`${chalk.green('✔')} ${chalk.gray(`${IActionLevel.GLOBAL} ${hookType}-action completed (${getProcessTime(this.record.startTime)})`)}`); @@ -207,7 +207,7 @@ You can still use them now, but we suggest to modify them.`) data: get(e, 'data'), stack: error.stack, exitCode: EXIT_CODE.RUN, - prefix: `${this.record.lable} ${hook.hookType}-action failed to [${this.record.command}]:`, + prefix: `${this.record.label} ${hook.hookType}-action failed to [${this.record.command}]:`, trackerType: ETrackerType.runtimeException, }); } @@ -221,7 +221,7 @@ You can still use them now, but we suggest to modify them.`) if (useAllowFailure) return; throw new DevsError(`The ${hook.path} directory does not exist.`, { exitCode: EXIT_CODE.DEVS, - prefix: `${this.record.lable} ${hook.hookType}-action failed to [${this.record.command}]:`, + prefix: `${this.record.label} ${hook.hookType}-action failed to [${this.record.command}]:`, trackerType: ETrackerType.parseException, }); } @@ -257,7 +257,7 @@ You can still use them now, but we suggest to modify them.`) data: get(e, 'data'), stack: error.stack, exitCode: EXIT_CODE.PLUGIN, - prefix: `${this.record.lable} ${hook.hookType}-action failed to [${this.record.command}]:`, + prefix: `${this.record.label} ${hook.hookType}-action failed to [${this.record.command}]:`, trackerType: ETrackerType.runtimeException, }); } @@ -295,7 +295,8 @@ You can still use them now, but we suggest to modify them.`) }; try { // Execute the command for the component with the prepared inputs. - return await instance[command](newInputs); + await instance[command](newInputs); + return; } catch (e) { const error = e as Error; // Check if the failure is allowed based on the record's allowFailure setting. @@ -308,7 +309,7 @@ You can still use them now, but we suggest to modify them.`) data: get(e, 'data'), stack: error.stack, exitCode: EXIT_CODE.COMPONENT, - prefix: `${this.record.lable} ${hook.hookType}-action failed to [${this.record.command}]:`, + prefix: `${this.record.label} ${hook.hookType}-action failed to [${this.record.command}]:`, trackerType: ETrackerType.runtimeException, }); } @@ -323,7 +324,7 @@ You can still use them now, but we suggest to modify them.`) // 方法不存在,此时系统将会认为是未找到组件方法,系统的exit code为100; throw new DevsError(`The [${command}] command was not found.`, { exitCode: EXIT_CODE.DEVS, - prefix: `${this.record.lable} ${hook.hookType}-action failed to [${this.record.command}]:`, + prefix: `${this.record.label} ${hook.hookType}-action failed to [${this.record.command}]:`, tips: `Please check the component ${componentName} has the ${command} command. Serverless Devs documents:${chalk.underline( 'https://manual.serverless-devs.com/', )}`, diff --git a/packages/engine/src/index.ts b/packages/engine/src/index.ts index 71322ec..71daaa9 100644 --- a/packages/engine/src/index.ts +++ b/packages/engine/src/index.ts @@ -339,7 +339,7 @@ class Engine { cwd: path.dirname(this.spec.yaml.path), vars: this.spec.yaml.vars, resources: {}, - __runtime: this.options.verify ? 'enigne' : 'parse', + __runtime: this.options.verify ? 'engine' : 'parse', __steps: this.context.steps, } as Record; for (const obj of this.context.steps) { diff --git a/packages/logger/src/engine-logger/index.ts b/packages/logger/src/engine-logger/index.ts index 8a708f6..b43e1c9 100644 --- a/packages/logger/src/engine-logger/index.ts +++ b/packages/logger/src/engine-logger/index.ts @@ -13,6 +13,9 @@ export default class EngineLogger extends Logger { private eol: string; private key: string; private level: LoggerLevel; + private writeMsg: string; + private tipsMsg: string; + private warnMsg: string; constructor(props: IOptions) { super({} as EggLoggerOptions); @@ -29,6 +32,9 @@ export default class EngineLogger extends Logger { this.level = level || get(props, 'level', 'INFO'); this.eol = eol; this.key = key; + this.writeMsg = ''; + this.tipsMsg = ''; + this.warnMsg = ''; const consoleTransport = new ConsoleTransport({ level: level || get(props, 'level', 'INFO'), @@ -81,10 +87,29 @@ export default class EngineLogger extends Logger { this.write(msg); } + tipsOnce(message: string, tips?: string) { + if (this.tipsMsg === message + tips) return; + this.tipsMsg = message + tips; + this.tips(message, tips); + } + write(msg: string) { super.write(transport.transportSecrets(msg)); } + // 多个相同write输出的时候,只保留第一个 + writeOnce(msg: string) { + if (msg === this.writeMsg) return; + this.writeMsg = msg; + this.write(msg); + } + + warnOnce(msg: string) { + if (msg === this.warnMsg) return; + this.warnMsg = msg; + super.warn(msg); + } + private setEol(eol: string = os.EOL) { const c = this.get('console') as object; const f = this.get('file'); diff --git a/packages/parse-spec/src/index.ts b/packages/parse-spec/src/index.ts index ff65aa4..263e92e 100644 --- a/packages/parse-spec/src/index.ts +++ b/packages/parse-spec/src/index.ts @@ -133,12 +133,12 @@ class ParseSpec { const project = devsProject ? devsProject : get(this.yaml.content, 'name'); // env.yaml is not exist if (isEmpty(envYamlContent)) { - this.options.logger.warn(`Environment file [${envPath}] is not found, run without environment.`); + this.options.logger.warnOnce(`Environment file [${envPath}] is not found, run without environment.`); return {}; } // default-env.json is not exist if (!fs.existsSync(ENVIRONMENT_FILE_PATH)) { - this.options.logger.warn(`Default env config file [${ENVIRONMENT_FILE_PATH}] is not found, run without environment.`); + this.options.logger.warnOnce(`Default env config file [${ENVIRONMENT_FILE_PATH}] is not found, run without environment.`); return {}; } const { environments } = envYamlContent; @@ -146,13 +146,13 @@ class ParseSpec { const defaultEnv = get(find(defaultEnvContent, { project: project }), 'default'); // project is not found in default-env.json if (!defaultEnv) { - this.options.logger.warn(`Default env is not set, run without environment.`); + this.options.logger.warnOnce(`Default env is not set, run without environment.`); return {}; } const environment = find(environments, item => item.name === defaultEnv); // default env is not found in env.yaml if (isEmpty(environment)) { - this.options.logger.warn(`Default env [${defaultEnv}] is not found, run without environment.`); + this.options.logger.warnOnce(`Default env [${defaultEnv}] is not found, run without environment.`); return {}; } return { project, environment }; @@ -171,9 +171,8 @@ class ParseSpec { const envYamlContent = utils.getYamlContent(envPath); // env file is not exist if (isEmpty(envYamlContent)) { - throw new DevsError(`Environment file [${envPath}] is not exist`, { - trackerType: ETrackerType.parseException, - }); + this.options.logger.warnOnce(`Environment file [${envPath}] is not found, run without environment.`); + return {}; } debug(`environment content: ${JSON.stringify(envYamlContent)}`); const { environments } = envYamlContent; @@ -183,10 +182,8 @@ class ParseSpec { const environment = find(environments, item => item.name === this.record.env); // env name is not found if (isEmpty(environment)) { - // TODO: @封崇 - throw new DevsError(`Env [${this.record.env}] was not found`, { - trackerType: ETrackerType.parseException, - }); + this.options.logger.warnOnce(`Env [${this.record.env}] was not found, run without environment.`); + return {}; } return { project, environment }; } From cb29ec71b634277f00dbe24b2f15ebdeade77e6c Mon Sep 17 00:00:00 2001 From: zxypro1 <1018995004@qq.com> Date: Thu, 21 Mar 2024 14:37:32 +0800 Subject: [PATCH 06/17] publish: parse-spec@0.0.26-beta.3, logger@0.0.5-beta.2, engine@0.1.2-beta.7 Signed-off-by: zxypro1 <1018995004@qq.com> --- packages/engine/package.json | 2 +- packages/logger/package.json | 2 +- packages/parse-spec/package.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/engine/package.json b/packages/engine/package.json index 6b32852..a9fd02b 100644 --- a/packages/engine/package.json +++ b/packages/engine/package.json @@ -1,6 +1,6 @@ { "name": "@serverless-devs/engine", - "version": "0.1.2-beta.6", + "version": "0.1.2-beta.7", "description": "a engine lib for serverless-devs", "main": "lib/index.js", "scripts": { diff --git a/packages/logger/package.json b/packages/logger/package.json index 064ad03..d4bb37d 100644 --- a/packages/logger/package.json +++ b/packages/logger/package.json @@ -1,6 +1,6 @@ { "name": "@serverless-devs/logger", - "version": "0.0.5-beta.1", + "version": "0.0.5-beta.2", "description": "", "main": "lib/index.js", "scripts": { diff --git a/packages/parse-spec/package.json b/packages/parse-spec/package.json index 9773277..abf1a9e 100644 --- a/packages/parse-spec/package.json +++ b/packages/parse-spec/package.json @@ -1,6 +1,6 @@ { "name": "@serverless-devs/parse-spec", - "version": "0.0.26-beta.2", + "version": "0.0.26-beta.3", "description": "a parse yaml spec lib for serverless-devs", "main": "lib/index.js", "scripts": { From cb9cf2378f69da426a212b440e2d98a94c0ab250 Mon Sep 17 00:00:00 2001 From: zxypro1 <1018995004@qq.com> Date: Mon, 1 Apr 2024 02:45:09 +0800 Subject: [PATCH 07/17] feat: support github registry Signed-off-by: zxypro1 <1018995004@qq.com> --- packages/downloads/src/index.ts | 16 ++++++ packages/load-application/src/constant.ts | 3 ++ packages/load-application/src/index.ts | 7 ++- packages/load-application/src/utils/index.ts | 9 +++- packages/load-application/src/v2.ts | 34 +++++++++--- packages/load-application/src/v3.ts | 57 +++++++++++++++----- 6 files changed, 100 insertions(+), 26 deletions(-) diff --git a/packages/downloads/src/index.ts b/packages/downloads/src/index.ts index 9288552..2084d45 100644 --- a/packages/downloads/src/index.ts +++ b/packages/downloads/src/index.ts @@ -79,6 +79,22 @@ class Download { // ignore error in windows } } + // support github zip + const items = fs.readdirSync(dest as string, { withFileTypes: true }); + const directories = items.filter(item => item.isDirectory()); + // only one directory, move + if (directories.length === 1 && items.length === 1) { + try { + const directoryName = directories[0].name; + const directoryPath = path.join(dest as string, directoryName); + const tmpFilePath = path.join(dest as string, `../${filePath}-${Date.now()}`); + fs.moveSync(directoryPath, tmpFilePath, { overwrite: true }); + fs.moveSync(tmpFilePath, dest as string, { overwrite: true }); + fs.removeSync(tmpFilePath); + } catch(e) { + throw e; + } + } } private async doDownload(url: string): Promise { const { headers, logger } = this.options; diff --git a/packages/load-application/src/constant.ts b/packages/load-application/src/constant.ts index d2bbe74..89f46e8 100644 --- a/packages/load-application/src/constant.ts +++ b/packages/load-application/src/constant.ts @@ -1,12 +1,15 @@ import chalk from 'chalk'; +import { getGlobalConfig } from '@serverless-devs/utils'; export const gray = chalk.hex('#8c8d91'); export const RANDOM_PATTERN = '${default-suffix}'; export const DEVSAPP = 'devsapp'; +export const GITHUB_REGISTRY = 'https://api.github.com/repos'; export const REGISTRY = { V2: 'https://registry.devsapp.cn/simple', V3: 'https://api.devsapp.cn/v3', + CUSTOM_URL: getGlobalConfig('registry'), }; export const CONFIGURE_LATER = 'configure_later'; diff --git a/packages/load-application/src/index.ts b/packages/load-application/src/index.ts index b27bb55..0de9698 100644 --- a/packages/load-application/src/index.ts +++ b/packages/load-application/src/index.ts @@ -2,21 +2,24 @@ import V3 from './v3'; import V2 from './v2'; import assert from 'assert'; import { IOptions } from './types'; -import { includes } from 'lodash'; +import { includes, get } from 'lodash'; +import { REGISTRY } from './constant'; const debug = require('@serverless-cd/debug')('serverless-devs:load-appliaction'); export default async (template: string, options: IOptions = {}) => { debug(`load application, template: ${template}, options: ${JSON.stringify(options)}`); + const { logger } = options; if (options.uri) { return await v3(template, options); } assert(template, 'template is required'); - if (includes(template, '/')) { + if (includes(template, '/') && REGISTRY.CUSTOM_URL in [REGISTRY.V3, REGISTRY.V2]) { return await v2(template, options); } try { return await v3(template, options); } catch (error) { + logger.warn(get(error, 'message') + ', try to load from v2 registry.'); debug(`v3 error, ${error}`); return await v2(template, options); } diff --git a/packages/load-application/src/utils/index.ts b/packages/load-application/src/utils/index.ts index 5ab6650..6075a08 100644 --- a/packages/load-application/src/utils/index.ts +++ b/packages/load-application/src/utils/index.ts @@ -1,5 +1,5 @@ import { endsWith, keys, replace } from 'lodash'; -import { RANDOM_PATTERN, REGISTRY } from '../constant'; +import { RANDOM_PATTERN, REGISTRY, GITHUB_REGISTRY } from '../constant'; import Credential from '@serverless-devs/credential'; export { default as getInputs } from './get-inputs'; @@ -10,7 +10,12 @@ export const tryfun = async (fn: Function, ...args: any[]) => { } catch (ex) {} }; -export const getUrlWithLatest = (name: string) => `${REGISTRY.V3}/packages/${name}/release/latest`; +export const getUrlWithLatest = (name: string) => { + if (REGISTRY.CUSTOM_URL === GITHUB_REGISTRY) { + return `${REGISTRY.CUSTOM_URL}/${name}`; + } + return `${REGISTRY.V3}/packages/${name}/release/latest` +}; export const getUrlWithVersion = (name: string, versionId: string) => `${REGISTRY.V3}/packages/${name}/release/tags/${versionId}`; export const randomId = () => Math.random().toString(36).substring(2, 6); diff --git a/packages/load-application/src/v2.ts b/packages/load-application/src/v2.ts index 793515b..253b337 100644 --- a/packages/load-application/src/v2.ts +++ b/packages/load-application/src/v2.ts @@ -4,7 +4,7 @@ import axios from 'axios'; import download from '@serverless-devs/downloads'; import artTemplate from 'art-template'; import { getYamlContent, isCiCdEnvironment, getYamlPath } from '@serverless-devs/utils'; -import { isEmpty, includes, split, get, has, set, sortBy, map, concat, keys, find } from 'lodash'; +import { isEmpty, includes, split, get, has, set, sortBy, map, concat, keys, find, startsWith } from 'lodash'; import parse from './parse'; import { IProvider, IOptions } from './types'; import { CONFIGURE_LATER, DEFAULT_MAGIC_ACCESS, REGISTRY } from './constant'; @@ -374,13 +374,31 @@ class LoadApplication { private async doLoad() { const { logger } = this.options; const zipball_url = this.version ? await this.doZipballUrlWithVersion() : await this.doZipballUrl(); - await download(zipball_url, { - dest: this.tempPath, - logger, - extract: true, - strip: 1, - filename: this.name, - }); + try { + await download(zipball_url, { + dest: this.tempPath, + logger, + extract: true, + strip: 1, + filename: this.name, + }); + } catch(e) { + logger.debug(e); + // if https, try http + if (startsWith(zipball_url, 'https')) { + logger.debug('https error, try http'); + const newZipballUrl = zipball_url.replace('https://', 'http://'); + await download(newZipballUrl, { + dest: this.tempPath, + logger, + extract: true, + strip: 1, + filename: this.name, + }); + } else { + throw e; + } + } } private async doZipballUrl() { const maps = { diff --git a/packages/load-application/src/v3.ts b/packages/load-application/src/v3.ts index bbcdf0b..e077cc0 100644 --- a/packages/load-application/src/v3.ts +++ b/packages/load-application/src/v3.ts @@ -4,17 +4,16 @@ import download from '@serverless-devs/downloads'; import _artTemplate from 'art-template'; import _devsArtTemplate from '@serverless-devs/art-template'; import { getYamlContent, registry, isCiCdEnvironment, getYamlPath } from '@serverless-devs/utils'; -import { isEmpty, includes, split, get, has, set, sortBy, map, concat, keys } from 'lodash'; +import { isEmpty, includes, split, get, has, set, sortBy, map, concat, keys, startsWith } from 'lodash'; import axios from 'axios'; import parse from './parse'; import { IOptions } from './types'; import { getInputs, getUrlWithLatest, getUrlWithVersion, getAllCredential, getDefaultValue } from './utils'; -import assert from 'assert'; import YAML from 'yaml'; import inquirer from 'inquirer'; import chalk from 'chalk'; import Credential from '@serverless-devs/credential'; -import { CONFIGURE_LATER, DEFAULT_MAGIC_ACCESS, gray } from './constant'; +import { CONFIGURE_LATER, DEFAULT_MAGIC_ACCESS, GITHUB_REGISTRY, gray } from './constant'; const debug = require('@serverless-cd/debug')('serverless-devs:load-appliaction'); class LoadApplication { @@ -52,7 +51,7 @@ class LoadApplication { */ private secretList: string[] = []; constructor(private template: string, private options: IOptions = {}) { - assert(!includes(this.template, '/'), `The component name ${this.template} cannot contain /`); + // assert(!includes(this.template, '/'), `The component name ${this.template} cannot contain /`); this.options.dest = this.options.dest || process.cwd(); this.options.logger = this.options.logger || console; const [name, version] = split(this.template, '@'); @@ -372,22 +371,52 @@ class LoadApplication { const { logger } = this.options; const zipball_url = this.options.uri || (await this.getZipballUrl()); debug(`zipball_url: ${zipball_url}`); - await download(zipball_url, { - dest: this.tempPath, - logger, - extract: true, - headers: { - ...registry.getSignHeaders(), - devs_mock_env: process.env.DEVS_MOCK_ENV || 'false', - }, - filename: this.name, - }); + try { + await download(zipball_url, { + dest: this.tempPath, + logger, + extract: true, + headers: { + 'User-Agent': 'Serverless-Devs (https://github.com/Serverless-Devs/Serverless-Devs)', + ...registry.getSignHeaders(), + devs_mock_env: process.env.DEVS_MOCK_ENV || 'false', + }, + // use final element as filename + filename: split(this.name, '/')[-1], + }); + } catch(e) { + logger.debug(e); + // if https, try http + if (startsWith(zipball_url, 'https')) { + logger.debug('https error, try http'); + const newZipballUrl = zipball_url.replace('https://', 'http://'); + await download(zipball_url, { + dest: this.tempPath, + logger, + extract: true, + headers: { + 'User-Agent': 'Serverless-Devs (https://github.com/Serverless-Devs/Serverless-Devs)', + ...registry.getSignHeaders(), + devs_mock_env: process.env.DEVS_MOCK_ENV || 'false', + }, + // use final element as filename + filename: split(this.name, '/')[-1], + }); + } else { + throw e; + } + } } private getZipballUrl = async () => { const url = this.version ? getUrlWithVersion(this.name, this.version) : getUrlWithLatest(this.name); debug(`url: ${url}`); const res = await axios.get(url, { headers: registry.getSignHeaders() }); debug(`res: ${JSON.stringify(res.data)}`); + if (startsWith(url, GITHUB_REGISTRY)) { + const defaultBranch = get(res, 'data.default_branch'); + if (isEmpty(defaultBranch)) throw new Error(`Default branch is not found`); + return url + `/zipball/${defaultBranch}`; + } const zipball_url = get(res, 'data.body.zipball_url'); const template = this.version ? `${this.name}@${this.version}` : this.name; if (isEmpty(zipball_url)) throw new Error(`Application ${template} is not found`); From 63445c9687924a0749333be39d26aa7a4cc5e1ca Mon Sep 17 00:00:00 2001 From: zxypro1 <1018995004@qq.com> Date: Mon, 1 Apr 2024 02:49:05 +0800 Subject: [PATCH 08/17] fix Signed-off-by: zxypro1 <1018995004@qq.com> --- packages/load-application/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/load-application/src/index.ts b/packages/load-application/src/index.ts index 0de9698..76fa34c 100644 --- a/packages/load-application/src/index.ts +++ b/packages/load-application/src/index.ts @@ -13,7 +13,7 @@ export default async (template: string, options: IOptions = {}) => { return await v3(template, options); } assert(template, 'template is required'); - if (includes(template, '/') && REGISTRY.CUSTOM_URL in [REGISTRY.V3, REGISTRY.V2]) { + if ((includes(template, '/') && REGISTRY.CUSTOM_URL === REGISTRY.V3) || (REGISTRY.CUSTOM_URL === REGISTRY.V2)) { return await v2(template, options); } try { From 91c88f9f3a2c3880042b594bc5406a992e79fe72 Mon Sep 17 00:00:00 2001 From: zxypro1 <1018995004@qq.com> Date: Mon, 1 Apr 2024 02:51:13 +0800 Subject: [PATCH 09/17] publish: downloads@0.0.6, load-application@0.0.13-beta.2, load-component@0.0.7-beta.3 Signed-off-by: zxypro1 <1018995004@qq.com> --- packages/downloads/package.json | 2 +- packages/load-application/package.json | 2 +- packages/load-component/package.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/downloads/package.json b/packages/downloads/package.json index 529fd06..c23bb21 100644 --- a/packages/downloads/package.json +++ b/packages/downloads/package.json @@ -1,6 +1,6 @@ { "name": "@serverless-devs/downloads", - "version": "0.0.5", + "version": "0.0.6", "description": "download for serverless-devs", "main": "lib/index.js", "scripts": { diff --git a/packages/load-application/package.json b/packages/load-application/package.json index b28de1c..56d692a 100644 --- a/packages/load-application/package.json +++ b/packages/load-application/package.json @@ -1,6 +1,6 @@ { "name": "@serverless-devs/load-application", - "version": "0.0.13-beta.1", + "version": "0.0.13-beta.2", "description": "load application for serverless-devs", "main": "lib/index.js", "scripts": { diff --git a/packages/load-component/package.json b/packages/load-component/package.json index 35cbde7..c891c1e 100644 --- a/packages/load-component/package.json +++ b/packages/load-component/package.json @@ -1,6 +1,6 @@ { "name": "@serverless-devs/load-component", - "version": "0.0.7-beta.2", + "version": "0.0.7-beta.3", "description": "request for serverless-devs", "main": "lib/index.js", "scripts": { From 1399e840d29ba0c2fbbc45cc5b631d01cae12a1b Mon Sep 17 00:00:00 2001 From: zxypro1 <1018995004@qq.com> Date: Mon, 1 Apr 2024 09:59:28 +0800 Subject: [PATCH 10/17] fix: support custom registry Signed-off-by: zxypro1 <1018995004@qq.com> --- packages/load-application/src/utils/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/load-application/src/utils/index.ts b/packages/load-application/src/utils/index.ts index 6075a08..ce98bb4 100644 --- a/packages/load-application/src/utils/index.ts +++ b/packages/load-application/src/utils/index.ts @@ -1,5 +1,5 @@ import { endsWith, keys, replace } from 'lodash'; -import { RANDOM_PATTERN, REGISTRY, GITHUB_REGISTRY } from '../constant'; +import { RANDOM_PATTERN, REGISTRY } from '../constant'; import Credential from '@serverless-devs/credential'; export { default as getInputs } from './get-inputs'; @@ -11,7 +11,7 @@ export const tryfun = async (fn: Function, ...args: any[]) => { }; export const getUrlWithLatest = (name: string) => { - if (REGISTRY.CUSTOM_URL === GITHUB_REGISTRY) { + if (REGISTRY.CUSTOM_URL !== REGISTRY.V3) { return `${REGISTRY.CUSTOM_URL}/${name}`; } return `${REGISTRY.V3}/packages/${name}/release/latest` From a30bf6bd0323c58e5f8415b3e5ad5ecf3213c66e Mon Sep 17 00:00:00 2001 From: zxypro1 <1018995004@qq.com> Date: Mon, 1 Apr 2024 10:00:33 +0800 Subject: [PATCH 11/17] publish: load-application@0.0.13-beta.3 Signed-off-by: zxypro1 <1018995004@qq.com> --- packages/load-application/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/load-application/package.json b/packages/load-application/package.json index 56d692a..7ef3b0f 100644 --- a/packages/load-application/package.json +++ b/packages/load-application/package.json @@ -1,6 +1,6 @@ { "name": "@serverless-devs/load-application", - "version": "0.0.13-beta.2", + "version": "0.0.13-beta.3", "description": "load application for serverless-devs", "main": "lib/index.js", "scripts": { From 1496044232cd3563703310c461be5789edb7a48a Mon Sep 17 00:00:00 2001 From: zxypro1 <1018995004@qq.com> Date: Mon, 1 Apr 2024 10:55:15 +0800 Subject: [PATCH 12/17] fix: registry provider support huoshan Signed-off-by: zxypro1 <1018995004@qq.com> --- packages/registry/package.json | 2 +- packages/registry/src/actions/constant.ts | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/registry/package.json b/packages/registry/package.json index 14afd4d..83a39cb 100644 --- a/packages/registry/package.json +++ b/packages/registry/package.json @@ -1,6 +1,6 @@ { "name": "@serverless-devs/registry", - "version": "0.0.8", + "version": "0.0.9", "description": "request for serverless-devs", "main": "lib/index.js", "scripts": { diff --git a/packages/registry/src/actions/constant.ts b/packages/registry/src/actions/constant.ts index a96ef39..0b0827d 100644 --- a/packages/registry/src/actions/constant.ts +++ b/packages/registry/src/actions/constant.ts @@ -44,6 +44,7 @@ export const publishSchema = { "Google Cloud Platform", "专有云", "其它", + "火山引擎", "Alibaba Cloud", "Tencent Cloud", "Huawei Cloud", From 0b7c0f68e1ebd5498f1be2644fe8438224410335 Mon Sep 17 00:00:00 2001 From: zxypro1 <1018995004@qq.com> Date: Mon, 1 Apr 2024 11:47:32 +0800 Subject: [PATCH 13/17] publish: load-application@0.0.13-beta.4 Signed-off-by: zxypro1 <1018995004@qq.com> --- packages/load-application/package.json | 2 +- packages/load-application/src/utils/index.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/load-application/package.json b/packages/load-application/package.json index 7ef3b0f..ba7f743 100644 --- a/packages/load-application/package.json +++ b/packages/load-application/package.json @@ -1,6 +1,6 @@ { "name": "@serverless-devs/load-application", - "version": "0.0.13-beta.3", + "version": "0.0.13-beta.4", "description": "load application for serverless-devs", "main": "lib/index.js", "scripts": { diff --git a/packages/load-application/src/utils/index.ts b/packages/load-application/src/utils/index.ts index ce98bb4..6075a08 100644 --- a/packages/load-application/src/utils/index.ts +++ b/packages/load-application/src/utils/index.ts @@ -1,5 +1,5 @@ import { endsWith, keys, replace } from 'lodash'; -import { RANDOM_PATTERN, REGISTRY } from '../constant'; +import { RANDOM_PATTERN, REGISTRY, GITHUB_REGISTRY } from '../constant'; import Credential from '@serverless-devs/credential'; export { default as getInputs } from './get-inputs'; @@ -11,7 +11,7 @@ export const tryfun = async (fn: Function, ...args: any[]) => { }; export const getUrlWithLatest = (name: string) => { - if (REGISTRY.CUSTOM_URL !== REGISTRY.V3) { + if (REGISTRY.CUSTOM_URL === GITHUB_REGISTRY) { return `${REGISTRY.CUSTOM_URL}/${name}`; } return `${REGISTRY.V3}/packages/${name}/release/latest` From 76bfc7df13c70c52fb07dc765b3da552e43692d7 Mon Sep 17 00:00:00 2001 From: zxypro1 <1018995004@qq.com> Date: Tue, 2 Apr 2024 19:33:48 +0800 Subject: [PATCH 14/17] fix: default load repos from devsapp Signed-off-by: zxypro1 <1018995004@qq.com> --- packages/load-application/package.json | 2 +- packages/load-application/src/utils/index.ts | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/load-application/package.json b/packages/load-application/package.json index ba7f743..4bef8f9 100644 --- a/packages/load-application/package.json +++ b/packages/load-application/package.json @@ -1,6 +1,6 @@ { "name": "@serverless-devs/load-application", - "version": "0.0.13-beta.4", + "version": "0.0.13-beta.5", "description": "load application for serverless-devs", "main": "lib/index.js", "scripts": { diff --git a/packages/load-application/src/utils/index.ts b/packages/load-application/src/utils/index.ts index 6075a08..7ab83c2 100644 --- a/packages/load-application/src/utils/index.ts +++ b/packages/load-application/src/utils/index.ts @@ -1,4 +1,4 @@ -import { endsWith, keys, replace } from 'lodash'; +import { endsWith, keys, replace, split } from 'lodash'; import { RANDOM_PATTERN, REGISTRY, GITHUB_REGISTRY } from '../constant'; import Credential from '@serverless-devs/credential'; @@ -12,6 +12,9 @@ export const tryfun = async (fn: Function, ...args: any[]) => { export const getUrlWithLatest = (name: string) => { if (REGISTRY.CUSTOM_URL === GITHUB_REGISTRY) { + if (split(name, '/').length === 1) { + return `${REGISTRY.CUSTOM_URL}/devsapp/${name}`; + } return `${REGISTRY.CUSTOM_URL}/${name}`; } return `${REGISTRY.V3}/packages/${name}/release/latest` From e523b91a8b52ffb07f83381dabb8a29a0d7074d4 Mon Sep 17 00:00:00 2001 From: zxypro1 <1018995004@qq.com> Date: Tue, 9 Apr 2024 16:03:59 +0800 Subject: [PATCH 15/17] fix: action `fc3 invoke` timeout Signed-off-by: zxypro1 <1018995004@qq.com> --- packages/engine/package.json | 2 +- packages/load-component/package.json | 2 +- packages/load-component/src/utils/index.ts | 4 ++++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/engine/package.json b/packages/engine/package.json index a9fd02b..d577f75 100644 --- a/packages/engine/package.json +++ b/packages/engine/package.json @@ -1,6 +1,6 @@ { "name": "@serverless-devs/engine", - "version": "0.1.2-beta.7", + "version": "0.1.2-beta.8", "description": "a engine lib for serverless-devs", "main": "lib/index.js", "scripts": { diff --git a/packages/load-component/package.json b/packages/load-component/package.json index c891c1e..48827b2 100644 --- a/packages/load-component/package.json +++ b/packages/load-component/package.json @@ -1,6 +1,6 @@ { "name": "@serverless-devs/load-component", - "version": "0.0.7-beta.3", + "version": "0.0.7-beta.4", "description": "request for serverless-devs", "main": "lib/index.js", "scripts": { diff --git a/packages/load-component/src/utils/index.ts b/packages/load-component/src/utils/index.ts index 3040cbe..84a3fc9 100644 --- a/packages/load-component/src/utils/index.ts +++ b/packages/load-component/src/utils/index.ts @@ -42,6 +42,10 @@ const getEntryFile = async (componentPath: string) => { export const buildComponentInstance = async (componentPath: string, params?: any) => { const requirePath = await getEntryFile(componentPath); + // bug: `- component: fc invoke` timeout. Delete require cache + try { + delete require.cache[requirePath]; + } catch {} const baseChildComponent = await require(requirePath); const ChildComponent = baseChildComponent.default || baseChildComponent; From 5bc1b74dcdf788865bda4e7e6155f84dbec85ad9 Mon Sep 17 00:00:00 2001 From: zxypro1 <1018995004@qq.com> Date: Tue, 9 Apr 2024 17:55:37 +0800 Subject: [PATCH 16/17] fix: action component command not found Signed-off-by: zxypro1 <1018995004@qq.com> --- packages/engine/package.json | 2 +- packages/engine/src/actions/index.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/engine/package.json b/packages/engine/package.json index d577f75..bf5fc2b 100644 --- a/packages/engine/package.json +++ b/packages/engine/package.json @@ -1,6 +1,6 @@ { "name": "@serverless-devs/engine", - "version": "0.1.2-beta.8", + "version": "0.1.2-beta.9", "description": "a engine lib for serverless-devs", "main": "lib/index.js", "scripts": { diff --git a/packages/engine/src/actions/index.ts b/packages/engine/src/actions/index.ts index ec7a336..d70bb8b 100644 --- a/packages/engine/src/actions/index.ts +++ b/packages/engine/src/actions/index.ts @@ -291,7 +291,7 @@ You can still use them now, but we suggest to modify them.`) // 方法存在,执行报错,退出码101 const newInputs = { ...this.record.componentProps, - argv: filter(argv.slice(2), o => !includes([componentName, command], o)), + args: filter(argv.slice(2), o => !includes([componentName, command], o)), }; try { // Execute the command for the component with the prepared inputs. From 31aa9fd21d88c79cb1f3378173adda2c867f6501 Mon Sep 17 00:00:00 2001 From: zxypro1 <1018995004@qq.com> Date: Wed, 10 Apr 2024 17:21:03 +0800 Subject: [PATCH 17/17] feat: support no input parameters Signed-off-by: zxypro1 <1018995004@qq.com> --- packages/load-application/package.json | 2 +- packages/load-application/src/utils/index.ts | 4 ++-- packages/load-application/src/v3.ts | 11 ++++++++--- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/packages/load-application/package.json b/packages/load-application/package.json index 4bef8f9..fc93a5c 100644 --- a/packages/load-application/package.json +++ b/packages/load-application/package.json @@ -1,6 +1,6 @@ { "name": "@serverless-devs/load-application", - "version": "0.0.13-beta.5", + "version": "0.0.13-beta.6", "description": "load application for serverless-devs", "main": "lib/index.js", "scripts": { diff --git a/packages/load-application/src/utils/index.ts b/packages/load-application/src/utils/index.ts index 7ab83c2..4e540db 100644 --- a/packages/load-application/src/utils/index.ts +++ b/packages/load-application/src/utils/index.ts @@ -1,4 +1,4 @@ -import { endsWith, keys, replace, split } from 'lodash'; +import { keys, replace, split } from 'lodash'; import { RANDOM_PATTERN, REGISTRY, GITHUB_REGISTRY } from '../constant'; import Credential from '@serverless-devs/credential'; @@ -30,5 +30,5 @@ export const getAllCredential = async ({ logger }: any) => { export const getDefaultValue = (value: any) => { if (typeof value !== 'string') return; - return endsWith(value, RANDOM_PATTERN) ? replace(value, RANDOM_PATTERN, randomId()) : value; + return replace(value, RANDOM_PATTERN, randomId()); }; diff --git a/packages/load-application/src/v3.ts b/packages/load-application/src/v3.ts index e077cc0..434eb9a 100644 --- a/packages/load-application/src/v3.ts +++ b/packages/load-application/src/v3.ts @@ -4,7 +4,7 @@ import download from '@serverless-devs/downloads'; import _artTemplate from 'art-template'; import _devsArtTemplate from '@serverless-devs/art-template'; import { getYamlContent, registry, isCiCdEnvironment, getYamlPath } from '@serverless-devs/utils'; -import { isEmpty, includes, split, get, has, set, sortBy, map, concat, keys, startsWith } from 'lodash'; +import { isEmpty, includes, split, get, has, set, sortBy, map, concat, keys, startsWith, merge } from 'lodash'; import axios from 'axios'; import parse from './parse'; import { IOptions } from './types'; @@ -212,6 +212,7 @@ class LoadApplication { const properties = get(publishData, 'Parameters.properties'); const requiredList = get(publishData, 'Parameters.required'); const promptList = []; + const tmpResult: any = {}; if (properties) { let rangeList = []; for (const key in properties) { @@ -232,8 +233,11 @@ class LoadApplication { } return true; }; - // 布尔类型 - if (item.type === 'boolean') { + if (item.input === 'false' || item.input === false) { + // 不手动输入 + tmpResult[name] = getDefaultValue(item.default) || ''; + } else if (item.type === 'boolean') { + // 布尔类型 promptList.push({ type: 'confirm', name, @@ -313,6 +317,7 @@ class LoadApplication { result.access = DEFAULT_MAGIC_ACCESS; } } + result = merge(tmpResult, result); return result; } private async getCredentialDirectly() {