|
| 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 |
0 commit comments