Skip to content

Commit

Permalink
fix: handle more 'workspace:*' specifiers in package.json (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju authored Nov 25, 2024
1 parent 4c8c9c9 commit f5cbda3
Showing 1 changed file with 53 additions and 7 deletions.
60 changes: 53 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,22 @@ pub enum PackageJsonDepValueParseError {
Unsupported { scheme: String },
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum PackageJsonDepWorkspaceReq {
/// "workspace:~"
Tilde,

/// "workspace:^"
Caret,

/// "workspace:x.y.z", "workspace:*", "workspace:^x.y.z"
VersionReq(VersionReq),
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum PackageJsonDepValue {
Req(PackageReq),
Workspace(VersionReq),
Workspace(PackageJsonDepWorkspaceReq),
}

pub type PackageJsonDepsMap =
Expand Down Expand Up @@ -351,8 +363,14 @@ impl PackageJson {
value: &str,
) -> Result<PackageJsonDepValue, PackageJsonDepValueParseError> {
if let Some(workspace_key) = value.strip_prefix("workspace:") {
let version_req = VersionReq::parse_from_npm(workspace_key)?;
return Ok(PackageJsonDepValue::Workspace(version_req));
let workspace_req = match workspace_key {
"~" => PackageJsonDepWorkspaceReq::Tilde,
"^" => PackageJsonDepWorkspaceReq::Caret,
_ => PackageJsonDepWorkspaceReq::VersionReq(
VersionReq::parse_from_npm(workspace_key)?,
),
};
return Ok(PackageJsonDepValue::Workspace(workspace_req));
}
if value.starts_with("file:")
|| value.starts_with("git:")
Expand Down Expand Up @@ -565,7 +583,13 @@ mod test {
.unwrap();
package_json.dependencies = Some(IndexMap::from([
("test".to_string(), "1".to_string()),
("work-test".to_string(), "workspace:1.1.1".to_string()),
(
"work-test-version-req".to_string(),
"workspace:1.1.1".to_string(),
),
("work-test-star".to_string(), "workspace:*".to_string()),
("work-test-tilde".to_string(), "workspace:~".to_string()),
("work-test-caret".to_string(), "workspace:^".to_string()),
("file-test".to_string(), "file:something".to_string()),
("git-test".to_string(), "git:something".to_string()),
("http-test".to_string(), "http://something".to_string()),
Expand Down Expand Up @@ -598,11 +622,33 @@ mod test {
))
),
(
"work-test".to_string(),
"work-test-version-req".to_string(),
Ok(PackageJsonDepValue::Workspace(
VersionReq::parse_from_npm("1.1.1").unwrap()
PackageJsonDepWorkspaceReq::VersionReq(
VersionReq::parse_from_npm("1.1.1").unwrap()
)
))
)
),
(
"work-test-star".to_string(),
Ok(PackageJsonDepValue::Workspace(
PackageJsonDepWorkspaceReq::VersionReq(
VersionReq::parse_from_npm("*").unwrap()
)
))
),
(
"work-test-tilde".to_string(),
Ok(PackageJsonDepValue::Workspace(
PackageJsonDepWorkspaceReq::Tilde
))
),
(
"work-test-caret".to_string(),
Ok(PackageJsonDepValue::Workspace(
PackageJsonDepWorkspaceReq::Caret
))
),
])
);
}
Expand Down

0 comments on commit f5cbda3

Please sign in to comment.