@@ -7,58 +7,83 @@ import { getInnerLibs } from '../utils/lib';
77import { getOutputChannel } from '../utils/outputChannel' ;
88import { createChatCompletionForScript } from '../utils/openai' ;
99import { getClipboardImage } from '../utils/clipboard' ;
10+ import { formatPath } from '../utils/platform' ;
1011
1112const { 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+
1372export 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 ) ;
0 commit comments