From b6df1aecea06715d69d33eca62d2a9ff6dfd7308 Mon Sep 17 00:00:00 2001 From: wjkang Date: Thu, 14 Dec 2023 18:47:31 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=A8=20feat:=20=E8=B5=84=E6=BA=90?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E5=99=A8=E6=96=87=E4=BB=B6=E5=A4=B9=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E2=80=9C=E6=89=A7=E8=A1=8C=E8=84=9A=E6=9C=AC=E2=80=9D?= =?UTF-8?q?=E8=8F=9C=E5=8D=95=EF=BC=8C=E6=94=AF=E6=8C=81=E8=B0=83=E7=94=A8?= =?UTF-8?q?=E5=A4=96=E9=83=A8=E8=84=9A=E6=9C=AC=EF=BC=8C=E5=9C=A8=E4=B8=8A?= =?UTF-8?q?=E4=B8=8B=E6=96=87=E4=B8=AD=E9=80=9A=E8=BF=87=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=20explorerSelectedPath=20=E4=BC=A0=E9=80=92=E9=80=89=E6=8B=A9?= =?UTF-8?q?=E7=9A=84=E6=96=87=E4=BB=B6=E5=A4=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 10 ++- src/commands/runSnippetScript.ts | 113 +++++++++++++++++++------------ src/utils/materials.ts | 2 + 3 files changed, 80 insertions(+), 45 deletions(-) diff --git a/package.json b/package.json index ce01fc9..214b131 100644 --- a/package.json +++ b/package.json @@ -98,6 +98,10 @@ { "command": "lowcode.runSnippetScript", "title": "Run Snippet Script" + }, + { + "command": "lowcode.runSnippetScriptOnExplorer", + "title": "执行脚本" } ], "menus": { @@ -151,11 +155,15 @@ }, { "command": "lowcode.generateCodeByWebview", + "group": "lowcode@3" + }, + { + "command": "lowcode.runSnippetScriptOnExplorer", "group": "lowcode@2" }, { "command": "lowcode.openDownloadMaterials", - "group": "lowcode@3" + "group": "lowcode@4" } ], "view/title": [ diff --git a/src/commands/runSnippetScript.ts b/src/commands/runSnippetScript.ts index dea8d84..0bc05fb 100644 --- a/src/commands/runSnippetScript.ts +++ b/src/commands/runSnippetScript.ts @@ -7,58 +7,83 @@ import { getInnerLibs } from '../utils/lib'; import { getOutputChannel } from '../utils/outputChannel'; import { createChatCompletionForScript } from '../utils/openai'; import { getClipboardImage } from '../utils/clipboard'; +import { formatPath } from '../utils/platform'; const { window } = vscode; +const handleRunSnippetScript = async (explorerSelectedPath?: string) => { + let templateList = getSnippets().filter( + (s) => s.preview.showInRunSnippetScript, + ); + if (explorerSelectedPath) { + templateList = getSnippets().filter( + (s) => s.preview.showInRunSnippetScriptOnExplorer, + ); + } + if (templateList.length === 0) { + window.showErrorMessage( + `请配置模板(通过 ${ + explorerSelectedPath + ? 'showInRunSnippetScriptOnExplorer' + : 'showInRunSnippetScript' + } 字段开启)`, + ); + return; + } + const templateResult = await window.showQuickPick( + templateList.map((s) => s.name), + { placeHolder: '请选择模板' }, + ); + if (!templateResult) { + return; + } + const template = templateList.find((s) => s.name === templateResult); + const scriptFile = path.join(template!.path, 'script/index.js'); + if (fs.existsSync(scriptFile)) { + delete eval('require').cache[eval('require').resolve(scriptFile)]; + const script = eval('require')(scriptFile); + if (script.onSelect) { + const context = { + vscode, + workspaceRootPath: rootPath, + env: getEnv(), + libs: getInnerLibs(), + outputChannel: getOutputChannel(), + log: getOutputChannel(), + createChatCompletion: createChatCompletionForScript, + materialPath: template!.path, + getClipboardImage, + code: '', + explorerSelectedPath, + }; + try { + await script.onSelect(context); + } catch (ex: any) { + window.showErrorMessage(ex.toString()); + } + } else { + window.showErrorMessage('脚本中未实现 onSelect 方法'); + } + } else { + window.showErrorMessage('当前模板中未添加脚本'); + } +}; + export const registerRunSnippetScript = (context: vscode.ExtensionContext) => { context.subscriptions.push( vscode.commands.registerTextEditorCommand( 'lowcode.runSnippetScript', async () => { - const templateList = getSnippets().filter( - (s) => s.preview.showInRunSnippetScript, - ); - if (templateList.length === 0) { - window.showErrorMessage( - '请配置模板(通过 showInRunSnippetScript 字段开启)', - ); - } - const templateResult = await window.showQuickPick( - templateList.map((s) => s.name), - { placeHolder: '请选择模板' }, - ); - if (!templateResult) { - return; - } - const template = templateList.find((s) => s.name === templateResult); - const scriptFile = path.join(template!.path, 'script/index.js'); - if (fs.existsSync(scriptFile)) { - delete eval('require').cache[eval('require').resolve(scriptFile)]; - const script = eval('require')(scriptFile); - if (script.onSelect) { - const context = { - vscode, - workspaceRootPath: rootPath, - env: getEnv(), - libs: getInnerLibs(), - outputChannel: getOutputChannel(), - log: getOutputChannel(), - createChatCompletion: createChatCompletionForScript, - materialPath: template!.path, - getClipboardImage, - code: '', - }; - try { - await script.onSelect(context); - } catch (ex: any) { - window.showErrorMessage(ex.toString()); - } - } else { - window.showErrorMessage('脚本中未实现 onSelect 方法'); - } - } else { - window.showErrorMessage('当前模板中未添加脚本'); - } + await handleRunSnippetScript(); + }, + ), + ); + context.subscriptions.push( + vscode.commands.registerCommand( + 'lowcode.runSnippetScriptOnExplorer', + async (args) => { + const explorerSelectedPath = formatPath(args.path); + await handleRunSnippetScript(explorerSelectedPath); }, ), ); diff --git a/src/utils/materials.ts b/src/utils/materials.ts index 0347bb8..37b5530 100644 --- a/src/utils/materials.ts +++ b/src/utils/materials.ts @@ -32,6 +32,7 @@ export const getLocalMaterials = ( notShowInSnippetsList?: boolean; notShowInintellisense?: boolean; showInRunSnippetScript?: boolean; + showInRunSnippetScriptOnExplorer?: boolean; schema?: string; chatGPT?: { commandPrompt?: string; @@ -186,6 +187,7 @@ export function getSnippets() { notShowInSnippetsList?: boolean; notShowInintellisense?: boolean; showInRunSnippetScript?: boolean; + showInRunSnippetScriptOnExplorer?: boolean; schema?: string; chatGPT?: { commandPrompt?: string; From d90b7237702bb80e4610ff79e24a0416f0cdc01c Mon Sep 17 00:00:00 2001 From: wjkang Date: Thu, 14 Dec 2023 22:43:55 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=90=9E=20fix:=20AYPI=20Schema=20?= =?UTF-8?q?=E4=B8=AD=20$$ref=E3=80=81$ref=20=E5=AD=97=E6=AE=B5=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/genCode/genCodeByYapi.ts | 35 +++++++++++++++++++++++++++-------- tsconfig.json | 2 ++ 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 214b131..3e31a28 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "displayName": "lowcode", "description": "lowcode tool, support ChatGPT", "author": "wjkang ", - "version": "1.7.2", + "version": "1.7.3", "icon": "asset/icon.png", "publisher": "wjkang", "repository": "https://github.com/lowcoding/lowcode-vscode", diff --git a/src/genCode/genCodeByYapi.ts b/src/genCode/genCodeByYapi.ts index ccfce08..354d9ae 100644 --- a/src/genCode/genCodeByYapi.ts +++ b/src/genCode/genCodeByYapi.ts @@ -94,6 +94,7 @@ export const genTemplateModelByYapi = async ( const reqBodyScheme = JSON.parse( stripJsonComments(res.data.data.req_body_other), ); + fixSchema(reqBodyScheme); delete reqBodyScheme.title; requestBodyType = await compile( reqBodyScheme, @@ -121,6 +122,7 @@ export const genTemplateModelByYapi = async ( // const ts = await jsonToTs(selectInfo.typeName, res.data.data.res_body); const resBodyJson = JSON.parse(stripJsonComments(res.data.data.res_body)); const schema = GenerateSchema.json(typeName || 'Schema', resBodyJson); + fixSchema(schema); let ts = await compile(schema, typeName, { bannerComment: '', }); @@ -131,6 +133,7 @@ export const genTemplateModelByYapi = async ( const reqBodyScheme = JSON.parse( stripJsonComments(res.data.data.req_body_other), ); + fixSchema(reqBodyScheme); delete reqBodyScheme.title; requestBodyType = await compile( reqBodyScheme, @@ -156,18 +159,34 @@ export const genTemplateModelByYapi = async ( return model; }; -const fixSchema = (obj: object) => { +function fixSchema(obj: object, fieldNames: string[] = ['$ref', '$$ref']) { // eslint-disable-next-line no-restricted-syntax for (const key in obj) { - // @ts-ignore - if (typeof obj[key] === 'object' && obj[key] !== null) { - // @ts-ignore + if (Array.isArray(obj[key])) { + obj[key].forEach((item: object) => { + if (typeof item === 'object' && item !== null) { + fixSchema(item, fieldNames); + } else { + // eslint-disable-next-line no-restricted-syntax + for (const fieldName of fieldNames) { + if (item && item[fieldName]) { + delete item[fieldName]; + } + } + } + }); + } else if (typeof obj[key] === 'object' && obj[key] !== null) { if (obj[key].type === 'object' && !obj[key].properties) { - // @ts-ignore delete obj[key]; } - // @ts-ignore - fixSchema(obj[key]); // 递归处理 + fixSchema(obj[key], fieldNames); + } else { + // eslint-disable-next-line no-restricted-syntax + for (const fieldName of fieldNames) { + if (key === fieldName) { + delete obj[key]; + } + } } } -}; +} diff --git a/tsconfig.json b/tsconfig.json index 712c9c6..e82eb61 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,11 +5,13 @@ "target": "ES5", "outDir": "build", "allowSyntheticDefaultImports": true, + "suppressImplicitAnyIndexErrors": true, "lib": [ "ES5" ], "sourceMap": true, "rootDir": "src", + "ignoreDeprecations": "5.0", "strict": true /* enable all strict type-checking options */ /* Additional Checks */ // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */