Skip to content

Commit

Permalink
Add optional "target" key to depot manifest. Fix pnpm directory
Browse files Browse the repository at this point in the history
  • Loading branch information
willcrichton committed Apr 4, 2024
1 parent 8c8f91d commit e9781d3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 22 deletions.
1 change: 1 addition & 0 deletions crates/depot/src/commands/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,7 @@ export default defineConfig(({{ mode }}) => ({{
let mut other: IndexMap<String, Value> = IndexMap::new();
let pkg_config = PackageDepotConfig {
platform: *platform,
target: Some(*target),
no_server: None,
};
other.insert("depot".into(), serde_json::to_value(pkg_config)?);
Expand Down
4 changes: 2 additions & 2 deletions crates/depot/src/commands/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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");
Expand Down
3 changes: 2 additions & 1 deletion crates/depot/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}),*]
Expand Down
44 changes: 25 additions & 19 deletions crates/depot/src/workspace/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down Expand Up @@ -111,6 +114,8 @@ impl FromStr for PackageName {
pub struct PackageDepotConfig {
pub platform: Platform,
#[serde(skip_serializing_if = "Option::is_none")]
pub target: Option<Target>,
#[serde(skip_serializing_if = "Option::is_none")]
pub no_server: Option<bool>,
}

Expand Down Expand Up @@ -145,7 +150,6 @@ pub struct PackageInner {
pub platform: Platform,
pub target: Target,
pub name: PackageName,
pub entry_point: PathBuf,
pub index: PackageIndex,

// Internals
Expand All @@ -171,7 +175,6 @@ impl Package {
root: PathBuf,
manifest: PackageManifest,
index: PackageIndex,
entry_point: PathBuf,
target: Target,
) -> Result<Self> {
let platform = manifest.config.platform;
Expand All @@ -185,7 +188,6 @@ impl Package {
Ok(Package::new(PackageInner {
root: root.to_owned(),
manifest,
entry_point,
target,
platform,
name,
Expand All @@ -195,27 +197,31 @@ impl Package {
}))
}

fn infer_target(root: &Path, manifest: &PackageManifest) -> Result<Target> {
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<Self> {
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(
Expand Down

0 comments on commit e9781d3

Please sign in to comment.