diff --git a/crates/turborepo-lib/src/lib.rs b/crates/turborepo-lib/src/lib.rs index ca9d8ba0b4250..dfe3068a3beca 100644 --- a/crates/turborepo-lib/src/lib.rs +++ b/crates/turborepo-lib/src/lib.rs @@ -5,6 +5,7 @@ mod config; mod package_manager; mod retry; mod shim; +mod turbo_json; mod ui; use anyhow::Result; diff --git a/crates/turborepo-lib/src/shim.rs b/crates/turborepo-lib/src/shim.rs index d27cba83cede6..ebb0695b5670d 100644 --- a/crates/turborepo-lib/src/shim.rs +++ b/crates/turborepo-lib/src/shim.rs @@ -19,7 +19,7 @@ use serde::{Deserialize, Serialize}; use tiny_gradient::{GradientStr, RGB}; use turbo_updater::check_for_updates; -use crate::{cli, get_version, PackageManager, Payload}; +use crate::{cli, get_version, turbo_json::TurboJson, PackageManager, Payload}; static TURBO_JSON: &str = "turbo.json"; // all arguments that result in a stdout that much be directly parsable and @@ -239,7 +239,7 @@ impl RepoState { // that contains a `turbo.json` file. let root_path = current_dir .ancestors() - .find(|p| fs::metadata(p.join(TURBO_JSON)).is_ok()); + .find(|p| TurboJson::open(p.join(TURBO_JSON)).map_or(false, |t| t.no_extends())); // If that directory exists, then we figure out if there are workspaces defined // in it NOTE: This may change with multiple `turbo.json` files diff --git a/crates/turborepo-lib/src/turbo_json.rs b/crates/turborepo-lib/src/turbo_json.rs new file mode 100644 index 0000000000000..f803a11ae19fb --- /dev/null +++ b/crates/turborepo-lib/src/turbo_json.rs @@ -0,0 +1,33 @@ +use std::{fs::File, path::Path}; + +use anyhow::Result; +use serde::Deserialize; + +#[derive(Debug, Deserialize)] +pub struct TurboJson { + extends: Option>, +} + +impl TurboJson { + pub fn open(path: impl AsRef) -> Result { + Ok(serde_json::from_reader(File::open(path)?)?) + } + pub fn no_extends(&self) -> bool { + self.extends.is_none() + } +} + +#[test] +fn test_turbo_json() { + let turbo_json: TurboJson = serde_json::from_str("{}").unwrap(); + assert_eq!(turbo_json.extends, None); + + let turbo_json: TurboJson = serde_json::from_str(r#"{ "extends": ["//"] }"#).unwrap(); + assert_eq!(turbo_json.extends, Some(vec!["//".to_string()])); + + let turbo_json: TurboJson = serde_json::from_str(r#"{ "extends": ["//", "~"] }"#).unwrap(); + assert_eq!( + turbo_json.extends, + Some(vec!["//".to_string(), "~".to_string()]) + ); +}