Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat: split up PackageJsonDeps into dependencies and devDependencies #6

Merged
merged 3 commits into from
Oct 22, 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
98 changes: 64 additions & 34 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,28 @@ pub enum PackageJsonDepValue {
Workspace(VersionReq),
}

pub type PackageJsonDeps =
pub type PackageJsonDepsMap =
IndexMap<String, Result<PackageJsonDepValue, PackageJsonDepValueParseError>>;

#[derive(Debug, Clone)]
pub struct PackageJsonDeps {
pub dependencies: PackageJsonDepsMap,
pub dev_dependencies: PackageJsonDepsMap,
}

impl PackageJsonDeps {
/// Gets a package.json dependency entry by alias.
pub fn get(
&self,
alias: &str,
) -> Option<&Result<PackageJsonDepValue, PackageJsonDepValueParseError>> {
self
.dependencies
.get(alias)
.or_else(|| self.dev_dependencies.get(alias))
}
}

#[derive(Debug, Error)]
pub enum PackageJsonLoadError {
#[error("Failed reading '{}'.", .path.display())]
Expand Down Expand Up @@ -349,28 +368,23 @@ impl PackageJson {
}
}

fn insert_deps(
deps: Option<&IndexMap<String, String>>,
result: &mut PackageJsonDeps,
) {
if let Some(deps) = deps {
for (key, value) in deps {
result
.entry(key.to_string())
.or_insert_with(|| parse_entry(key, value));
}
fn get_map(deps: Option<&IndexMap<String, String>>) -> PackageJsonDepsMap {
let Some(deps) = deps else {
return Default::default();
};
let mut result = IndexMap::with_capacity(deps.len());
for (key, value) in deps {
result
.entry(key.to_string())
.or_insert_with(|| parse_entry(key, value));
}
result
}

let deps = self.dependencies.as_ref();
let dev_deps = self.dev_dependencies.as_ref();
let mut result = IndexMap::new();

// favors the deps over dev_deps
insert_deps(deps, &mut result);
insert_deps(dev_deps, &mut result);

result
PackageJsonDeps {
dependencies: get_map(self.dependencies.as_ref()),
dev_dependencies: get_map(self.dev_dependencies.as_ref()),
}
}
}

Expand Down Expand Up @@ -421,9 +435,11 @@ mod test {
fn get_local_package_json_version_reqs_for_tests(
package_json: &PackageJson,
) -> IndexMap<String, Result<PackageJsonDepValue, String>> {
package_json
.resolve_local_package_json_deps()
let deps = package_json.resolve_local_package_json_deps();
deps
.dependencies
.into_iter()
.chain(deps.dev_dependencies)
.map(|(k, v)| {
(
k,
Expand All @@ -449,31 +465,45 @@ mod test {
]));
package_json.dev_dependencies = Some(IndexMap::from([
("package_b".to_string(), "~2.2".to_string()),
// should be ignored
("other".to_string(), "^3.2".to_string()),
]));
let deps = get_local_package_json_version_reqs_for_tests(&package_json);
let deps = package_json.resolve_local_package_json_deps();
assert_eq!(
deps,
IndexMap::from([
deps
.dependencies
.into_iter()
.map(|d| (d.0, d.1.unwrap()))
.collect::<Vec<_>>(),
Vec::from([
(
"test".to_string(),
Ok(PackageJsonDepValue::Req(
PackageReq::from_str("test@^1.2").unwrap()
))
PackageJsonDepValue::Req(PackageReq::from_str("test@^1.2").unwrap())
),
(
"other".to_string(),
Ok(PackageJsonDepValue::Req(
PackageJsonDepValue::Req(
PackageReq::from_str("package@~1.3").unwrap()
))
)
),
])
);
assert_eq!(
deps
.dev_dependencies
.into_iter()
.map(|d| (d.0, d.1.unwrap()))
.collect::<Vec<_>>(),
Vec::from([
(
"package_b".to_string(),
Ok(PackageJsonDepValue::Req(
PackageJsonDepValue::Req(
PackageReq::from_str("package_b@~2.2").unwrap()
))
)
)
),
(
"other".to_string(),
PackageJsonDepValue::Req(PackageReq::from_str("other@^3.2").unwrap())
),
])
);
}
Expand Down