|
|
@ -90,15 +90,7 @@ impl Repository {
|
|
|
|
pub async fn init(config: Config) -> LibResult<Self> {
|
|
|
|
pub async fn init(config: Config) -> LibResult<Self> {
|
|
|
|
Self::create_folders().await?;
|
|
|
|
Self::create_folders().await?;
|
|
|
|
let web_api = WebApi::new(&config.dist_base_url);
|
|
|
|
let web_api = WebApi::new(&config.dist_base_url);
|
|
|
|
|
|
|
|
let versions = load_versions(&web_api).await?;
|
|
|
|
let versions = if let Some(v) = Versions::load().await {
|
|
|
|
|
|
|
|
v
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
let all_versions = web_api.get_versions().await?;
|
|
|
|
|
|
|
|
let v = Versions::new(all_versions);
|
|
|
|
|
|
|
|
v.save().await?;
|
|
|
|
|
|
|
|
v
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ok(Self {
|
|
|
|
Ok(Self {
|
|
|
|
config,
|
|
|
|
config,
|
|
|
@ -125,24 +117,15 @@ impl Repository {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Returns the path for the given node version
|
|
|
|
/// Returns the path for the given node version
|
|
|
|
pub async fn get_version_path(&self, version: &NodeVersion) -> LibResult<Option<NodePath>> {
|
|
|
|
pub fn get_version_path(&self, version: &NodeVersion) -> Option<NodePath> {
|
|
|
|
let info = self.parse_req(&version);
|
|
|
|
let info = self.parse_req(&version);
|
|
|
|
let mut iter = fs::read_dir(&*NODE_VERSIONS_DIR).await?;
|
|
|
|
let path = build_version_path(&info.version);
|
|
|
|
let mut path = None;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while let Some(entry) = iter.next_entry().await? {
|
|
|
|
if path.exists() {
|
|
|
|
if let Ok(version) = Version::parse(entry.file_name().to_string_lossy().as_ref()) {
|
|
|
|
Some(NodePath::new(path))
|
|
|
|
if version == info.version {
|
|
|
|
} else {
|
|
|
|
path = Some(entry.path());
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let Some(path) = path else {
|
|
|
|
|
|
|
|
return Ok(None);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
let path = path.join(format!("node-v{}-{}-{}", info.version, OS, ARCH));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ok(Some(NodePath::new(path)))
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Returns a list of installed versions
|
|
|
|
/// Returns a list of installed versions
|
|
|
@ -160,10 +143,10 @@ impl Repository {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Returns if the given version is installed
|
|
|
|
/// Returns if the given version is installed
|
|
|
|
pub async fn is_installed(&self, version: &NodeVersion) -> LibResult<bool> {
|
|
|
|
pub fn is_installed(&self, version: &NodeVersion) -> bool {
|
|
|
|
let info = self.parse_req(version);
|
|
|
|
let info = self.parse_req(version);
|
|
|
|
|
|
|
|
|
|
|
|
Ok(self.installed_versions().await?.contains(&info.version))
|
|
|
|
build_version_path(&info.version).exists()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Installs a specified node version
|
|
|
|
/// Installs a specified node version
|
|
|
@ -208,3 +191,22 @@ impl Repository {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
|
|
|
async fn load_versions(web_api: &WebApi) -> Result<Versions, crate::error::Error> {
|
|
|
|
|
|
|
|
let versions = if let Some(v) = Versions::load().await {
|
|
|
|
|
|
|
|
v
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
let all_versions = web_api.get_versions().await?;
|
|
|
|
|
|
|
|
let v = Versions::new(all_versions);
|
|
|
|
|
|
|
|
v.save().await?;
|
|
|
|
|
|
|
|
v
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(versions)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn build_version_path(version: &Version) -> PathBuf {
|
|
|
|
|
|
|
|
NODE_VERSIONS_DIR
|
|
|
|
|
|
|
|
.join(version.to_string())
|
|
|
|
|
|
|
|
.join(format!("node-v{}-{}-{}", version, OS, ARCH))
|
|
|
|
|
|
|
|
}
|
|
|
|