Skip to content

Commit

Permalink
Merge pull request #14 from lowcoding/v1.7.3
Browse files Browse the repository at this point in the history
V1.7.3
  • Loading branch information
wjkang authored Dec 14, 2023
2 parents 0493f78 + d90b723 commit afb65d2
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 54 deletions.
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"displayName": "lowcode",
"description": "lowcode tool, support ChatGPT",
"author": "wjkang <ruoxieme@gmail.com>",
"version": "1.7.2",
"version": "1.7.3",
"icon": "asset/icon.png",
"publisher": "wjkang",
"repository": "https://github.com/lowcoding/lowcode-vscode",
Expand Down Expand Up @@ -98,6 +98,10 @@
{
"command": "lowcode.runSnippetScript",
"title": "Run Snippet Script"
},
{
"command": "lowcode.runSnippetScriptOnExplorer",
"title": "执行脚本"
}
],
"menus": {
Expand Down Expand Up @@ -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": [
Expand Down
113 changes: 69 additions & 44 deletions src/commands/runSnippetScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
),
);
Expand Down
35 changes: 27 additions & 8 deletions src/genCode/genCodeByYapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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: '',
});
Expand All @@ -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,
Expand All @@ -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];
}
}
}
}
};
}
2 changes: 2 additions & 0 deletions src/utils/materials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const getLocalMaterials = (
notShowInSnippetsList?: boolean;
notShowInintellisense?: boolean;
showInRunSnippetScript?: boolean;
showInRunSnippetScriptOnExplorer?: boolean;
schema?: string;
chatGPT?: {
commandPrompt?: string;
Expand Down Expand Up @@ -186,6 +187,7 @@ export function getSnippets() {
notShowInSnippetsList?: boolean;
notShowInintellisense?: boolean;
showInRunSnippetScript?: boolean;
showInRunSnippetScriptOnExplorer?: boolean;
schema?: string;
chatGPT?: {
commandPrompt?: string;
Expand Down
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down

0 comments on commit afb65d2

Please sign in to comment.