use crate::error::Result;
use crate::module_trait::{Module, ModuleContext};
use crate::modules::utils;
use std::process::Command;
pub struct DenoModule;
impl Default for DenoModule {
fn default() -> Self {
Self::new()
}
}
impl DenoModule {
pub fn new() -> Self {
Self
}
}
impl Module for DenoModule {
fn fs_markers(&self) -> &'static [&'static str] {
&["deno.json", "deno.jsonc"]
}
fn is_blocking(&self) -> bool {
true
}
fn render(&self, format: &str, context: &ModuleContext) -> Result<Option<String>> {
let has_marker = ["deno.json", "deno.jsonc"]
.into_iter()
.any(|marker| context.marker_path(marker).is_some());
if !has_marker {
return Ok(None);
}
if context.no_version {
return Ok(Some(String::new()));
}
let normalized_format = utils::validate_version_format(format, "deno")?;
let version = match get_deno_version() {
Some(v) => v,
None => return Ok(None),
};
let version_str = version.as_str();
match normalized_format {
"full" => Ok(Some(version_str.to_string())),
"short" => Ok(Some(utils::shorten_version(version_str))),
"major" => Ok(version_str.split('.').next().map(|s| s.to_string())),
_ => unreachable!("validate_version_format should have caught this"),
}
}
}
#[cold]
fn get_deno_version() -> Option<String> {
let output = Command::new("deno").arg("--version").output().ok()?;
if !output.status.success() {
return None;
}
let version_str = String::from_utf8_lossy(&output.stdout);
version_str
.lines()
.next()
.and_then(|l| l.split_whitespace().nth(1))
.map(|v| v.to_string())
}