Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: handle more 'workspace:*' specifiers in package.json #7

Merged
merged 2 commits into from
Nov 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 53 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,22 @@ pub enum PackageJsonDepValueParseError {
Unsupported { scheme: String },
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum PackageJsonDepWorkspaceReq {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't love the name, any suggestions?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems ok.

/// "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 @@ -344,8 +356,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 @@ -569,7 +587,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 @@ -602,11 +626,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