Skip to content

Commit aeb6e33

Browse files
committed
created component_generator.lua to allow users to generate Apex classes and lightning components
1 parent 4c688ad commit aeb6e33

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
local Debug = require("salesforce.debug")
2+
local Util = require("salesforce.util")
3+
4+
local M = {}
5+
6+
local executable = Util.get_sf_executable()
7+
local prompt_for_name_and_dir = function(name_prompt, directory_prompt)
8+
local lwc_name = vim.fn.input(name_prompt)
9+
local directory = vim.fn.input(directory_prompt, vim.fn.expand("%:p:h"), "dir")
10+
if vim.fn.isdirectory(directory) == 1 then
11+
print("Selected directory: " .. directory)
12+
else
13+
error("invalid directory selected")
14+
end
15+
return lwc_name, directory
16+
end
17+
18+
local component_creation_callback = function(job)
19+
vim.schedule(function()
20+
local sfdx_output = job:result()
21+
sfdx_output = table.concat(sfdx_output)
22+
Debug:log("component_generator.lua", "Result from command:")
23+
Debug:log("component_generator.lua", sfdx_output)
24+
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)
28+
end
29+
end)
30+
end
31+
32+
M.create_lightning_component = function()
33+
local name, dir = prompt_for_name_and_dir("Component Name: ", "Please select a directory")
34+
35+
vim.ui.select(
36+
{ "Aura", "Lightning Web Component" },
37+
{
38+
prompt = "Lightning Component Type: ",
39+
},
40+
vim.schedule_wrap(function(choice)
41+
local type
42+
if choice == "Aura" then
43+
type = "aura"
44+
else
45+
type = "lwc"
46+
end
47+
local command = string.format(
48+
"%s lightning generate component --name %s --type %s --output-dir %s",
49+
executable,
50+
name,
51+
type,
52+
dir
53+
)
54+
Util.send_cli_command(
55+
command,
56+
component_creation_callback,
57+
"generate component",
58+
"component_generator.lua"
59+
)
60+
end)
61+
)
62+
end
63+
64+
M.create_apex = function()
65+
local name, dir = prompt_for_name_and_dir("Apex Name: ", "Please select a directory: ")
66+
vim.ui.select(
67+
{ "class", "trigger" },
68+
{
69+
prompt = "Apex file type: ",
70+
},
71+
vim.schedule_wrap(function(choice)
72+
local command = string.format(
73+
"%s apex generate %s --name %s --output-dir %s",
74+
executable,
75+
choice,
76+
name,
77+
dir
78+
)
79+
Util.send_cli_command(
80+
command,
81+
component_creation_callback,
82+
"generate component",
83+
"component_generator.lua"
84+
)
85+
end)
86+
)
87+
end
88+
89+
return M

lua/salesforce/util.lua

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local Debug = require("salesforce.debug")
22

3+
local Job = require("plenary.job")
34
local M = {}
45
local namespace = vim.api.nvim_create_namespace("salesforce.util")
56

@@ -122,6 +123,34 @@ function M.salesforce_cli_available()
122123
return false
123124
end
124125

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)
132+
local new_job = Job:new({
133+
command = M.get_sf_executable(),
134+
env = M.get_env(),
135+
args = args,
136+
on_exit = on_exit_callback,
137+
on_stderr = function(_, data)
138+
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)
142+
end)
143+
end,
144+
})
145+
if not M.current_job or not M.current_job:is_running() then
146+
M.current_job = new_job
147+
M.current_bufnr = vim.api.nvim_get_current_buf()
148+
M.current_job:start()
149+
else
150+
M.notify_command_in_progress(category)
151+
end
152+
end
153+
125154
local function get_apex_ls_namespace()
126155
local diagnostic_namespaces = vim.diagnostic.get_namespaces()
127156
for id, ns in pairs(diagnostic_namespaces) do

0 commit comments

Comments
 (0)