From 1d366167c61dbf545dd7f22f61d6cd9f3b9b13e4 Mon Sep 17 00:00:00 2001 From: Miguel Ramos Date: Mon, 22 Jul 2024 00:02:00 +0100 Subject: [PATCH] feat: change file exist check --- src/bumps.rs | 9 +++++++++ src/changes.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ src/conventional.rs | 5 +++++ src/lib.rs | 1 + 4 files changed, 58 insertions(+) diff --git a/src/bumps.rs b/src/bumps.rs index 38f4a0a0..ab38e2db 100644 --- a/src/bumps.rs +++ b/src/bumps.rs @@ -30,6 +30,7 @@ pub enum Bump { #[cfg(not(feature = "napi"))] #[derive(Debug, Clone, Deserialize, Serialize, Copy, PartialEq)] +/// Enum representing the type of bump to be performed. pub enum Bump { Major, Minor, @@ -50,6 +51,7 @@ pub struct BumpOptions { #[cfg(not(feature = "napi"))] #[derive(Debug, Clone, Deserialize, Serialize, PartialEq)] +/// Struct representing the options for the bump operation. pub struct BumpOptions { pub packages: Vec, pub release_as: Bump, @@ -60,6 +62,7 @@ pub struct BumpOptions { #[cfg(not(feature = "napi"))] #[derive(Debug, Clone, Deserialize, Serialize)] +/// Struct representing the bump package. pub struct BumpPackage { pub from: String, pub to: String, @@ -78,6 +81,7 @@ pub struct BumpPackage { } impl Bump { + /// Bumps the version of the package to major. fn bump_major(version: String) -> SemVersion { let mut sem_version = SemVersion::parse(&version).unwrap(); sem_version.major += 1; @@ -88,6 +92,7 @@ impl Bump { sem_version } + /// Bumps the version of the package to minor. fn bump_minor(version: String) -> SemVersion { let mut sem_version = SemVersion::parse(&version).unwrap(); sem_version.minor += 1; @@ -97,6 +102,7 @@ impl Bump { sem_version } + /// Bumps the version of the package to patch. fn bump_patch(version: String) -> SemVersion { let mut sem_version = SemVersion::parse(&version).unwrap(); sem_version.patch += 1; @@ -105,6 +111,7 @@ impl Bump { sem_version } + /// Bumps the version of the package to snapshot appending the sha to the version. fn bump_snapshot(version: String) -> SemVersion { let sha = git_current_sha(None); let alpha = format!("alpha.{}", sha); @@ -116,6 +123,7 @@ impl Bump { } } +/// Bumps the version of dev-dependencies and dependencies. pub fn sync_bumps(bump_package: &BumpPackage, cwd: Option) -> Vec { get_packages(cwd) .iter() @@ -139,6 +147,7 @@ pub fn sync_bumps(bump_package: &BumpPackage, cwd: Option) -> Vec>() } +/// Get bumps version of the package. pub fn get_bumps(options: BumpOptions) -> Vec { let ref root = match options.cwd { Some(ref dir) => get_project_root_path(Some(PathBuf::from(dir))).unwrap(), diff --git a/src/changes.rs b/src/changes.rs index 7ca2e29f..a5352474 100644 --- a/src/changes.rs +++ b/src/changes.rs @@ -6,6 +6,7 @@ //! The changes are stored in a `.changes.json` file in the root of the project. //! //! # Example +//! ```json //! { //! "message": "chore(release): release new version", //! "changes": { @@ -16,6 +17,7 @@ //! }], //! } //!} +//!``` use serde::{Deserialize, Serialize}; use std::io::BufWriter; use std::{ @@ -30,10 +32,12 @@ use crate::bumps::Bump; use super::git::git_current_branch; use super::paths::get_project_root_path; +/// Dynamic data structure to store changes type ChangesData = BTreeMap>; #[cfg(not(feature = "napi"))] #[derive(Debug, Clone, Deserialize, Serialize, PartialEq)] +/// Options to initialize the changes file pub struct ChangesOptions { pub message: Option, } @@ -47,6 +51,7 @@ pub struct ChangesOptions { #[cfg(not(feature = "napi"))] #[derive(Debug, Clone, Deserialize, Serialize, PartialEq)] +/// Data structure to store changes file pub struct ChangesFileData { pub message: Option, pub changes: ChangesData, @@ -62,6 +67,7 @@ pub struct ChangesFileData { #[cfg(not(feature = "napi"))] #[derive(Debug, Clone, Deserialize, Serialize, PartialEq)] +/// Data structure to store changes pub struct Changes { pub changes: ChangesData, } @@ -75,6 +81,7 @@ pub struct Changes { #[cfg(not(feature = "napi"))] #[derive(Debug, Clone, Deserialize, Serialize, PartialEq)] +/// Data structure to store a change pub struct Change { pub package: String, pub release_as: Bump, @@ -90,6 +97,8 @@ pub struct Change { pub deploy: Vec, } +/// Initialize the changes file. If the file does not exist, it will create it with the default message. +/// If the file exists, it will return the content of the file. pub fn init_changes( cwd: Option, change_options: &Option, @@ -131,6 +140,7 @@ pub fn init_changes( } } +/// Add a change to the changes file in the root of the project. pub fn add_change(change: &Change, cwd: Option) -> bool { let ref root = match cwd { Some(ref dir) => get_project_root_path(Some(PathBuf::from(dir))).unwrap(), @@ -182,6 +192,7 @@ pub fn add_change(change: &Change, cwd: Option) -> bool { false } +/// Remove a change from the changes file in the root of the project. pub fn remove_change(branch_name: String, cwd: Option) -> bool { let ref root = match cwd { Some(ref dir) => get_project_root_path(Some(PathBuf::from(dir))).unwrap(), @@ -212,6 +223,7 @@ pub fn remove_change(branch_name: String, cwd: Option) -> bool { false } +/// Get all changes from the changes file in the root of the project. pub fn get_changes(cwd: Option) -> Changes { let ref root = match cwd { Some(ref dir) => get_project_root_path(Some(PathBuf::from(dir))).unwrap(), @@ -237,6 +249,7 @@ pub fn get_changes(cwd: Option) -> Changes { } } +/// Get all changes for a specific branch from the changes file in the root of the project. pub fn get_change(branch: String, cwd: Option) -> Vec { let ref root = match cwd { Some(ref dir) => get_project_root_path(Some(PathBuf::from(dir))).unwrap(), @@ -262,6 +275,7 @@ pub fn get_change(branch: String, cwd: Option) -> Vec { vec![] } +/// Check if a change exists in the changes file in the root of the project. pub fn change_exist(branch: String, cwd: Option) -> bool { let ref root = match cwd { Some(ref dir) => get_project_root_path(Some(PathBuf::from(dir))).unwrap(), @@ -283,6 +297,19 @@ pub fn change_exist(branch: String, cwd: Option) -> bool { false } +/// Check if a changes file exists in the root of the project. +pub fn changes_file_exist(cwd: Option) -> bool { + let ref root = match cwd { + Some(ref dir) => get_project_root_path(Some(PathBuf::from(dir))).unwrap(), + None => get_project_root_path(None).unwrap(), + }; + + let root_path = Path::new(root); + let ref changes_path = root_path.join(String::from(".changes.json")); + + changes_path.exists() +} + #[cfg(test)] mod tests { use super::*; @@ -436,4 +463,20 @@ mod tests { remove_dir_all(&monorepo_dir)?; Ok(()) } + + #[test] + fn test_changes_file_exist() -> Result<(), Box> { + let ref monorepo_dir = create_test_monorepo(&PackageManager::Npm)?; + let project_root = get_project_root_path(Some(monorepo_dir.to_path_buf())); + + let ref root = project_root.unwrap().to_string(); + + let ref changes_path = monorepo_dir.join(String::from(".changes.json")); + let result = changes_file_exist(Some(root.to_string())); + + assert_eq!(result, false); + assert_eq!(changes_path.is_file(), false); + remove_dir_all(&monorepo_dir)?; + Ok(()) + } } diff --git a/src/conventional.rs b/src/conventional.rs index 05b3d339..7fd3a407 100644 --- a/src/conventional.rs +++ b/src/conventional.rs @@ -34,6 +34,7 @@ pub struct ConventionalPackage { #[cfg(not(feature = "napi"))] #[derive(Debug, Clone, Deserialize, Serialize)] +/// A struct that represents a conventional package pub struct ConventionalPackage { pub package_info: PackageInfo, pub conventional_config: Value, @@ -51,11 +52,13 @@ pub struct ConventionalPackageOptions { #[cfg(not(feature = "napi"))] #[derive(Debug, Clone)] +/// A struct that represents options for a conventional package pub struct ConventionalPackageOptions { pub version: Option, pub title: Option, } +/// Process commits for groupint type, extracting data fn process_commits<'a>(commits: &Vec, config: &GitConfig) -> Vec> { commits .iter() @@ -78,6 +81,7 @@ fn process_commits<'a>(commits: &Vec, config: &GitConfig) -> Vec>() } +/// Defines the config for conventional, template usage for changelog fn define_config( owner: String, repo: String, @@ -239,6 +243,7 @@ fn define_config( cliff_config } +/// Generate changelog output fn generate_changelog( commits: &Vec, config: &Config, diff --git a/src/lib.rs b/src/lib.rs index 110e6bed..43dd706a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +#![doc(html_logo_url = "https://avatars.githubusercontent.com/u/2073802?s=200&v=4")] //! # Workspace node tools //! //! This crate provides a set of tools to work with node workspaces.