Skip to content

Commit 75d0c1d

Browse files
authored
Merge pull request #6 from jonathanmorris180/jm/component-generator
Update component-generator
2 parents 0123d55 + dd8d7f7 commit 75d0c1d

File tree

2 files changed

+84
-32
lines changed

2 files changed

+84
-32
lines changed

lua/salesforce/component_generator.lua

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

44
local M = {}
55

6-
local executable = Util.get_sf_executable()
6+
local category = "Component generation"
7+
78
local 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
1621
end
@@ -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)
3052
end
3153

3254
M.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
)
62100
end
63101

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

lua/salesforce/util.lua

Lines changed: 9 additions & 10 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)
@@ -123,26 +127,21 @@ function M.salesforce_cli_available()
123127
return false
124128
end
125129

126-
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)
130-
local args = M.split(command, " ")
131-
table.remove(args, 1)
130+
M.send_cli_command = function(args, on_exit_callback, category, caller_name)
131+
local command = table.concat(args, " ")
132+
Debug:log(caller_name, "Executing command %s", command)
132133
local new_job = Job:new({
133134
command = M.get_sf_executable(),
134135
env = M.get_env(),
135136
args = args,
136137
on_exit = on_exit_callback,
137138
on_stderr = function(_, data)
138139
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)
140+
Debug:log(caller_name, "Command stderr is: %s", data)
142141
end)
143142
end,
144143
})
145-
if not M[category] or M[category].current_job or not M[category].current_job:is_running() then
144+
if not M[category] or not M[category].current_job:is_running() then
146145
M[category] = {}
147146
M[category].current_job = new_job
148147
M[category].current_bufnr = vim.api.nvim_get_current_buf()

0 commit comments

Comments
 (0)