-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
lib.rs
170 lines (145 loc) · 5.17 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use std::{fmt, time::Duration};
use console::style;
use semver::Version as SemVerVersion;
use serde::Deserialize;
use thiserror::Error as ThisError;
use update_informer::{
http_client::{GenericHttpClient, HeaderMap, HttpClient},
Check, Package, Registry, Result as UpdateResult,
};
mod ui;
// 800ms
const DEFAULT_TIMEOUT: Duration = Duration::from_millis(800);
// 1 day
const DEFAULT_INTERVAL: Duration = Duration::from_secs(60 * 60 * 24);
const NOTIFIER_DISABLE_VARS: [&str; 2] = ["NO_UPDATE_NOTIFIER", "TURBO_NO_UPDATE_NOTIFIER"];
const ENVIRONMENTAL_DISABLE_VARS: [&str; 1] = ["CI"];
#[derive(ThisError, Debug)]
pub enum UpdateNotifierError {
#[error("Failed to write to terminal")]
RenderError(#[from] ui::utils::GetDisplayLengthError),
#[error("Failed to parse current version")]
VersionError(#[from] semver::Error),
}
#[derive(Deserialize, Debug)]
struct NpmVersionData {
version: String,
}
#[derive(Debug)]
enum VersionTag {
Latest,
Canary,
}
impl fmt::Display for VersionTag {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
VersionTag::Latest => write!(f, "latest"),
VersionTag::Canary => write!(f, "canary"),
}
}
}
struct NPMRegistry;
impl Registry for NPMRegistry {
const NAME: &'static str = "npm-registry";
fn get_latest_version<T: HttpClient>(
http: GenericHttpClient<T>,
pkg: &Package,
) -> UpdateResult<Option<String>> {
// determine tag to request
let tag = get_tag_from_version(&pkg.version().semver().pre);
// since we're overloading tag within name, we need to split it back out
let full_name = pkg.to_string();
let split_name: Vec<&str> = full_name.split('/').collect();
let name = split_name[1];
let url = format!(
"https://turbo.build/api/binaries/version?name={name}&tag={tag}",
name = name,
tag = tag
);
let result: NpmVersionData = http.get(&url)?;
Ok(Some(result.version))
}
}
// Source https://github.com/mgrachev/update-informer/blob/main/src/http_client/reqwest.rs
// Vendored here until update-informer allows us to control tls implementation
pub struct ReqwestHttpClient;
impl HttpClient for ReqwestHttpClient {
fn get<T: serde::de::DeserializeOwned>(
url: &str,
timeout: Duration,
headers: HeaderMap,
) -> Result<T, Box<dyn std::error::Error>> {
let mut req = reqwest::blocking::Client::builder()
.timeout(timeout)
.build()?
.get(url);
for (key, value) in headers {
req = req.header(key, value);
}
let json = req.send()?.json()?;
Ok(json)
}
}
fn get_tag_from_version(pre: &semver::Prerelease) -> VersionTag {
match pre {
t if t.contains("canary") => VersionTag::Canary,
_ => VersionTag::Latest,
}
}
fn should_skip_notification() -> bool {
NOTIFIER_DISABLE_VARS
.iter()
.any(|var| std::env::var(var).is_ok())
|| ENVIRONMENTAL_DISABLE_VARS
.iter()
.any(|var| std::env::var(var).is_ok())
|| !atty::is(atty::Stream::Stdout)
}
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> {
// bail early if the user has disabled update notifications
if should_skip_notification() {
return Ok(());
}
// we want notifications per channel (latest, canary, etc) so we need to ensure
// we have one cached latest version per channel. UpdateInformer does not
// support this out of the box, so we hack it into the name by overloading
// owner (in the supported owner/name format) to be channel/name.
let parsed_version = SemVerVersion::parse(current_version)?;
let tag = get_tag_from_version(&parsed_version.pre);
let package_name = format!("{}/{}", tag, package_name);
let timeout = timeout.unwrap_or(DEFAULT_TIMEOUT);
let interval = interval.unwrap_or(DEFAULT_INTERVAL);
let informer = update_informer::new(NPMRegistry, package_name, current_version)
.http_client(ReqwestHttpClient)
.timeout(timeout)
.interval(interval);
if let Ok(Some(version)) = informer.check_version() {
let latest_version = version.to_string();
// TODO: make this package manager aware
let update_cmd = style("npx @turbo/codemod update").cyan().bold();
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 = style("v").dim(),
current_version = style(current_version).dim(),
latest_version = style(latest_version).green().bold(),
github_repo = github_repo,
update_cmd = update_cmd
);
if let Some(footer) = footer {
return ui::message(&format!("{}\n{}", msg, footer));
}
return ui::message(&msg);
}
Ok(())
}