From e9781d3f2aa52e6531fbdc0c3f2d280baa1e0a94 Mon Sep 17 00:00:00 2001 From: Will Crichton Date: Thu, 4 Apr 2024 12:06:56 -0700 Subject: [PATCH] Add optional "target" key to depot manifest. Fix pnpm directory --- crates/depot/src/commands/new.rs | 1 + crates/depot/src/commands/setup.rs | 4 +-- crates/depot/src/utils.rs | 3 +- crates/depot/src/workspace/package.rs | 44 +++++++++++++++------------ 4 files changed, 30 insertions(+), 22 deletions(-) diff --git a/crates/depot/src/commands/new.rs b/crates/depot/src/commands/new.rs index c6fcf71..cc1dffe 100644 --- a/crates/depot/src/commands/new.rs +++ b/crates/depot/src/commands/new.rs @@ -611,6 +611,7 @@ export default defineConfig(({{ mode }}) => ({{ let mut other: IndexMap = IndexMap::new(); let pkg_config = PackageDepotConfig { platform: *platform, + target: Some(*target), no_server: None, }; other.insert("depot".into(), serde_json::to_value(pkg_config)?); diff --git a/crates/depot/src/commands/setup.rs b/crates/depot/src/commands/setup.rs index 8e63311..4912578 100644 --- a/crates/depot/src/commands/setup.rs +++ b/crates/depot/src/commands/setup.rs @@ -51,7 +51,7 @@ impl GlobalConfig { root.display() ); - let pnpm_in_root = root.join(".bin").join("pnpm"); + let pnpm_in_root = root.join("bin").join("pnpm"); let pnpm_path = if pnpm_in_root.exists() { pnpm_in_root } else { @@ -143,7 +143,7 @@ impl SetupCommand { root: config_dir, pnpm_path: PathBuf::new(), }; - let bindir = config.root.join(".bin"); + let bindir = config.root.join("bin"); utils::create_dir_if_missing(&bindir)?; let pnpm_path = bindir.join("pnpm"); diff --git a/crates/depot/src/utils.rs b/crates/depot/src/utils.rs index f4d419e..245c2e3 100644 --- a/crates/depot/src/utils.rs +++ b/crates/depot/src/utils.rs @@ -54,11 +54,12 @@ macro_rules! test_packages { if !other.contains_key("depot") { other.insert(String::from("depot"), serde_json::to_value(PackageDepotConfig { platform: Platform::Browser, + target: None, no_server: None }).unwrap()); } let manifest = PackageManifest::from_json(manifest, std::path::Path::new("dummy.rs")).expect("Manifest failed to convert to Depot format"); - let pkg = Package::from_parts("dummy.rs".into(), manifest, index.get(), "dummy".into(), Target::Lib).expect("Package failed to build"); + let pkg = Package::from_parts("dummy.rs".into(), manifest, index.get(), Target::Lib).expect("Package failed to build"); index.set(index.get() + 1); pkg }),*] diff --git a/crates/depot/src/workspace/package.rs b/crates/depot/src/workspace/package.rs index b567d2d..9dc7595 100644 --- a/crates/depot/src/workspace/package.rs +++ b/crates/depot/src/workspace/package.rs @@ -34,10 +34,13 @@ impl Platform { } } -#[derive(Copy, Clone, clap::ValueEnum)] +#[derive(Copy, Clone, clap::ValueEnum, serde::Serialize, serde::Deserialize)] pub enum Target { + #[serde(rename = "lib")] Lib, + #[serde(rename = "site")] Site, + #[serde(rename = "script")] Script, } @@ -111,6 +114,8 @@ impl FromStr for PackageName { pub struct PackageDepotConfig { pub platform: Platform, #[serde(skip_serializing_if = "Option::is_none")] + pub target: Option, + #[serde(skip_serializing_if = "Option::is_none")] pub no_server: Option, } @@ -145,7 +150,6 @@ pub struct PackageInner { pub platform: Platform, pub target: Target, pub name: PackageName, - pub entry_point: PathBuf, pub index: PackageIndex, // Internals @@ -171,7 +175,6 @@ impl Package { root: PathBuf, manifest: PackageManifest, index: PackageIndex, - entry_point: PathBuf, target: Target, ) -> Result { let platform = manifest.config.platform; @@ -185,7 +188,6 @@ impl Package { Ok(Package::new(PackageInner { root: root.to_owned(), manifest, - entry_point, target, platform, name, @@ -195,27 +197,31 @@ impl Package { })) } + fn infer_target(root: &Path, manifest: &PackageManifest) -> Result { + if let Some(target) = manifest.config.target { + Ok(target) + } else if Self::find_source_file(root, "lib").is_some() { + Ok(Target::Lib) + } else if Self::find_source_file(root, "main").is_some() { + Ok(Target::Script) + } else if Self::find_source_file(root, "index").is_some() { + Ok(Target::Site) + } else { + bail!( + "Could not infer target. Consider adding a \"target\" entry under \"depot\" to: {}", + root.join("package.json").display() + ) + } + } + pub fn load(root: &Path, index: PackageIndex) -> Result { let root = root .canonicalize() .with_context(|| format!("Could not find package root: `{}`", root.display()))?; let manifest_path = root.join("package.json"); let manifest = PackageManifest::load(&manifest_path)?; - - let (entry_point, target) = if let Some(entry_point) = Package::find_source_file(&root, "lib") { - (entry_point, Target::Lib) - } else if let Some(entry_point) = Package::find_source_file(&root, "main") { - (entry_point, Target::Script) - } else if let Some(entry_point) = Package::find_source_file(&root, "index") { - (entry_point, Target::Site) - } else { - bail!( - "Could not find entry point to package in directory: `{}`", - root.display() - ) - }; - - Self::from_parts(root, manifest, index, entry_point, target) + let target = Self::infer_target(&root, &manifest)?; + Self::from_parts(root, manifest, index, target) } pub fn start_process(