Skip to content

Commit afb65d2

Browse files
authored
Merge pull request #14 from lowcoding/v1.7.3
V1.7.3
2 parents 0493f78 + d90b723 commit afb65d2

File tree

5 files changed

+110
-54
lines changed

5 files changed

+110
-54
lines changed

package.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"displayName": "lowcode",
44
"description": "lowcode tool, support ChatGPT",
55
"author": "wjkang <[email protected]>",
6-
"version": "1.7.2",
6+
"version": "1.7.3",
77
"icon": "asset/icon.png",
88
"publisher": "wjkang",
99
"repository": "https://github.com/lowcoding/lowcode-vscode",
@@ -98,6 +98,10 @@
9898
{
9999
"command": "lowcode.runSnippetScript",
100100
"title": "Run Snippet Script"
101+
},
102+
{
103+
"command": "lowcode.runSnippetScriptOnExplorer",
104+
"title": "执行脚本"
101105
}
102106
],
103107
"menus": {
@@ -151,11 +155,15 @@
151155
},
152156
{
153157
"command": "lowcode.generateCodeByWebview",
158+
"group": "lowcode@3"
159+
},
160+
{
161+
"command": "lowcode.runSnippetScriptOnExplorer",
154162
"group": "lowcode@2"
155163
},
156164
{
157165
"command": "lowcode.openDownloadMaterials",
158-
"group": "lowcode@3"
166+
"group": "lowcode@4"
159167
}
160168
],
161169
"view/title": [

src/commands/runSnippetScript.ts

Lines changed: 69 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,58 +7,83 @@ import { getInnerLibs } from '../utils/lib';
77
import { getOutputChannel } from '../utils/outputChannel';
88
import { createChatCompletionForScript } from '../utils/openai';
99
import { getClipboardImage } from '../utils/clipboard';
10+
import { formatPath } from '../utils/platform';
1011

1112
const { window } = vscode;
1213

14+
const handleRunSnippetScript = async (explorerSelectedPath?: string) => {
15+
let templateList = getSnippets().filter(
16+
(s) => s.preview.showInRunSnippetScript,
17+
);
18+
if (explorerSelectedPath) {
19+
templateList = getSnippets().filter(
20+
(s) => s.preview.showInRunSnippetScriptOnExplorer,
21+
);
22+
}
23+
if (templateList.length === 0) {
24+
window.showErrorMessage(
25+
`请配置模板(通过 ${
26+
explorerSelectedPath
27+
? 'showInRunSnippetScriptOnExplorer'
28+
: 'showInRunSnippetScript'
29+
} 字段开启)`,
30+
);
31+
return;
32+
}
33+
const templateResult = await window.showQuickPick(
34+
templateList.map((s) => s.name),
35+
{ placeHolder: '请选择模板' },
36+
);
37+
if (!templateResult) {
38+
return;
39+
}
40+
const template = templateList.find((s) => s.name === templateResult);
41+
const scriptFile = path.join(template!.path, 'script/index.js');
42+
if (fs.existsSync(scriptFile)) {
43+
delete eval('require').cache[eval('require').resolve(scriptFile)];
44+
const script = eval('require')(scriptFile);
45+
if (script.onSelect) {
46+
const context = {
47+
vscode,
48+
workspaceRootPath: rootPath,
49+
env: getEnv(),
50+
libs: getInnerLibs(),
51+
outputChannel: getOutputChannel(),
52+
log: getOutputChannel(),
53+
createChatCompletion: createChatCompletionForScript,
54+
materialPath: template!.path,
55+
getClipboardImage,
56+
code: '',
57+
explorerSelectedPath,
58+
};
59+
try {
60+
await script.onSelect(context);
61+
} catch (ex: any) {
62+
window.showErrorMessage(ex.toString());
63+
}
64+
} else {
65+
window.showErrorMessage('脚本中未实现 onSelect 方法');
66+
}
67+
} else {
68+
window.showErrorMessage('当前模板中未添加脚本');
69+
}
70+
};
71+
1372
export const registerRunSnippetScript = (context: vscode.ExtensionContext) => {
1473
context.subscriptions.push(
1574
vscode.commands.registerTextEditorCommand(
1675
'lowcode.runSnippetScript',
1776
async () => {
18-
const templateList = getSnippets().filter(
19-
(s) => s.preview.showInRunSnippetScript,
20-
);
21-
if (templateList.length === 0) {
22-
window.showErrorMessage(
23-
'请配置模板(通过 showInRunSnippetScript 字段开启)',
24-
);
25-
}
26-
const templateResult = await window.showQuickPick(
27-
templateList.map((s) => s.name),
28-
{ placeHolder: '请选择模板' },
29-
);
30-
if (!templateResult) {
31-
return;
32-
}
33-
const template = templateList.find((s) => s.name === templateResult);
34-
const scriptFile = path.join(template!.path, 'script/index.js');
35-
if (fs.existsSync(scriptFile)) {
36-
delete eval('require').cache[eval('require').resolve(scriptFile)];
37-
const script = eval('require')(scriptFile);
38-
if (script.onSelect) {
39-
const context = {
40-
vscode,
41-
workspaceRootPath: rootPath,
42-
env: getEnv(),
43-
libs: getInnerLibs(),
44-
outputChannel: getOutputChannel(),
45-
log: getOutputChannel(),
46-
createChatCompletion: createChatCompletionForScript,
47-
materialPath: template!.path,
48-
getClipboardImage,
49-
code: '',
50-
};
51-
try {
52-
await script.onSelect(context);
53-
} catch (ex: any) {
54-
window.showErrorMessage(ex.toString());
55-
}
56-
} else {
57-
window.showErrorMessage('脚本中未实现 onSelect 方法');
58-
}
59-
} else {
60-
window.showErrorMessage('当前模板中未添加脚本');
61-
}
77+
await handleRunSnippetScript();
78+
},
79+
),
80+
);
81+
context.subscriptions.push(
82+
vscode.commands.registerCommand(
83+
'lowcode.runSnippetScriptOnExplorer',
84+
async (args) => {
85+
const explorerSelectedPath = formatPath(args.path);
86+
await handleRunSnippetScript(explorerSelectedPath);
6287
},
6388
),
6489
);

src/genCode/genCodeByYapi.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ export const genTemplateModelByYapi = async (
9494
const reqBodyScheme = JSON.parse(
9595
stripJsonComments(res.data.data.req_body_other),
9696
);
97+
fixSchema(reqBodyScheme);
9798
delete reqBodyScheme.title;
9899
requestBodyType = await compile(
99100
reqBodyScheme,
@@ -121,6 +122,7 @@ export const genTemplateModelByYapi = async (
121122
// const ts = await jsonToTs(selectInfo.typeName, res.data.data.res_body);
122123
const resBodyJson = JSON.parse(stripJsonComments(res.data.data.res_body));
123124
const schema = GenerateSchema.json(typeName || 'Schema', resBodyJson);
125+
fixSchema(schema);
124126
let ts = await compile(schema, typeName, {
125127
bannerComment: '',
126128
});
@@ -131,6 +133,7 @@ export const genTemplateModelByYapi = async (
131133
const reqBodyScheme = JSON.parse(
132134
stripJsonComments(res.data.data.req_body_other),
133135
);
136+
fixSchema(reqBodyScheme);
134137
delete reqBodyScheme.title;
135138
requestBodyType = await compile(
136139
reqBodyScheme,
@@ -156,18 +159,34 @@ export const genTemplateModelByYapi = async (
156159
return model;
157160
};
158161

159-
const fixSchema = (obj: object) => {
162+
function fixSchema(obj: object, fieldNames: string[] = ['$ref', '$$ref']) {
160163
// eslint-disable-next-line no-restricted-syntax
161164
for (const key in obj) {
162-
// @ts-ignore
163-
if (typeof obj[key] === 'object' && obj[key] !== null) {
164-
// @ts-ignore
165+
if (Array.isArray(obj[key])) {
166+
obj[key].forEach((item: object) => {
167+
if (typeof item === 'object' && item !== null) {
168+
fixSchema(item, fieldNames);
169+
} else {
170+
// eslint-disable-next-line no-restricted-syntax
171+
for (const fieldName of fieldNames) {
172+
if (item && item[fieldName]) {
173+
delete item[fieldName];
174+
}
175+
}
176+
}
177+
});
178+
} else if (typeof obj[key] === 'object' && obj[key] !== null) {
165179
if (obj[key].type === 'object' && !obj[key].properties) {
166-
// @ts-ignore
167180
delete obj[key];
168181
}
169-
// @ts-ignore
170-
fixSchema(obj[key]); // 递归处理
182+
fixSchema(obj[key], fieldNames);
183+
} else {
184+
// eslint-disable-next-line no-restricted-syntax
185+
for (const fieldName of fieldNames) {
186+
if (key === fieldName) {
187+
delete obj[key];
188+
}
189+
}
171190
}
172191
}
173-
};
192+
}

src/utils/materials.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export const getLocalMaterials = (
3232
notShowInSnippetsList?: boolean;
3333
notShowInintellisense?: boolean;
3434
showInRunSnippetScript?: boolean;
35+
showInRunSnippetScriptOnExplorer?: boolean;
3536
schema?: string;
3637
chatGPT?: {
3738
commandPrompt?: string;
@@ -186,6 +187,7 @@ export function getSnippets() {
186187
notShowInSnippetsList?: boolean;
187188
notShowInintellisense?: boolean;
188189
showInRunSnippetScript?: boolean;
190+
showInRunSnippetScriptOnExplorer?: boolean;
189191
schema?: string;
190192
chatGPT?: {
191193
commandPrompt?: string;

tsconfig.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
"target": "ES5",
66
"outDir": "build",
77
"allowSyntheticDefaultImports": true,
8+
"suppressImplicitAnyIndexErrors": true,
89
"lib": [
910
"ES5"
1011
],
1112
"sourceMap": true,
1213
"rootDir": "src",
14+
"ignoreDeprecations": "5.0",
1315
"strict": true /* enable all strict type-checking options */
1416
/* Additional Checks */
1517
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */

0 commit comments

Comments
 (0)