Skip to content

Commit

Permalink
feat: add debugger_cmd config
Browse files Browse the repository at this point in the history
This is to allow providing a command in PATH that launches the debug server,
instead of having to deal with providing absolute paths.
  • Loading branch information
williamboman committed Sep 21, 2022
1 parent 32b0b9f commit e95044e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ npm run compile
```lua
require("dap-vscode-js").setup({
-- node_path = "node", -- Path of node executable. Defaults to $NODE_PATH, and then "node"
-- debugger_path = "(runtimedir)/site/pack/packer/opt/vscode-js-debug", -- Path to vscode-js-debug installation.
-- debugger_path = "(runtimedir)/site/pack/packer/opt/vscode-js-debug", -- Path to vscode-js-debug installation.
-- debugger_cmd = { "js-debug-adapter" }, -- Command to use to launch the debug server. Takes precedence over `node_path` and `debugger_path`.
adapters = { 'pwa-node', 'pwa-chrome', 'pwa-msedge', 'node-terminal', 'pwa-extensionHost' }, -- which adapters to register in nvim-dap
})

Expand Down
1 change: 1 addition & 0 deletions lua/dap-vscode-js/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local utils = require("dap-vscode-js.utils")
local defaults = {
node_path = os.getenv("NODE_PATH") or "node",
debugger_path = utils.join_paths(utils.get_runtime_dir(), "site/pack/packer/opt/vscode-js-debug"),
debugger_cmd = nil,
}

local config = vim.deepcopy(defaults)
Expand Down
1 change: 1 addition & 0 deletions lua/dap-vscode-js/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
---@class Settings @Plugin configuration options
---@field node_path string: Path of node executable. Defaults to $NODE_PATH, and then "node"
---@field debugger_path string: Path to vscode-js-debug. Defaults to (runtimedir)/site/pack/packer/opt/vscode-js-debug
---@field debugger_cmd string[]?: The command to use to launch the debug server. This option takes precedence over both `node_path` and `debugger_path`.
---@field adapters string[]: List of adapters to configure. Options are 'pwa-node', 'pwa-chrome', 'pwa-msedge', 'node-terminal', 'pwa-extensionHost'. Defaults to all. See https://github.com/microsoft/vscode-js-debug/blob/main/OPTIONS.md for configuration options.

local config = require("dap-vscode-js.config")
Expand Down
32 changes: 21 additions & 11 deletions lua/dap-vscode-js/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ local function debugger_entrypoint(debugger_path)
return M.join_paths(debugger_path, "out/src/vsDebugServer.js")
end

---@param config Settings
local function get_spawn_cmd(config)
if config.debugger_cmd then
return assert(config.debugger_cmd[1], "debugger_cmd is empty"), { table.unpack(config.debugger_cmd, 2) }
end
local entrypoint = debugger_entrypoint(config.debugger_path)

if not file_exists(entrypoint) then
error("Debugger entrypoint file '" .. entrypoint .. "' does not exist. Did it build properly?")
end
return config.node_path, { entrypoint }
end

function M.start_debugger(config, on_launch, on_exit, on_error, on_stderror)
on_launch = schedule_wrap_safe(on_launch)
on_exit = schedule_wrap_safe(on_exit)
Expand All @@ -60,13 +73,6 @@ function M.start_debugger(config, on_launch, on_exit, on_error, on_stderror)
local stderr = uv.new_pipe(false)
local handle, pid_or_err

local entrypoint = debugger_entrypoint(config.debugger_path)

if not file_exists(entrypoint) then
on_error("Debugger entrypoint file '" .. entrypoint .. "' does not exist. Did it build properly?")
return
end

local exit = function(code, signal)
stdin:close()
stdout:close()
Expand All @@ -77,10 +83,14 @@ function M.start_debugger(config, on_launch, on_exit, on_error, on_stderror)
on_exit(code, signal)
end

handle, pid_or_err = uv.spawn(config.node_path, {
args = {
entrypoint,
},
local ok, cmd, args = pcall(get_spawn_cmd, config)
if not ok then
on_error(cmd)
return
end

handle, pid_or_err = uv.spawn(cmd, {
args = args,
stdio = { stdin, stdout, stderr },
detached = true,
}, function(code, signal)
Expand Down

0 comments on commit e95044e

Please sign in to comment.