diff --git a/package.json b/package.json index ce01fc9..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", @@ -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/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/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; 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. */