Skip to content

Commit

Permalink
Merge branch 'main' into improvement-on-with-vite
Browse files Browse the repository at this point in the history
  • Loading branch information
zsh77 committed Mar 5, 2024
2 parents eff3992 + 1eb99c1 commit 1e66999
Show file tree
Hide file tree
Showing 102 changed files with 9,282 additions and 6,383 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/turborepo-lib/src/commands/telemetry.rs
Expand Up @@ -38,7 +38,7 @@ pub fn configure(
base: &mut CommandBase,
telemetry: CommandEventBuilder,
) {
let config = TelemetryConfig::new();
let config = TelemetryConfig::with_default_config_path();
let mut config = match config {
Ok(config) => config,
Err(e) => {
Expand Down
2 changes: 2 additions & 0 deletions crates/turborepo-telemetry/Cargo.toml
Expand Up @@ -11,6 +11,7 @@ workspace = true

[dev-dependencies]
serde_json = { workspace = true }
tempfile = { workspace = true }
test-case = { workspace = true }
turborepo-vercel-api-mock = { workspace = true }

Expand All @@ -30,6 +31,7 @@ sha2 = "0.10.8"
thiserror = { workspace = true }
tokio = { workspace = true, features = ["full", "time"] }
tracing = { workspace = true }
turbopath = { workspace = true }
turborepo-api-client = { workspace = true }
turborepo-dirs = { path = "../turborepo-dirs" }
turborepo-ui = { workspace = true }
Expand Down
84 changes: 41 additions & 43 deletions crates/turborepo-telemetry/src/config.rs
@@ -1,4 +1,4 @@
use std::{env, fs, path::Path};
use std::env;

use chrono::{DateTime, Utc};
pub use config::{Config, ConfigError, File, FileFormat};
Expand All @@ -7,6 +7,7 @@ use serde::{Deserialize, Serialize};
use serde_json;
use sha2::{Digest, Sha256};
use tracing::{debug, error};
use turbopath::{AbsoluteSystemPath, AbsoluteSystemPathBuf};
use turborepo_dirs::config_dir;
use turborepo_ui::{color, BOLD, GREY, UI, UNDERLINE};
use uuid::Uuid;
Expand Down Expand Up @@ -48,20 +49,24 @@ impl Default for TelemetryConfigContents {

#[derive(Debug)]
pub struct TelemetryConfig {
config_path: String,
config_path: AbsoluteSystemPathBuf,
config: TelemetryConfigContents,
}

impl TelemetryConfig {
pub fn new() -> Result<TelemetryConfig, ConfigError> {
let file_path = &get_config_path()?;
debug!("Telemetry config path: {}", file_path);
if !Path::new(file_path).try_exists().unwrap_or(false) {
write_new_config()?;
pub fn with_default_config_path() -> Result<TelemetryConfig, ConfigError> {
let config_path = default_config_path()?;
TelemetryConfig::new(config_path)
}

pub fn new(config_path: AbsoluteSystemPathBuf) -> Result<TelemetryConfig, ConfigError> {
debug!("Telemetry config path: {}", config_path);
if !config_path.exists() {
write_new_config(&config_path)?;
}

let mut settings = Config::builder();
settings = settings.add_source(File::new(file_path, FileFormat::Json));
settings = settings.add_source(File::new(config_path.as_str(), FileFormat::Json));
let settings = settings.build();

// If this is a FileParse error, we assume something corrupted the file or
Expand All @@ -71,16 +76,18 @@ impl TelemetryConfig {
let config = match settings {
Ok(settings) => settings.try_deserialize::<TelemetryConfigContents>()?,
Err(ConfigError::FileParse { .. }) => {
fs::remove_file(file_path).map_err(|e| ConfigError::Message(e.to_string()))?;
write_new_config()?;
config_path
.remove_file()
.map_err(|e| ConfigError::Message(e.to_string()))?;
write_new_config(&config_path)?;
return Err(settings.unwrap_err());
}
// Propagate other errors
Err(err) => return Err(err),
};

let config = TelemetryConfig {
config_path: file_path.to_string(),
config_path,
config,
};

Expand All @@ -90,13 +97,14 @@ impl TelemetryConfig {
fn write(&self) -> Result<(), ConfigError> {
let serialized = serde_json::to_string_pretty(&self.config)
.map_err(|e| ConfigError::Message(e.to_string()))?;
fs::write(&self.config_path, serialized)
self.config_path
.create_with_contents(serialized)
.map_err(|e| ConfigError::Message(e.to_string()))?;
Ok(())
}

pub fn one_way_hash(input: &str) -> String {
match TelemetryConfig::new() {
match TelemetryConfig::with_default_config_path() {
Ok(config) => config.one_way_hash_with_config_salt(input),
Err(_) => TelemetryConfig::one_way_hash_with_tmp_salt(input),
}
Expand Down Expand Up @@ -219,44 +227,34 @@ impl TelemetryConfig {
}
}

fn get_config_path() -> Result<String, ConfigError> {
if cfg!(test) {
let tmp_dir = env::temp_dir();
let config_path = tmp_dir.join("test-telemetry.json");
Ok(config_path.to_str().unwrap().to_string())
} else {
let config_dir = config_dir().ok_or(ConfigError::Message(
"Unable to find telemetry config directory".to_string(),
))?;
// stored as a sibling to the turbo global config
let config_path = config_dir.join("turborepo").join("telemetry.json");
Ok(config_path.to_str().unwrap().to_string())
}
fn default_config_path() -> Result<AbsoluteSystemPathBuf, ConfigError> {
let config_dir = config_dir().ok_or(ConfigError::Message(
"Unable to find telemetry config directory".to_string(),
))?;
let abs_config_dir = AbsoluteSystemPathBuf::try_from(config_dir.as_path()).map_err(|e| {
ConfigError::Message(format!(
"Invalid config directory {}: {}",
config_dir.display(),
e
))
})?;
// stored as a sibling to the turbo global config
Ok(abs_config_dir.join_components(&["turborepo", "telemetry.json"]))
}

fn write_new_config() -> Result<(), ConfigError> {
let file_path = get_config_path()?;
fn write_new_config(file_path: &AbsoluteSystemPath) -> Result<(), ConfigError> {
let serialized = serde_json::to_string_pretty(&TelemetryConfigContents::default())
.map_err(|e| ConfigError::Message(e.to_string()))?;

// Extract the directory path from the file path
let dir_path = Path::new(&file_path).parent().ok_or_else(|| {
ConfigError::Message("Failed to extract directory path from file path".to_string())
})?;

// Create the directory if it doesn't exist
if !dir_path.try_exists().unwrap_or(false) {
fs::create_dir_all(dir_path).map_err(|e| {
ConfigError::Message(format!(
"Failed to create directory {}: {}",
dir_path.display(),
e
))
})?;
}
file_path
.ensure_dir()
.map_err(|_| ConfigError::Message("Failed to create directory".to_string()))?;

// Write the file
fs::write(&file_path, serialized).map_err(|e| ConfigError::Message(e.to_string()))?;
file_path
.create_with_contents(serialized)
.map_err(|e| ConfigError::Message(e.to_string()))?;
Ok(())
}

Expand Down
33 changes: 27 additions & 6 deletions crates/turborepo-telemetry/src/lib.rs
Expand Up @@ -76,12 +76,12 @@ pub fn telem(event: events::TelemetryEvent) {
}

fn init(
mut config: TelemetryConfig,
client: impl telemetry::TelemetryClient + Clone + Send + Sync + 'static,
ui: UI,
) -> Result<(TelemetryHandle, TelemetrySender), Box<dyn std::error::Error>> {
let (tx, rx) = mpsc::unbounded_channel();
let (cancel_tx, cancel_rx) = oneshot::channel();
let mut config = TelemetryConfig::new()?;
config.show_alert(ui);

let session_id = Uuid::new_v4();
Expand Down Expand Up @@ -122,7 +122,8 @@ pub fn init_telemetry(
debug!("telemetry already initialized");
return Err(Box::new(Error::AlreadyInitialized()));
}
let (handle, sender) = init(client, ui)?;
let config = TelemetryConfig::with_default_config_path()?;
let (handle, sender) = init(config, client, ui)?;
SENDER_INSTANCE.set(sender).unwrap();
Ok(handle)
}
Expand Down Expand Up @@ -260,11 +261,12 @@ mod tests {
select,
sync::{mpsc, mpsc::UnboundedReceiver},
};
use turbopath::AbsoluteSystemPathBuf;
use turborepo_api_client::telemetry::TelemetryClient;
use turborepo_ui::UI;
use turborepo_vercel_api::telemetry::{TelemetryEvent, TelemetryGenericEvent};

use crate::init;
use crate::{config::TelemetryConfig, init};

#[derive(Clone)]
struct DummyClient {
Expand Down Expand Up @@ -322,16 +324,27 @@ mod tests {
}
}

fn temp_dir() -> (tempfile::TempDir, AbsoluteSystemPathBuf) {
let temp_dir = tempfile::tempdir().unwrap();
let path = AbsoluteSystemPathBuf::try_from(temp_dir.path()).unwrap();
(temp_dir, path)
}

#[tokio::test]
async fn test_batching() {
let (_tmp, temp_dir) = temp_dir();
let config =
TelemetryConfig::new(temp_dir.join_components(&["turborepo", "telemetry.json"]))
.unwrap();

let (tx, mut rx) = mpsc::unbounded_channel();

let client = DummyClient {
events: Default::default(),
tx,
};

let result = init(client.clone(), UI::new(false));
let result = init(config, client.clone(), UI::new(false));

let (telemetry_handle, telemetry_sender) = result.unwrap();

Expand Down Expand Up @@ -360,14 +373,18 @@ mod tests {

#[tokio::test]
async fn test_batching_across_two_batches() {
let (_tmp, temp_dir) = temp_dir();
let config =
TelemetryConfig::new(temp_dir.join_components(&["turborepo", "telemetry.json"]))
.unwrap();
let (tx, mut rx) = mpsc::unbounded_channel();

let client = DummyClient {
events: Default::default(),
tx,
};

let result = init(client.clone(), UI::new(false));
let result = init(config, client.clone(), UI::new(false));

let (telemetry_handle, telemetry_sender) = result.unwrap();

Expand Down Expand Up @@ -402,14 +419,18 @@ mod tests {

#[tokio::test]
async fn test_closing() {
let (_tmp, temp_dir) = temp_dir();
let config =
TelemetryConfig::new(temp_dir.join_components(&["turborepo", "telemetry.json"]))
.unwrap();
let (tx, mut _rx) = mpsc::unbounded_channel();

let client = DummyClient {
events: Default::default(),
tx,
};

let result = init(client.clone(), UI::new(false));
let result = init(config, client.clone(), UI::new(false));

let (telemetry_handle, telemetry_sender) = result.unwrap();

Expand Down
14 changes: 7 additions & 7 deletions examples/basic/apps/docs/package.json
Expand Up @@ -10,19 +10,19 @@
},
"dependencies": {
"@repo/ui": "workspace:*",
"next": "^14.0.4",
"next": "^14.1.1",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@next/eslint-plugin-next": "^14.0.4",
"@next/eslint-plugin-next": "^14.1.1",
"@repo/eslint-config": "workspace:*",
"@repo/typescript-config": "workspace:*",
"@types/eslint": "^8.56.1",
"@types/node": "^20.10.6",
"@types/react": "^18.2.46",
"@types/react-dom": "^18.2.18",
"eslint": "^8.56.0",
"@types/eslint": "^8.56.5",
"@types/node": "^20.11.24",
"@types/react": "^18.2.61",
"@types/react-dom": "^18.2.19",
"eslint": "^8.57.0",
"typescript": "^5.3.3"
}
}
14 changes: 7 additions & 7 deletions examples/basic/apps/web/package.json
Expand Up @@ -10,19 +10,19 @@
},
"dependencies": {
"@repo/ui": "workspace:*",
"next": "^14.0.4",
"next": "^14.1.1",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@next/eslint-plugin-next": "^14.0.4",
"@next/eslint-plugin-next": "^14.1.1",
"@repo/eslint-config": "workspace:*",
"@repo/typescript-config": "workspace:*",
"@types/eslint": "^8.56.1",
"@types/node": "^20.10.6",
"@types/react": "^18.2.46",
"@types/react-dom": "^18.2.18",
"eslint": "^8.56.0",
"@types/eslint": "^8.56.5",
"@types/node": "^20.11.24",
"@types/react": "^18.2.61",
"@types/react-dom": "^18.2.19",
"eslint": "^8.57.0",
"typescript": "^5.3.3"
}
}
4 changes: 2 additions & 2 deletions examples/basic/package.json
Expand Up @@ -10,8 +10,8 @@
"devDependencies": {
"@repo/eslint-config": "workspace:*",
"@repo/typescript-config": "workspace:*",
"prettier": "^3.1.1",
"turbo": "^1.12.1"
"prettier": "^3.2.5",
"turbo": "^1.12.4"
},
"packageManager": "pnpm@8.9.0",
"engines": {
Expand Down
8 changes: 4 additions & 4 deletions examples/basic/packages/eslint-config/package.json
Expand Up @@ -8,12 +8,12 @@
"react-internal.js"
],
"devDependencies": {
"@vercel/style-guide": "^5.1.0",
"eslint-config-turbo": "^1.11.3",
"@vercel/style-guide": "^5.2.0",
"eslint-config-turbo": "^1.12.4",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-only-warn": "^1.1.0",
"@typescript-eslint/parser": "^6.17.0",
"@typescript-eslint/eslint-plugin": "^6.17.0",
"@typescript-eslint/parser": "^7.1.0",
"@typescript-eslint/eslint-plugin": "^7.1.0",
"typescript": "^5.3.3"
}
}
12 changes: 6 additions & 6 deletions examples/basic/packages/ui/package.json
Expand Up @@ -14,12 +14,12 @@
"devDependencies": {
"@repo/eslint-config": "workspace:*",
"@repo/typescript-config": "workspace:*",
"@turbo/gen": "^1.11.3",
"@types/node": "^20.10.6",
"@types/eslint": "^8.56.1",
"@types/react": "^18.2.46",
"@types/react-dom": "^18.2.18",
"eslint": "^8.56.0",
"@turbo/gen": "^1.12.4",
"@types/node": "^20.11.24",
"@types/eslint": "^8.56.5",
"@types/react": "^18.2.61",
"@types/react-dom": "^18.2.19",
"eslint": "^8.57.0",
"react": "^18.2.0",
"typescript": "^5.3.3"
}
Expand Down

0 comments on commit 1e66999

Please sign in to comment.