Skip to content

Commit 4255510

Browse files
williambomanmxsdev
andauthored
feat: add debugger_cmd config (#10)
* feat: add debugger_cmd config This is to allow providing a command in PATH that launches the debug server, instead of having to deal with providing absolute paths. * add unit Co-authored-by: charburgx <charburgx@gmail.com>
1 parent 32b0b9f commit 4255510

5 files changed

Lines changed: 50 additions & 13 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ npm run compile
5252
```lua
5353
require("dap-vscode-js").setup({
5454
-- node_path = "node", -- Path of node executable. Defaults to $NODE_PATH, and then "node"
55-
-- debugger_path = "(runtimedir)/site/pack/packer/opt/vscode-js-debug", -- Path to vscode-js-debug installation.
55+
-- debugger_path = "(runtimedir)/site/pack/packer/opt/vscode-js-debug", -- Path to vscode-js-debug installation.
56+
-- debugger_cmd = { "js-debug-adapter" }, -- Command to use to launch the debug server. Takes precedence over `node_path` and `debugger_path`.
5657
adapters = { 'pwa-node', 'pwa-chrome', 'pwa-msedge', 'node-terminal', 'pwa-extensionHost' }, -- which adapters to register in nvim-dap
5758
})
5859

lua/dap-vscode-js/config.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ local utils = require("dap-vscode-js.utils")
33
local defaults = {
44
node_path = os.getenv("NODE_PATH") or "node",
55
debugger_path = utils.join_paths(utils.get_runtime_dir(), "site/pack/packer/opt/vscode-js-debug"),
6+
debugger_cmd = nil,
67
}
78

89
local config = vim.deepcopy(defaults)

lua/dap-vscode-js/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
---@class Settings @Plugin configuration options
44
---@field node_path string: Path of node executable. Defaults to $NODE_PATH, and then "node"
55
---@field debugger_path string: Path to vscode-js-debug. Defaults to (runtimedir)/site/pack/packer/opt/vscode-js-debug
6+
---@field debugger_cmd string[]?: The command to use to launch the debug server. This option takes precedence over both `node_path` and `debugger_path`.
67
---@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.
78

89
local config = require("dap-vscode-js.config")

lua/dap-vscode-js/utils.lua

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@ local function debugger_entrypoint(debugger_path)
4949
return M.join_paths(debugger_path, "out/src/vsDebugServer.js")
5050
end
5151

52+
---@param config Settings
53+
local function get_spawn_cmd(config)
54+
if config.debugger_cmd then
55+
return assert(config.debugger_cmd[1], "debugger_cmd is empty"), { unpack(config.debugger_cmd, 2) }
56+
end
57+
local entrypoint = debugger_entrypoint(config.debugger_path)
58+
59+
if not file_exists(entrypoint) then
60+
error("Debugger entrypoint file '" .. entrypoint .. "' does not exist. Did it build properly?")
61+
end
62+
return config.node_path, { entrypoint }
63+
end
64+
5265
function M.start_debugger(config, on_launch, on_exit, on_error, on_stderror)
5366
on_launch = schedule_wrap_safe(on_launch)
5467
on_exit = schedule_wrap_safe(on_exit)
@@ -60,13 +73,6 @@ function M.start_debugger(config, on_launch, on_exit, on_error, on_stderror)
6073
local stderr = uv.new_pipe(false)
6174
local handle, pid_or_err
6275

63-
local entrypoint = debugger_entrypoint(config.debugger_path)
64-
65-
if not file_exists(entrypoint) then
66-
on_error("Debugger entrypoint file '" .. entrypoint .. "' does not exist. Did it build properly?")
67-
return
68-
end
69-
7076
local exit = function(code, signal)
7177
stdin:close()
7278
stdout:close()
@@ -77,10 +83,14 @@ function M.start_debugger(config, on_launch, on_exit, on_error, on_stderror)
7783
on_exit(code, signal)
7884
end
7985

80-
handle, pid_or_err = uv.spawn(config.node_path, {
81-
args = {
82-
entrypoint,
83-
},
86+
local ok, cmd, args = pcall(get_spawn_cmd, config)
87+
if not ok then
88+
on_error(cmd)
89+
return
90+
end
91+
92+
handle, pid_or_err = uv.spawn(cmd, {
93+
args = args,
8494
stdio = { stdin, stdout, stderr },
8595
detached = true,
8696
}, function(code, signal)
@@ -115,4 +125,5 @@ function M.start_debugger(config, on_launch, on_exit, on_error, on_stderror)
115125
return proc
116126
end
117127

128+
M.get_spawn_cmd = get_spawn_cmd
118129
return M

tests/unit/utils_spec.lua

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
local utils = require("dap-vscode-js.utils")
22
local async = require("plenary.async.tests")
33
local wrap = require("plenary.async.async").wrap
4-
-- local async = require"dap-vscode-js-tests.utils".async
4+
local config = require("dap-vscode-js.config")
5+
local test_utils = require("__dap_js_test_util")
56

67
describe("dap-vscode-js.utils", function()
78
describe(".start_debugger", function()
@@ -23,4 +24,26 @@ describe("dap-vscode-js.utils", function()
2324
end, 1)
2425
)
2526
end)
27+
28+
describe(".get_spawn_cmd", function()
29+
it("will use debug_cmd when provided", function()
30+
test_utils.setup_dapjs({
31+
debugger_cmd = { "a", "b", "c" },
32+
})
33+
34+
local cmdname, args = utils.get_spawn_cmd(config)
35+
assert.same(cmdname, "a")
36+
assert.same(args, { "b", "c" })
37+
end)
38+
39+
it("will error when debug_cmd is invalid", function()
40+
test_utils.setup_dapjs({
41+
debugger_cmd = {},
42+
})
43+
44+
assert.errors(function()
45+
utils.get_spawn_cmd(config)
46+
end)
47+
end)
48+
end)
2649
end)

0 commit comments

Comments
 (0)