Skip to content

Commit 797c39e

Browse files
committed
✨ feat: add runSnippetScript command
1 parent 1602b6a commit 797c39e

File tree

6 files changed

+76
-31
lines changed

6 files changed

+76
-31
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,6 @@ module.exports = {
3737
'no-unused-expressions': 'off',
3838
'max-len': 'off',
3939
'class-methods-use-this': 'off',
40+
'no-eval': 'off',
4041
},
4142
};

package.json

Lines changed: 9 additions & 1 deletion
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.5.0",
6+
"version": "1.6.0",
77
"icon": "asset/icon.png",
88
"publisher": "wjkang",
99
"repository": "https://github.com/lowcoding/lowcode-vscode",
@@ -94,6 +94,10 @@
9494
{
9595
"command": "lowcode.askChatGPTWithTemplate",
9696
"title": "Ask ChatGPT With Template"
97+
},
98+
{
99+
"command": "lowcode.runSnippetScript",
100+
"title": "Run Snippet Script"
97101
}
98102
],
99103
"menus": {
@@ -134,6 +138,10 @@
134138
{
135139
"command": "lowcode.askChatGPTWithTemplate",
136140
"group": "lowcode@5"
141+
},
142+
{
143+
"command": "lowcode.runSnippetScript",
144+
"group": "lowcode@6"
137145
}
138146
],
139147
"lowcode/explorer/context": [

src/commands/generateCodeV2.ts

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/commands/runSnippetScript.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import * as vscode from 'vscode';
2+
import * as path from 'path';
3+
import * as fs from 'fs-extra';
4+
import { getSnippets } from '../utils/materials';
5+
import { getEnv, rootPath } from '../utils/vscodeEnv';
6+
import { getInnerLibs } from '../utils/lib';
7+
import { getOutputChannel } from '../utils/outputChannel';
8+
import { createChatCompletionForScript } from '../utils/openai';
9+
10+
const { window } = vscode;
11+
12+
export const registerRunSnippetScript = (context: vscode.ExtensionContext) => {
13+
context.subscriptions.push(
14+
vscode.commands.registerTextEditorCommand(
15+
'lowcode.runSnippetScript',
16+
async () => {
17+
const templateList = getSnippets().filter(
18+
(s) => !s.preview.notShowInCommand,
19+
);
20+
if (templateList.length === 0) {
21+
window.showErrorMessage('请配置模板(代码片段)');
22+
}
23+
const templateResult = await window.showQuickPick(
24+
templateList.map((s) => s.name),
25+
{ placeHolder: '请选择模板' },
26+
);
27+
if (!templateResult) {
28+
return;
29+
}
30+
const template = templateList.find((s) => s.name === templateResult);
31+
const scriptFile = path.join(template!.path, 'script/index.js');
32+
if (fs.existsSync(scriptFile)) {
33+
delete eval('require').cache[eval('require').resolve(scriptFile)];
34+
const script = eval('require')(scriptFile);
35+
if (script.onSelect) {
36+
const context = {
37+
vscode,
38+
workspaceRootPath: rootPath,
39+
env: getEnv(),
40+
libs: getInnerLibs(),
41+
outputChannel: getOutputChannel(),
42+
log: getOutputChannel(),
43+
createChatCompletion: createChatCompletionForScript,
44+
materialPath: template!.path,
45+
code: '',
46+
};
47+
try {
48+
await script.onSelect(context);
49+
} catch (ex: any) {
50+
window.showErrorMessage(ex.toString());
51+
}
52+
} else {
53+
window.showErrorMessage('脚本中未实现 onSelect 方法');
54+
}
55+
} else {
56+
window.showErrorMessage('当前模板中未添加脚本');
57+
}
58+
},
59+
),
60+
);
61+
};

src/extension.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { init, setLastActiveTextEditorId } from './context';
99
import { registerQuickGenerateBlock } from './commands/quickGenerateBlock';
1010
import { registerChatGPTViewProvider } from './webview';
1111
import { registerChatGPTCommand } from './commands/chatGPT';
12+
import { registerRunSnippetScript } from './commands/runSnippetScript';
1213

1314
export function activate(context: vscode.ExtensionContext) {
1415
vscode.window.onDidChangeActiveTextEditor(
@@ -26,6 +27,8 @@ export function activate(context: vscode.ExtensionContext) {
2627

2728
generateCode(context);
2829

30+
registerRunSnippetScript(context);
31+
2932
createOrShowWebview(context);
3033

3134
createOrShowAddSnippetWebview(context);

src/utils/generate.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export const genCodeByBlock = async (data: {
9393
.join(data.path, ...data.createPath)
9494
.replace(/\\/g, '/'),
9595
createChatCompletion: createChatCompletionForScript,
96+
materialPath: block,
9697
};
9798
data.model = {
9899
...data.model,
@@ -196,6 +197,7 @@ export const genCodeBySnippet = async (data: {
196197
outputChannel: getOutputChannel(),
197198
log: getOutputChannel(),
198199
createChatCompletion: createChatCompletionForScript,
200+
materialPath: snippetPath,
199201
code: '',
200202
};
201203
const extendModel = await hook.beforeCompile(context);

0 commit comments

Comments
 (0)