@@ -3,14 +3,19 @@ local Util = require("salesforce.util")
33
44local M = {}
55
6- local executable = Util .get_sf_executable ()
6+ local category = " Component generation"
7+
78local prompt_for_name_and_dir = function (name_prompt , directory_prompt )
89 local lwc_name = vim .fn .input (name_prompt )
910 local directory = vim .fn .input (directory_prompt , vim .fn .expand (" %:p:h" ), " dir" )
1011 if vim .fn .isdirectory (directory ) == 1 then
11- print ( " Selected directory: " .. directory )
12+ Debug : log ( " component_generator.lua " , string.format ( " Selected directory: %s " , directory ) )
1213 else
13- error (" invalid directory selected" )
14+ Util .clear_and_notify (
15+ string.format (" Directory does not exist: %s" , directory ),
16+ vim .log .levels .ERROR
17+ )
18+ directory = nil
1419 end
1520 return lwc_name , directory
1621end
@@ -21,71 +26,119 @@ local component_creation_callback = function(job)
2126 sfdx_output = table.concat (sfdx_output )
2227 Debug :log (" component_generator.lua" , " Result from command:" )
2328 Debug :log (" component_generator.lua" , sfdx_output )
29+ local json_ok , sfdx_response = pcall (vim .json .decode , sfdx_output )
30+ if not json_ok or not sfdx_response then
31+ vim .notify (
32+ " Failed to parse the 'lightning generate component' SFDX command output" ,
33+ vim .log .levels .ERROR
34+ )
35+ return
36+ end
2437
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 )
38+ if sfdx_response .status == 0 then
39+ Util .clear_and_notify (
40+ string.format (
41+ " Successfully created component in directory: %s" ,
42+ sfdx_response .result .outputDir
43+ )
44+ )
45+ else
46+ Util .clear_and_notify (
47+ string.format (" Failed to create component: %s" , vim .inspect (sfdx_response )),
48+ vim .log .levels .ERROR
49+ )
2850 end
2951 end )
3052end
3153
3254M .create_lightning_component = function ()
33- local name , dir = prompt_for_name_and_dir (" Component Name: " , " Please select a directory" )
55+ local name , dir =
56+ prompt_for_name_and_dir (" Lightning Component name: " , " Please select a directory: " )
57+
58+ if not dir then
59+ return
60+ end
61+
62+ Util .clear_prompt ()
3463
3564 vim .ui .select (
3665 { " Aura" , " Lightning Web Component" },
3766 {
3867 prompt = " Lightning Component Type: " ,
3968 },
4069 vim .schedule_wrap (function (choice )
70+ if not choice then
71+ return
72+ end
4173 local type
4274 if choice == " Aura" then
4375 type = " aura"
4476 else
4577 type = " lwc"
4678 end
47- local command = string.format (
48- " %s lightning generate component --name %s --type %s --output-dir %s" ,
49- executable ,
79+ local args = {
80+ " lightning" ,
81+ " generate" ,
82+ " component" ,
83+ " --name" ,
5084 name ,
85+ " --type" ,
5186 type ,
52- dir
53- )
87+ " --output-dir" ,
88+ dir ,
89+ " --json" ,
90+ }
91+ Util .clear_and_notify (" Creating component..." )
5492 Util .send_cli_command (
55- command ,
93+ args ,
5694 component_creation_callback ,
57- " generate component " ,
95+ category ,
5896 " component_generator.lua"
5997 )
6098 end )
6199 )
62100end
63101
64102M .create_apex = function ()
65- local name , dir = prompt_for_name_and_dir (" Apex Name: " , " Please select a directory: " )
103+ local name , dir =
104+ prompt_for_name_and_dir (" Trigger or Class name: " , " Please select a directory: " )
105+
106+ if not dir then
107+ return
108+ end
109+
110+ Util .clear_prompt ()
111+
66112 vim .ui .select (
67113 { " Class" , " Trigger" },
68114 {
69115 prompt = " Apex file type: " ,
70116 },
71117 vim .schedule_wrap (function (choice )
118+ if not choice then
119+ return
120+ end
72121 local type
73122 if choice == " Class" then
74123 type = " class"
75124 else
76125 type = " trigger"
77126 end
78- local command = string.format (
79- " %s apex generate %s --name %s --output-dir %s " ,
80- executable ,
127+ local args = {
128+ " apex" ,
129+ " generate " ,
81130 type ,
131+ " --name" ,
82132 name ,
83- dir
84- )
133+ " --output-dir" ,
134+ dir ,
135+ " --json" ,
136+ }
137+ Util .clear_and_notify (" Creating component..." )
85138 Util .send_cli_command (
86- command ,
139+ args ,
87140 component_creation_callback ,
88- " generate component " ,
141+ category ,
89142 " component_generator.lua"
90143 )
91144 end )
0 commit comments