use super::*;
pub fn browser_type(type_name: &str) -> Option<&'static str> {
match type_name {
"chrome" | "pwa-chrome" => Some("pwa-chrome"),
"msedge" | "pwa-msedge" => Some("pwa-msedge"),
_ => None,
}
}
pub const BROWSER_DEBUG_PORT: u16 = 9222;
pub fn js_config_error(rc: &ResolvedConfig) -> Option<&'static str> {
if browser_type(&rc.type_name).is_some() {
return None;
}
match rc.request {
RequestKind::Launch if !js_launch_has_target(rc) => {
Some("a node launch needs a \"program\", a \"runtimeExecutable\" or a \"command\"")
}
RequestKind::Attach if rc.port.is_none() => Some("a node attach needs a \"port\""),
_ => None,
}
}
pub fn with_headless_browser(mut request: Value) -> Value {
if let Some(args) = request["arguments"].as_object_mut() {
let runtime = args
.entry("runtimeArgs")
.or_insert_with(|| Value::Array(Vec::new()));
if runtime.is_null() {
*runtime = Value::Array(Vec::new());
}
if let Some(list) = runtime.as_array_mut() {
let headless = list
.iter()
.any(|a| a.as_str().is_some_and(|a| a.starts_with("--headless")));
if !headless {
list.push(json!("--headless=new"));
}
}
}
request
}
pub(super) fn node_terminal_command(extra: &mut Map<String, Value>) {
let Some(Value::String(command)) = extra.remove("command") else {
return;
};
if names_runtime_executable(extra) || command.trim().is_empty() {
return;
}
let shell_syntax = command.contains(['&', '|', ';', '<', '>', '$', '`', '(', ')', '*', '?']);
let words = match shlex::split(&command) {
Some(words) if !shell_syntax && !words.is_empty() && !is_assignment(&words[0]) => words,
_ => vec![String::from("sh"), String::from("-c"), command],
};
let mut words = words.into_iter().map(Value::String);
extra.insert("runtimeExecutable".into(), words.next().expect("not empty"));
let mut runtime_args: Vec<Value> = words.collect();
if let Some(Value::Array(more)) = extra.get("runtimeArgs") {
runtime_args.extend(more.iter().cloned());
}
extra.insert("runtimeArgs".into(), Value::Array(runtime_args));
}
pub(super) fn is_assignment(word: &str) -> bool {
word.split_once('=').is_some_and(|(name, _)| {
name.starts_with(|c: char| c.is_ascii_alphabetic() || c == '_')
&& name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
})
}
pub(super) fn names_runtime_executable(extra: &Map<String, Value>) -> bool {
matches!(extra.get("runtimeExecutable"), Some(Value::String(s)) if !s.trim().is_empty())
}
pub fn js_launch_has_target(rc: &ResolvedConfig) -> bool {
rc.program.is_some()
|| names_runtime_executable(&rc.extra)
|| matches!(rc.extra.get("runtimeArgs"), Some(Value::Array(a)) if !a.is_empty())
}
pub fn with_workspace_folder(mut request: Value, root: &Path) -> Value {
if let Some(args) = request.get_mut("arguments").and_then(Value::as_object_mut) {
args.entry("__workspaceFolder")
.or_insert_with(|| json!(root.to_string_lossy()));
}
request
}
pub fn js_request(rc: &ResolvedConfig) -> Value {
let browser = browser_type(&rc.type_name);
let mut args = base_arguments(rc);
args.insert("type".into(), json!(browser.unwrap_or("pwa-node")));
if !rc.env.is_empty() {
args.insert("env".into(), json!(rc.env));
}
match rc.request {
RequestKind::Launch => {
args.insert("request".into(), json!("launch"));
if browser.is_none() {
args.insert("console".into(), json!("internalConsole"));
}
if !args.contains_key("sourceMaps") {
args.insert("sourceMaps".into(), json!(true));
}
request_value("launch", args)
}
RequestKind::Attach => {
args.insert("request".into(), json!("attach"));
let port = rc.port.or(browser.map(|_| BROWSER_DEBUG_PORT));
if let Some(port) = port {
args.insert("port".into(), json!(port));
}
if !args.contains_key("address") {
args.insert("address".into(), json!("127.0.0.1"));
}
args.remove("stopOnEntry");
request_value("attach", args)
}
}
}