Skip to content

Commit 880c0c4

Browse files
feat: minor alterations for component generator
1 parent 0123d55 commit 880c0c4

File tree

2 files changed

+61
-18
lines changed

2 files changed

+61
-18
lines changed

lua/salesforce/component_generator.lua

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@ local Util = require("salesforce.util")
33

44
local M = {}
55

6+
local category = "Component generation"
7+
68
local executable = Util.get_sf_executable()
79
local 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
1622
end
@@ -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)
3050
end
3151

3252
M.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
)
6293
end
6394

6495
M.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)

lua/salesforce/util.lua

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ function M.notify_default_org_not_set()
3939
vim.notify(message, vim.log.levels.ERROR)
4040
end
4141

42+
function M.clear_prompt()
43+
vim.fn.feedkeys(":", "nx")
44+
end
45+
4246
function M.clear_and_notify(msg, log_level)
4347
vim.fn.feedkeys(":", "nx")
4448
vim.notify(msg, log_level)
@@ -124,9 +128,7 @@ function M.salesforce_cli_available()
124128
end
125129

126130
M.send_cli_command = function(command, on_exit_callback, category, caller_name)
127-
vim.schedule(function()
128-
Debug:log(caller_name, "Executing command %s", command)
129-
end)
131+
Debug:log(caller_name, "Executing command %s", command)
130132
local args = M.split(command, " ")
131133
table.remove(args, 1)
132134
local new_job = Job:new({
@@ -136,13 +138,11 @@ M.send_cli_command = function(command, on_exit_callback, category, caller_name)
136138
on_exit = on_exit_callback,
137139
on_stderr = function(_, data)
138140
vim.schedule(function()
139-
Debug:log(caller_name, "command stderr is: %s", data)
140-
Debug:log(string.format("%s", data))
141-
vim.notify(data, vim.log.levels.ERROR)
141+
Debug:log(caller_name, "Command stderr is: %s", data)
142142
end)
143143
end,
144144
})
145-
if not M[category] or M[category].current_job or not M[category].current_job:is_running() then
145+
if not M[category] or not M[category].current_job:is_running() then
146146
M[category] = {}
147147
M[category].current_job = new_job
148148
M[category].current_bufnr = vim.api.nvim_get_current_buf()

0 commit comments

Comments
 (0)