Skip to content

Commit ce0f006

Browse files
Merge pull request #47 from Ztrizzo/component-generator
Component generator
2 parents 4c688ad + 75d0c1d commit ce0f006

File tree

5 files changed

+190
-0
lines changed

5 files changed

+190
-0
lines changed

doc/salesforce.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ extension for VS Code. Out of the box commands include:
2323
- `SalesforceRetrieveFromOrg`: Pull the current file from the org
2424
- `SalesforceDiffFile`: Diff the current file against the file in the org
2525
- `SalesforceSetDefaultOrg`: Set the default org for the current project
26+
- `SalesforceCreateLightningComponent`: Create an Aura Component or LWC
27+
- `SalesforceCreateApex`: Create Apex trigger or class
2628

2729
# Setup ~
2830
To use this plugin, you must first setup your project by running `require("salesforce").setup({})`.
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
local Debug = require("salesforce.debug")
2+
local Util = require("salesforce.util")
3+
4+
local M = {}
5+
6+
local category = "Component generation"
7+
8+
local prompt_for_name_and_dir = function(name_prompt, directory_prompt)
9+
local lwc_name = vim.fn.input(name_prompt)
10+
local directory = vim.fn.input(directory_prompt, vim.fn.expand("%:p:h"), "dir")
11+
if vim.fn.isdirectory(directory) == 1 then
12+
Debug:log("component_generator.lua", string.format("Selected directory: %s", directory))
13+
else
14+
Util.clear_and_notify(
15+
string.format("Directory does not exist: %s", directory),
16+
vim.log.levels.ERROR
17+
)
18+
directory = nil
19+
end
20+
return lwc_name, directory
21+
end
22+
23+
local component_creation_callback = function(job)
24+
vim.schedule(function()
25+
local sfdx_output = job:result()
26+
sfdx_output = table.concat(sfdx_output)
27+
Debug:log("component_generator.lua", "Result from command:")
28+
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
37+
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+
)
50+
end
51+
end)
52+
end
53+
54+
M.create_lightning_component = function()
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()
63+
64+
vim.ui.select(
65+
{ "Aura", "Lightning Web Component" },
66+
{
67+
prompt = "Lightning Component Type: ",
68+
},
69+
vim.schedule_wrap(function(choice)
70+
if not choice then
71+
return
72+
end
73+
local type
74+
if choice == "Aura" then
75+
type = "aura"
76+
else
77+
type = "lwc"
78+
end
79+
local args = {
80+
"lightning",
81+
"generate",
82+
"component",
83+
"--name",
84+
name,
85+
"--type",
86+
type,
87+
"--output-dir",
88+
dir,
89+
"--json",
90+
}
91+
Util.clear_and_notify("Creating component...")
92+
Util.send_cli_command(
93+
args,
94+
component_creation_callback,
95+
category,
96+
"component_generator.lua"
97+
)
98+
end)
99+
)
100+
end
101+
102+
M.create_apex = function()
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+
112+
vim.ui.select(
113+
{ "Class", "Trigger" },
114+
{
115+
prompt = "Apex file type: ",
116+
},
117+
vim.schedule_wrap(function(choice)
118+
if not choice then
119+
return
120+
end
121+
local type
122+
if choice == "Class" then
123+
type = "class"
124+
else
125+
type = "trigger"
126+
end
127+
local args = {
128+
"apex",
129+
"generate",
130+
type,
131+
"--name",
132+
name,
133+
"--output-dir",
134+
dir,
135+
"--json",
136+
}
137+
Util.clear_and_notify("Creating component...")
138+
Util.send_cli_command(
139+
args,
140+
component_creation_callback,
141+
category,
142+
"component_generator.lua"
143+
)
144+
end)
145+
)
146+
end
147+
148+
return M

lua/salesforce/init.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
--- - `SalesforceRetrieveFromOrg`: Pull the current file from the org
2020
--- - `SalesforceDiffFile`: Diff the current file against the file in the org
2121
--- - `SalesforceSetDefaultOrg`: Set the default org for the current project
22+
--- - `SalesforceCreateLightningComponent`: Create an Aura Component or LWC
23+
--- - `SalesforceCreateApex`: Create Apex trigger or class
2224
---
2325
--- # Setup ~
2426
--- To use this plugin, you must first setup your project by running `require("salesforce").setup({})`.

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

@@ -38,6 +39,10 @@ function M.notify_default_org_not_set()
3839
vim.notify(message, vim.log.levels.ERROR)
3940
end
4041

42+
function M.clear_prompt()
43+
vim.fn.feedkeys(":", "nx")
44+
end
45+
4146
function M.clear_and_notify(msg, log_level)
4247
vim.fn.feedkeys(":", "nx")
4348
vim.notify(msg, log_level)
@@ -122,6 +127,30 @@ function M.salesforce_cli_available()
122127
return false
123128
end
124129

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)
133+
local new_job = Job:new({
134+
command = M.get_sf_executable(),
135+
env = M.get_env(),
136+
args = args,
137+
on_exit = on_exit_callback,
138+
on_stderr = function(_, data)
139+
vim.schedule(function()
140+
Debug:log(caller_name, "Command stderr is: %s", data)
141+
end)
142+
end,
143+
})
144+
if not M[category] or not M[category].current_job:is_running() then
145+
M[category] = {}
146+
M[category].current_job = new_job
147+
M[category].current_bufnr = vim.api.nvim_get_current_buf()
148+
M[category].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

plugin/salesforce.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ local Diff = require("salesforce.diff")
1313
local Config = require("salesforce.config")
1414
local OrgManager = require("salesforce.org_manager")
1515
local Debug = require("salesforce.debug")
16+
local ComponentGenerator = require("salesforce.component_generator")
1617

1718
Debug:log("salesforce.lua", "Initializing Salesforce plugin commands...")
1819

@@ -68,3 +69,11 @@ end, {})
6869
vim.api.nvim_create_user_command("SalesforceSetDefaultOrg", function()
6970
OrgManager:set_default_org()
7071
end, {})
72+
73+
vim.api.nvim_create_user_command("SalesforceCreateLightningComponent", function()
74+
ComponentGenerator:create_lightning_component()
75+
end, {})
76+
77+
vim.api.nvim_create_user_command("SalesforceCreateApex", function()
78+
ComponentGenerator:create_apex()
79+
end, {})

0 commit comments

Comments
 (0)