mirror of https://github.com/Trivernis/nenv
Add .node-version file support
parent
835a366caa
commit
a634f2be47
@ -0,0 +1,19 @@
|
||||
use std::str::FromStr;
|
||||
|
||||
use miette::{Context, IntoDiagnostic};
|
||||
|
||||
use crate::repository::NodeVersion;
|
||||
|
||||
use super::VersionDetector;
|
||||
|
||||
pub struct EnvDetector;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl VersionDetector for EnvDetector {
|
||||
async fn detect_version() -> miette::Result<Option<crate::repository::NodeVersion>> {
|
||||
std::env::var("NODE_VERSION")
|
||||
.into_diagnostic()
|
||||
.context("Reading version from environment")
|
||||
.map(|v| NodeVersion::from_str(&v).ok())
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
use async_trait::async_trait;
|
||||
|
||||
use futures::future;
|
||||
use miette::Result;
|
||||
mod env_detector;
|
||||
mod package_json_detector;
|
||||
mod version_file_detector;
|
||||
|
||||
use crate::repository::NodeVersion;
|
||||
|
||||
use self::{
|
||||
env_detector::EnvDetector, package_json_detector::PackageJsonDetector,
|
||||
version_file_detector::VersionFileDetector,
|
||||
};
|
||||
|
||||
#[async_trait]
|
||||
pub trait VersionDetector {
|
||||
async fn detect_version() -> Result<Option<NodeVersion>>;
|
||||
}
|
||||
|
||||
pub struct ParallelDetector;
|
||||
|
||||
#[async_trait]
|
||||
impl VersionDetector for ParallelDetector {
|
||||
async fn detect_version() -> Result<Option<NodeVersion>> {
|
||||
let version = future::join_all(vec![
|
||||
VersionFileDetector::detect_version(),
|
||||
PackageJsonDetector::detect_version(),
|
||||
EnvDetector::detect_version(),
|
||||
])
|
||||
.await
|
||||
.into_iter()
|
||||
.filter_map(Result::ok)
|
||||
.find_map(|v| v);
|
||||
|
||||
Ok(version)
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
use std::str::FromStr;
|
||||
|
||||
use miette::{Context, IntoDiagnostic};
|
||||
use tokio::fs;
|
||||
|
||||
use crate::{repository::NodeVersion, utils::find_in_parents};
|
||||
|
||||
use super::VersionDetector;
|
||||
|
||||
pub struct VersionFileDetector;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl VersionDetector for VersionFileDetector {
|
||||
async fn detect_version() -> miette::Result<Option<crate::repository::NodeVersion>> {
|
||||
let dir = std::env::current_dir().into_diagnostic()?;
|
||||
|
||||
if let Some(path) = find_in_parents(dir, ".node-version") {
|
||||
let version_string = fs::read_to_string(path)
|
||||
.await
|
||||
.into_diagnostic()
|
||||
.context("Reading version file.")?;
|
||||
Ok(NodeVersion::from_str(&version_string).ok())
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue