1use std::borrow::Cow;
2
3use anyhow::{Result, bail};
4use async_trait::async_trait;
5use dap::{DapLocator, DebugRequest, adapters::DebugAdapterName};
6use gpui::SharedString;
7
8use task::{DebugScenario, SpawnInTerminal, TaskTemplate, VariableName};
9
10pub(crate) struct NodeLocator;
11
12const TYPESCRIPT_RUNNER_VARIABLE: VariableName =
13 VariableName::Custom(Cow::Borrowed("TYPESCRIPT_RUNNER"));
14
15#[async_trait]
16impl DapLocator for NodeLocator {
17 fn name(&self) -> SharedString {
18 SharedString::new_static("Node")
19 }
20
21 /// Determines whether this locator can generate debug target for given task.
22 async fn create_scenario(
23 &self,
24 build_config: &TaskTemplate,
25 resolved_label: &str,
26 adapter: &DebugAdapterName,
27 ) -> Option<DebugScenario> {
28 if adapter.0.as_ref() != "JavaScript" {
29 return None;
30 }
31 if build_config.command != TYPESCRIPT_RUNNER_VARIABLE.template_value() {
32 return None;
33 }
34
35 let config = serde_json::json!({
36 "request": "launch",
37 "type": "pwa-node",
38 "args": build_config.args.clone(),
39 "cwd": build_config.cwd.clone(),
40 "runtimeExecutable": build_config.command.clone(),
41 "env": build_config.env.clone(),
42 "runtimeArgs": ["--inspect-brk"],
43 "console": "integratedTerminal",
44 });
45
46 Some(DebugScenario {
47 adapter: adapter.0.clone(),
48 label: resolved_label.to_string().into(),
49 build: None,
50 config,
51 tcp_connection: None,
52 })
53 }
54
55 async fn run(&self, _: SpawnInTerminal) -> Result<DebugRequest> {
56 bail!("JavaScript locator should not require DapLocator::run to be ran");
57 }
58}