-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
237 additions
and
327 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[package] | ||
name = "turbo-updater" | ||
version = "0.1.0" | ||
edition = "2021" | ||
description = "Minimal wrapper around update-informer to provide NPM registry support and consistent UI" | ||
license = "MPL-2.0" | ||
publish = false | ||
|
||
[dependencies] | ||
colored = "2.0" | ||
serde = { version = "1.0.126", features = ["derive"] } | ||
strip-ansi-escapes = "0.1.1" | ||
terminal_size = "0.2" | ||
thiserror = "1.0" | ||
update-informer = "0.5.0" | ||
ureq = { version = "2.3.0", features = ["json"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use std::time::Duration; | ||
|
||
use colored::*; | ||
use serde::Deserialize; | ||
use thiserror::Error as ThisError; | ||
use update_informer::{Check, Package, Registry, Result as UpdateResult}; | ||
use ureq; | ||
|
||
mod ui; | ||
|
||
const DEFAULT_TIMEOUT: Duration = Duration::from_millis(800); | ||
const DEFAULT_INTERVAL: Duration = Duration::from_secs(60 * 60 * 24); | ||
|
||
#[derive(ThisError, Debug)] | ||
pub enum UpdateNotifierError { | ||
#[error("Failed to write to terminal")] | ||
RenderError(#[from] ui::utils::GetDisplayLengthError), | ||
} | ||
|
||
#[derive(Deserialize)] | ||
struct NpmVersionData { | ||
version: String, | ||
} | ||
|
||
struct NPMRegistry; | ||
|
||
impl Registry for NPMRegistry { | ||
const NAME: &'static str = "npm_registry"; | ||
fn get_latest_version(pkg: &Package, _timeout: Duration) -> UpdateResult<Option<String>> { | ||
let url = format!("https://registry.npmjs.org/{}/latest", pkg); | ||
let resp = ureq::get(&url).timeout(_timeout).call()?; | ||
let result = resp.into_json::<NpmVersionData>().unwrap(); | ||
Ok(Some(result.version)) | ||
} | ||
} | ||
|
||
pub fn check_for_updates( | ||
package_name: &str, | ||
github_repo: &str, | ||
footer: Option<&str>, | ||
current_version: &str, | ||
timeout: Option<Duration>, | ||
interval: Option<Duration>, | ||
) -> Result<(), UpdateNotifierError> { | ||
let timeout = timeout.unwrap_or(DEFAULT_TIMEOUT); | ||
let interval = interval.unwrap_or(DEFAULT_INTERVAL); | ||
let informer = update_informer::new(NPMRegistry, package_name, current_version) | ||
.timeout(timeout) | ||
.interval(interval); | ||
if let Some(version) = informer.check_version().ok().flatten() { | ||
let latest_version = version.to_string(); | ||
let msg = format!( | ||
" | ||
Update available {version_prefix}{current_version} ≫ {latest_version} | ||
Changelog: {github_repo}/releases/tag/{latest_version} | ||
Run \"{update_cmd}\" to update | ||
", | ||
version_prefix = "v".dimmed(), | ||
current_version = current_version.dimmed(), | ||
latest_version = latest_version.green().bold(), | ||
github_repo = github_repo, | ||
// TODO: make this package manager aware | ||
update_cmd = "npm i -g turbo".cyan().bold(), | ||
); | ||
|
||
if let Some(footer) = footer { | ||
return ui::message(&format!("{}\n{}", msg, footer)); | ||
} | ||
|
||
return ui::message(&msg); | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.