Skip to content

Commit

Permalink
feat(task): helper method for getting task names (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Nov 20, 2024
1 parent a8831ad commit 7a02e01
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/workspace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1708,6 +1708,29 @@ impl WorkspaceMemberTasksConfig {
.unwrap_or(true)
}

pub fn task_names(&self) -> impl Iterator<Item = &str> {
self
.deno_json
.as_ref()
.into_iter()
.flat_map(|d| d.tasks.keys())
.chain(
self
.package_json
.as_ref()
.into_iter()
.flat_map(|d| d.tasks.keys())
.filter(|pkg_json_key| {
self
.deno_json
.as_ref()
.map(|d| !d.tasks.contains_key(pkg_json_key.as_str()))
.unwrap_or(true)
}),
)
.map(|s| s.as_str())
}

pub fn tasks_count(&self) -> usize {
self.deno_json.as_ref().map(|d| d.tasks.len()).unwrap_or(0)
+ self
Expand Down Expand Up @@ -1754,6 +1777,28 @@ impl WorkspaceTasksConfig {
}
}

pub fn task_names(&self) -> impl Iterator<Item = &str> {
self
.member
.as_ref()
.into_iter()
.flat_map(|r| r.task_names())
.chain(
self
.root
.as_ref()
.into_iter()
.flat_map(|m| m.task_names())
.filter(|root_key| {
self
.member
.as_ref()
.map(|m| m.task(root_key).is_none())
.unwrap_or(true)
}),
)
}

pub fn task(&self, name: &str) -> Option<(&Url, TaskOrScript)> {
self
.member
Expand Down Expand Up @@ -2191,6 +2236,10 @@ mod test {
member: root.clone(),
}
);
assert_eq!(
tasks_config.task_names().collect::<Vec<_>>(),
["hi", "overwrite"]
);
}
// member
{
Expand All @@ -2215,6 +2264,10 @@ mod test {
}),
}
);
assert_eq!(
tasks_config.task_names().collect::<Vec<_>>(),
["overwrite", "bye", "hi"]
);
}
// pkg json
{
Expand All @@ -2240,6 +2293,10 @@ mod test {
})
}
);
assert_eq!(
tasks_config.task_names().collect::<Vec<_>>(),
["hi", "overwrite", "script"]
);
}
}

Expand Down

0 comments on commit 7a02e01

Please sign in to comment.