node.rs

 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            && build_config.command != "composer"
33            && build_config.command != "npm"
34        {
35            return None;
36        }
37
38        let config = serde_json::json!({
39            "request": "launch",
40            "type": "pwa-node",
41            "args": build_config.args.clone(),
42            "cwd": build_config.cwd.clone(),
43            "runtimeExecutable": build_config.command.clone(),
44            "env": build_config.env.clone(),
45            "runtimeArgs": ["--inspect-brk"],
46            "console": "integratedTerminal",
47        });
48
49        Some(DebugScenario {
50            adapter: adapter.0.clone(),
51            label: resolved_label.to_string().into(),
52            build: None,
53            config,
54            tcp_connection: None,
55        })
56    }
57
58    async fn run(&self, _: SpawnInTerminal) -> Result<DebugRequest> {
59        bail!("JavaScript locator should not require DapLocator::run to be ran");
60    }
61}