@@ -3,14 +3,20 @@ local Util = require("salesforce.util")
33
44local M = {}
55
6+ local category = " Component generation"
7+
68local executable = Util .get_sf_executable ()
79local prompt_for_name_and_dir = function (name_prompt , directory_prompt )
810 local lwc_name = vim .fn .input (name_prompt )
911 local directory = vim .fn .input (directory_prompt , vim .fn .expand (" %:p:h" ), " dir" )
1012 if vim .fn .isdirectory (directory ) == 1 then
11- print ( " Selected directory: " .. directory )
13+ Debug : log ( " component_generator.lua " , string.format ( " Selected directory: %s " , directory ) )
1214 else
13- error (" invalid directory selected" )
15+ Util .clear_and_notify (
16+ string.format (" Directory does not exist: %s" , directory ),
17+ vim .log .levels .ERROR
18+ )
19+ directory = nil
1420 end
1521 return lwc_name , directory
1622end
@@ -21,71 +27,108 @@ local component_creation_callback = function(job)
2127 sfdx_output = table.concat (sfdx_output )
2228 Debug :log (" component_generator.lua" , " Result from command:" )
2329 Debug :log (" component_generator.lua" , sfdx_output )
30+ local json_ok , sfdx_response = pcall (vim .json .decode , sfdx_output )
31+ if not json_ok or not sfdx_response then
32+ vim .notify (
33+ " Failed to parse the 'lightning generate component' SFDX command output" ,
34+ vim .log .levels .ERROR
35+ )
36+ return
37+ end
2438
25- -- Errors still trigger this callback, but have an emty output.
26- if sfdx_output ~= nil and sfdx_output ~= " " then
27- vim .notify (sfdx_output , vim .log .levels .INFO )
39+ if sfdx_response .status == 0 then
40+ Util .clear_and_notify (
41+ string.format (" Successfully created in dir: %s" , sfdx_response .result .outputDir )
42+ )
43+ else
44+ Util .clear_and_notify (
45+ string.format (" Failed to create component: %s" , vim .inspect (sfdx_response )),
46+ vim .log .levels .ERROR
47+ )
2848 end
2949 end )
3050end
3151
3252M .create_lightning_component = function ()
33- local name , dir = prompt_for_name_and_dir (" Component Name: " , " Please select a directory" )
53+ local name , dir =
54+ prompt_for_name_and_dir (" Lightning Component name: " , " Please select a directory: " )
55+
56+ if not dir then
57+ return
58+ end
59+
60+ Util .clear_prompt ()
3461
3562 vim .ui .select (
3663 { " Aura" , " Lightning Web Component" },
3764 {
3865 prompt = " Lightning Component Type: " ,
3966 },
4067 vim .schedule_wrap (function (choice )
68+ if not choice then
69+ return
70+ end
4171 local type
4272 if choice == " Aura" then
4373 type = " aura"
4474 else
4575 type = " lwc"
4676 end
4777 local command = string.format (
48- " %s lightning generate component --name %s --type %s --output-dir %s" ,
78+ " %s lightning generate component --name %s --type %s --output-dir %s --json " ,
4979 executable ,
5080 name ,
5181 type ,
5282 dir
5383 )
84+ Util .clear_and_notify (" Creating component..." )
5485 Util .send_cli_command (
5586 command ,
5687 component_creation_callback ,
57- " generate component " ,
88+ category ,
5889 " component_generator.lua"
5990 )
6091 end )
6192 )
6293end
6394
6495M .create_apex = function ()
65- local name , dir = prompt_for_name_and_dir (" Apex Name: " , " Please select a directory: " )
96+ local name , dir =
97+ prompt_for_name_and_dir (" Trigger or Class name: " , " Please select a directory: " )
98+
99+ if not dir then
100+ return
101+ end
102+
103+ Util .clear_prompt ()
104+
66105 vim .ui .select (
67106 { " Class" , " Trigger" },
68107 {
69108 prompt = " Apex file type: " ,
70109 },
71110 vim .schedule_wrap (function (choice )
111+ if not choice then
112+ return
113+ end
72114 local type
73115 if choice == " Class" then
74116 type = " class"
75117 else
76118 type = " trigger"
77119 end
78120 local command = string.format (
79- " %s apex generate %s --name %s --output-dir %s" ,
121+ " %s apex generate %s --name %s --output-dir %s --json " ,
80122 executable ,
81123 type ,
82124 name ,
83125 dir
84126 )
127+ Util .clear_and_notify (" Creating component..." )
85128 Util .send_cli_command (
86129 command ,
87130 component_creation_callback ,
88- " generate component " ,
131+ category ,
89132 " component_generator.lua"
90133 )
91134 end )
0 commit comments