use std::path::{Path, PathBuf};
use tempfile::NamedTempFile;
use crate::error::DocusaurusError;
pub fn find_node() -> Result<PathBuf, DocusaurusError> {
Ok(which::which("node")?)
}
pub fn find_addon(site_dir: &Path) -> Result<PathBuf, DocusaurusError> {
let addon_name = addon_filename();
let mut dir = site_dir.to_path_buf();
loop {
let candidate = dir
.join("node_modules")
.join("docusau-rs")
.join(&addon_name);
if candidate.exists() {
return Ok(candidate);
}
match dir.parent() {
Some(parent) => dir = parent.to_path_buf(),
None => return Err(DocusaurusError::AddonNotFound),
}
}
}
fn addon_filename() -> String {
if cfg!(target_os = "windows") {
"docusau_rs.win32-x64-msvc.node".to_string()
} else if cfg!(target_os = "macos") {
if cfg!(target_arch = "aarch64") {
"docusau_rs.darwin-arm64.node".to_string()
} else {
"docusau_rs.darwin-x64.node".to_string()
}
} else if cfg!(target_arch = "aarch64") {
"docusau_rs.linux-arm64-gnu.node".to_string()
} else {
"docusau_rs.linux-x64-gnu.node".to_string()
}
}
pub fn write_temp_config(config_json: &str) -> Result<(NamedTempFile, PathBuf), DocusaurusError> {
use std::io::Write as _;
let escaped = config_json.replace('\\', r"\\").replace('`', r"\`");
let js_content = format!("module.exports = JSON.parse(`{escaped}`);\n");
let mut file = tempfile::Builder::new().suffix(".js").tempfile()?;
file.write_all(js_content.as_bytes())?;
file.flush()?;
let path = file.path().to_path_buf();
Ok((file, path))
}