Skip to content

Commit

Permalink
improve root handling
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinMalfait committed Jul 8, 2023
1 parent 2a9c0af commit 504718f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 12 deletions.
20 changes: 12 additions & 8 deletions src/changelog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,14 @@ impl Changelog {
!section_name.eq_ignore_ascii_case(&self.unreleased_heading(scope))
} else {
match scope {
Some(scope) => section_name.to_lowercase().starts_with(&format!(
"[{}@v{}]",
scope.name(),
name.to_lowercase()
)),
None => section_name
Some(scope) if !scope.is_root() => {
section_name.to_lowercase().starts_with(&format!(
"[{}@v{}]",
scope.name(),
name.to_lowercase()
))
}
_ => section_name
.to_lowercase()
.starts_with(&format!("[{}]", name.to_lowercase())),
}
Expand Down Expand Up @@ -389,8 +391,10 @@ impl Changelog {
link.clone().replace(
"HEAD",
&match scope {
Some(scope) => format!("{}@v{}", scope.name(), version),
None => format!("v{}", version),
Some(scope) if !scope.is_root() => {
format!("{}@v{}", scope.name(), version)
}
_ => format!("v{}", version),
},
),
);
Expand Down
6 changes: 4 additions & 2 deletions src/github/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ impl Commit {

let long_hash = git.long_hash(maybe_hash)?;
let short_hash = git.short_hash(maybe_hash)?;
let title = git.commit_message(maybe_hash)?;
let mut title = git.commit_message(maybe_hash)?;
// Uppercase first letter of `title`
title.replace_range(..1, &title[..1].to_uppercase());

Ok(Self {
hash: long_hash,
short_hash,
short_hash: short_hash[0..7].to_string(),
title,
repo,
})
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ async fn main() -> Result<()> {
.items(
&options
.iter()
.map(|package| package.name())
.map(|package| package.display_name())
.collect::<Vec<_>>(),
)
.clear(true)
Expand Down
31 changes: 30 additions & 1 deletion src/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ pub struct PackageJSON {
// Meta data
#[serde(skip)]
pwd: PathBuf,
#[serde(skip)]
is_root: bool,

// Actual PackageJSON data
name: String,
Expand All @@ -153,6 +155,29 @@ impl PackageJSON {
.map_err(|e| eyre!(e))
}

pub fn from_root(dir: &Path) -> Result<Self> {
let package_json_path = dir.join("package.json");
let contents = std::fs::read_to_string(package_json_path)?;
serde_json::from_str::<Self>(&contents)
.map(|mut pkg| {
pkg.pwd = dir.to_path_buf();
pkg
})
.map_err(|e| eyre!(e))
.map(|mut root| {
root.is_root = true;
root
})
}

pub fn display_name(&self) -> String {
if self.is_root {
format!("{} {}", self.name, "(root)".italic().dimmed())
} else {
self.name.to_owned()
}
}

pub fn from_current_directory() -> Result<Self> {
Self::from_directory(&std::env::current_dir()?)
}
Expand All @@ -165,6 +190,10 @@ impl PackageJSON {
&self.name
}

pub fn is_root(&self) -> bool {
self.is_root
}

pub fn version_mut(&mut self) -> &mut SemVer {
&mut self.version
}
Expand All @@ -176,7 +205,7 @@ impl PackageJSON {
pub fn packages(&self) -> Result<Vec<PackageJSON>> {
let base = &self.pwd;

let mut packages: Vec<PackageJSON> = vec![];
let mut packages: Vec<PackageJSON> = vec![PackageJSON::from_root(base)?];

if let Some(workspaces) = &self.workspaces {
for workspace_glob in workspaces {
Expand Down

0 comments on commit 504718f

Please sign in to comment.