From becd234ac6e590552fc4a570b979435b2779e335 Mon Sep 17 00:00:00 2001 From: Miguel Ramos Date: Fri, 19 Jul 2024 16:18:07 +0100 Subject: [PATCH 1/3] feat: changes implementation --- Cargo.toml | 10 ++++-- src/lib.rs | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ca75434..d50bff9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ authors = ["Miguel Ramos "] edition = "2021" name = "websublime_workspace-tools" -version = "0.7.3" +version = "0.7.4" exclude = ["tests/*", "examples/*", "node_modules/*", "target/*"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -12,8 +12,12 @@ crate-type = ["cdylib"] [dependencies] napi-derive = "2.16.9" -napi = { version = "2.16.8", default-features = false, features = ["napi9", "serde-json", "tokio_rt"] } -workspace-node-tools = { version = "1.0.6", features = ["napi", "napi-derive"] } +napi = { version = "2.16.8", default-features = false, features = [ + "napi9", + "serde-json", + "tokio_rt", +] } +workspace-node-tools = { version = "1.0.7", features = ["napi", "napi-derive"] } [build-dependencies] napi-build = "2" diff --git a/src/lib.rs b/src/lib.rs index ccbe4c9..de36f3b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,10 @@ use std::path::{Path, PathBuf}; use workspace_node_tools::bumps::{get_bumps, BumpOptions, BumpPackage}; +use workspace_node_tools::changes::{ + add_change, change_exist, get_change, init_changes, remove_change, Change, ChangesFileData, + ChangesOptions, +}; use workspace_node_tools::conventional::{ get_conventional_for_package, ConventionalPackage, ConventionalPackageOptions, }; @@ -429,3 +433,89 @@ pub fn js_get_conventional_for_package( pub fn js_get_bumps(options: BumpOptions) -> Vec { get_bumps(options) } + +/// Init changes +/// +/// # Examples +/// +/// ``` +/// const { initChanges } = require('workspace-node-tools'); +/// const changes = initChanges(process.cwd(), ChangesOptions{}); +/// ``` +/// +/// @param cwd - The root path to start searching from +#[napi(js_name = "initChanges")] +pub fn js_init_changes( + cwd: Option, + change_options: Option, +) -> ChangesFileData { + init_changes(cwd, &change_options) +} + +/// Add change +/// +/// # Examples +/// +/// ``` +/// const { addChange } = require('workspace-node-tools'); +/// addChange(Change{}, process.cwd()); +/// ``` +/// +/// @param change - The change to add +/// @param cwd - The root path to start searching from +#[napi(js_name = "addChange")] +pub fn js_add_change(change: Change, cwd: Option) -> bool { + add_change(&change, cwd) +} + +/// Remove change +/// +/// # Examples +/// +/// ``` +/// const { removeChange } = require('workspace-node-tools'); +/// removeChange("branch-name", process.cwd()); +/// ``` +/// +/// @param branch_name - The branch name to remove +/// @param cwd - The root path to start searching from +#[napi(js_name = "removeChange")] +pub fn js_remove_change(branch_name: String, cwd: Option) -> bool { + remove_change(branch_name, cwd) +} + +/// Change exist +/// +/// # Examples +/// +/// ``` +/// const { changeExist } = require('workspace-node-tools'); +/// const exist = changeExist("branch-name", process.cwd()); +/// ``` +/// +/// @param branch_name - The branch name to check +/// @param cwd - The root path to start searching from +#[napi(js_name = "changeExist")] +pub fn js_change_exist(branch_name: String, cwd: Option) -> bool { + change_exist(branch_name, cwd) +} + +/// Get change +/// +/// # Examples +/// +/// ``` +/// const { getChange } = require('workspace-node-tools'); +/// const changes = getChange("branch-name", process.cwd()); +/// ``` +/// +/// @param branch_name - The branch name to get +/// @param cwd - The root path to start searching from +#[napi(js_name = "getChange")] +pub fn js_get_change(branch_name: String, cwd: Option) -> Vec { + get_change(branch_name, cwd) +} + +/*pub fn js_get_changes(cwd: Option) -> ChangesData { +get_changes(cwd) +}*/ From 3b725b2d657098e5923466cd5a2668a183691eca Mon Sep 17 00:00:00 2001 From: Miguel Ramos Date: Fri, 19 Jul 2024 16:49:54 +0100 Subject: [PATCH 2/3] feat: getChanges and types --- Cargo.toml | 2 +- index.d.ts | 169 +++++++++++++++++++++++++++++++++++++++++++++++++---- index.js | 6 ++ src/lib.rs | 21 +++++-- 4 files changed, 181 insertions(+), 17 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d50bff9..1fa0149 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,7 @@ napi = { version = "2.16.8", default-features = false, features = [ "serde-json", "tokio_rt", ] } -workspace-node-tools = { version = "1.0.7", features = ["napi", "napi-derive"] } +workspace-node-tools = { version = "1.0.8", features = ["napi", "napi-derive"] } [build-dependencies] napi-build = "2" diff --git a/index.d.ts b/index.d.ts index ac46250..d4c3084 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,10 +1,25 @@ /* auto-generated by NAPI-RS */ /* eslint-disable */ +/** + * Add change + * + * # Examples + * + * ``` + * const { addChange } = require('workspace-node-tools'); + * addChange(Change{}, process.cwd()); + * ``` + * + * @param change - The change to add + * @param cwd - The root path to start searching from + */ +export declare function addChange(change: Change, cwd?: string | undefined | null): boolean + export enum Bump { Major = 'Major', Minor = 'Minor', Patch = 'Patch', - Snapshot = 'Snapshot' + Snapshot = 'Snapshot', } export interface BumpOptions { @@ -22,6 +37,44 @@ export interface BumpPackage { conventional: ConventionalPackage } +export interface Change { + package: string + releaseAs: Bump + deploy: Array +} + +export type ChangesData = { + [key: string]: Array +} + +/** + * Change exist + * + * # Examples + * + * ``` + * const { changeExist } = require('workspace-node-tools'); + * const exist = changeExist("branch-name", process.cwd()); + * ``` + * + * @param branch_name - The branch name to check + * @param cwd - The root path to start searching from + */ +export declare function changeExist(branchName: string, cwd?: string | undefined | null): boolean + +export interface Changes { + changes: ChangesData +} + +export interface ChangesFileData { + message?: string + changes: ChangesData +} + +export interface ChangesOptions { + message?: string +} + export interface Commit { hash: string authorName: string @@ -70,7 +123,11 @@ export declare function detectPackageManager(root: string): PackageManager | nul * @param branch - The branch to compare against * @param cwd - The root path to start searching from */ -export declare function getAllFilesChangedSinceBranch(packageInfo: Array, branch: string, cwd?: string | undefined | null): Array +export declare function getAllFilesChangedSinceBranch( + packageInfo: Array, + branch: string, + cwd?: string | undefined | null, +): Array /** * Get bumps @@ -86,6 +143,21 @@ export declare function getAllFilesChangedSinceBranch(packageInfo: Array +/** + * Get change + * + * # Examples + * + * ``` + * const { getChange } = require('workspace-node-tools'); + * const changes = getChange("branch-name", process.cwd()); + * ``` + * + * @param branch_name - The branch name to get + * @param cwd - The root path to start searching from + */ +export declare function getChange(branchName: string, cwd?: string | undefined | null): Array + /** * Get changed packages * @@ -99,7 +171,24 @@ export declare function getBumps(options: BumpOptions): Array * @param sha - The commit sha to compare against (normally main branch) * @param cwd - The root path to start searching from */ -export declare function getChangedPackages(sha?: string | undefined | null, cwd?: string | undefined | null): Array +export declare function getChangedPackages( + sha?: string | undefined | null, + cwd?: string | undefined | null, +): Array + +/** + * Get changes + * + * # Examples + * + * ``` + * const { getChanges } = require('workspace-node-tools'); + * const changes = getChanges(process.cwd()); + * ``` + * + * @param cwd - The root path to start searching from + */ +export declare function getChanges(cwd?: string | undefined | null): Changes /** * Get commits since a commit id @@ -115,7 +204,11 @@ export declare function getChangedPackages(sha?: string | undefined | null, cwd? * @param since - The commit id (accepts branch, tag) * @param relative - The relative path to search from */ -export declare function getCommitsSince(cwd?: string | undefined | null, since?: string | undefined | null, relative?: string | undefined | null): Array +export declare function getCommitsSince( + cwd?: string | undefined | null, + since?: string | undefined | null, + relative?: string | undefined | null, +): Array /** * Get the conventional for a package @@ -132,7 +225,12 @@ export declare function getCommitsSince(cwd?: string | undefined | null, since?: * @param cwd - The root path to start searching from * @param conventional_options - The conventional options */ -export declare function getConventionalForPackage(packageInfo: PackageInfo, noFetchAll?: boolean | undefined | null, cwd?: string | undefined | null, conventionalOptions?: ConventionalPackageOptions | undefined | null): ConventionalPackage +export declare function getConventionalForPackage( + packageInfo: PackageInfo, + noFetchAll?: boolean | undefined | null, + cwd?: string | undefined | null, + conventionalOptions?: ConventionalPackageOptions | undefined | null, +): ConventionalPackage /** * Get the defined package manager @@ -176,7 +274,10 @@ export declare function getDivergedCommit(refer: string, cwd?: string | undefine * @param package_info - The list of package info * @param cwd - The root path to start searching from */ -export declare function getLastKnownPublishTagInfoForAllPackages(packageInfo: Array, cwd?: string | undefined | null): Array +export declare function getLastKnownPublishTagInfoForAllPackages( + packageInfo: Array, + cwd?: string | undefined | null, +): Array /** * Get the last known publish tag info for a package @@ -191,7 +292,10 @@ export declare function getLastKnownPublishTagInfoForAllPackages(packageInfo: Ar * @param package_info - The package info * @param cwd - The root path to start searching from */ -export declare function getLastKnownPublishTagInfoForPackage(packageInfo: PackageInfo, cwd?: string | undefined | null): PublishTagInfo | null +export declare function getLastKnownPublishTagInfoForPackage( + packageInfo: PackageInfo, + cwd?: string | undefined | null, +): PublishTagInfo | null /** * Get packages available in the monorepo @@ -234,7 +338,10 @@ export declare function getProjectRootPath(root?: string | undefined | null): st * @param cwd - The root path to start searching from * @param local - Fetch local tags */ -export declare function getRemoteOrLocalTags(cwd?: string | undefined | null, local?: boolean | undefined | null): Array +export declare function getRemoteOrLocalTags( + cwd?: string | undefined | null, + local?: boolean | undefined | null, +): Array /** * Get all files changed since a commit id @@ -266,7 +373,12 @@ export declare function gitAllFilesChangedSinceSha(sha: string, cwd?: string | u * @param footer - The commit footer * @param cwd - The root path to start searching from */ -export declare function gitCommit(message: string, body?: string | undefined | null, footer?: string | undefined | null, cwd?: string | undefined | null): boolean +export declare function gitCommit( + message: string, + body?: string | undefined | null, + footer?: string | undefined | null, + cwd?: string | undefined | null, +): boolean /** * Get the branch name from a commit id @@ -383,7 +495,28 @@ export declare function gitPush(cwd?: string | undefined | null, followTags?: bo * @param message - The tag message * @param cwd - The root path to start searching from */ -export declare function gitTag(tag: string, message?: string | undefined | null, cwd?: string | undefined | null): boolean +export declare function gitTag( + tag: string, + message?: string | undefined | null, + cwd?: string | undefined | null, +): boolean + +/** + * Init changes + * + * # Examples + * + * ``` + * const { initChanges } = require('workspace-node-tools'); + * const changes = initChanges(process.cwd(), ChangesOptions{}); + * ``` + * + * @param cwd - The root path to start searching from + */ +export declare function initChanges( + cwd?: string | undefined | null, + changeOptions?: ChangesOptions | undefined | null, +): ChangesFileData /** * Check if the workdir is unclean @@ -417,7 +550,7 @@ export enum PackageManager { Npm = 'Npm', Yarn = 'Yarn', Pnpm = 'Pnpm', - Bun = 'Bun' + Bun = 'Bun', } export interface PackageRepositoryInfo { @@ -437,3 +570,17 @@ export interface RemoteTags { tag: string } +/** + * Remove change + * + * # Examples + * + * ``` + * const { removeChange } = require('workspace-node-tools'); + * removeChange("branch-name", process.cwd()); + * ``` + * + * @param branch_name - The branch name to remove + * @param cwd - The root path to start searching from + */ +export declare function removeChange(branchName: string, cwd?: string | undefined | null): boolean diff --git a/index.js b/index.js index ee35380..8f0a6e6 100644 --- a/index.js +++ b/index.js @@ -361,11 +361,15 @@ if (!nativeBinding) { throw new Error(`Failed to load native binding`) } +module.exports.addChange = nativeBinding.addChange module.exports.Bump = nativeBinding.Bump +module.exports.changeExist = nativeBinding.changeExist module.exports.detectPackageManager = nativeBinding.detectPackageManager module.exports.getAllFilesChangedSinceBranch = nativeBinding.getAllFilesChangedSinceBranch module.exports.getBumps = nativeBinding.getBumps +module.exports.getChange = nativeBinding.getChange module.exports.getChangedPackages = nativeBinding.getChangedPackages +module.exports.getChanges = nativeBinding.getChanges module.exports.getCommitsSince = nativeBinding.getCommitsSince module.exports.getConventionalForPackage = nativeBinding.getConventionalForPackage module.exports.getDefinedPackageManager = nativeBinding.getDefinedPackageManager @@ -385,5 +389,7 @@ module.exports.gitFirstSha = nativeBinding.gitFirstSha module.exports.gitPreviousSha = nativeBinding.gitPreviousSha module.exports.gitPush = nativeBinding.gitPush module.exports.gitTag = nativeBinding.gitTag +module.exports.initChanges = nativeBinding.initChanges module.exports.isWorkdirUnclean = nativeBinding.isWorkdirUnclean module.exports.PackageManager = nativeBinding.PackageManager +module.exports.removeChange = nativeBinding.removeChange diff --git a/src/lib.rs b/src/lib.rs index de36f3b..2c9a2ee 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,8 +4,8 @@ use std::path::{Path, PathBuf}; use workspace_node_tools::bumps::{get_bumps, BumpOptions, BumpPackage}; use workspace_node_tools::changes::{ - add_change, change_exist, get_change, init_changes, remove_change, Change, ChangesFileData, - ChangesOptions, + add_change, change_exist, get_change, get_changes, init_changes, remove_change, Change, Changes, + ChangesFileData, ChangesOptions, }; use workspace_node_tools::conventional::{ get_conventional_for_package, ConventionalPackage, ConventionalPackageOptions, @@ -516,6 +516,17 @@ pub fn js_get_change(branch_name: String, cwd: Option) -> Vec { get_change(branch_name, cwd) } -/*pub fn js_get_changes(cwd: Option) -> ChangesData { -get_changes(cwd) -}*/ +/// Get changes +/// +/// # Examples +/// +/// ``` +/// const { getChanges } = require('workspace-node-tools'); +/// const changes = getChanges(process.cwd()); +/// ``` +/// +/// @param cwd - The root path to start searching from +#[napi(js_name = "getChanges")] +pub fn js_get_changes(cwd: Option) -> Changes { + get_changes(cwd) +} From 86dc3c6e2a9127a0182690588e5b972a9c7ae5ec Mon Sep 17 00:00:00 2001 From: Miguel Ramos Date: Fri, 19 Jul 2024 17:08:03 +0100 Subject: [PATCH 3/3] chore: update readme api documentation --- README.md | 59 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 32 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 8be6e45..82af458 100644 --- a/README.md +++ b/README.md @@ -16,33 +16,38 @@ This package offer a set of functions to retrieve information about the monorepo ## API - -| ###Function | ###Description | -|---|---| -| `getProjectRootPath(root?: string): string or undefined` | Get the root path of the project. | -| `getDefinedPackageManager(root?: string): string or undefined` | Get the package manager defined in the project. | -| `detectPackageManager(root: string): PackageManager or undefined` | Detect the package manager defined in the project. | -| `getPackages(cwd?: string): Array` | Get the list of packages in the monorepo. | -| `getChangedPackages(sha?: string, cwd: string): Array` | Get the list of packages that have changed since the given sha ('main'). | -| `gitFetchAll(cwd?: string, fetch_tags?: boolean): boolean` | Execute a `fetch` command to get the latest changes from the remote repository. You can also retrieve tags | -| `gitCommit(message: string, body?: string, footer?: string cwd?: string): boolean` | Commit the changes. | -| `gitTag(tag: string, message?: string, cwd?: string): boolean` | Tag the repository with the given tag. | -| `gitPush(cwd?: string, follow_tags?: boolean): boolean` | Push the changes to the remote repository, including optional tags. | -| `gitCurrentBranch(cwd?: string): string or undefined` | Get the current branch name. | -| `gitCurrentSha(cwd?: string): string` | Get's the current commit id. | -| `gitPreviousSha(cwd?: string): string or undefined` | Get's the previous commit id. | -| `gitFirstSha(cwd?: string, branch?: string): string or undefined` | Get's the first commit id in a branch. Compare is done between branch..Head, and it should be used as main..HEAD | -| `isWorkdirUnclean(cwd?: string): boolean` | Check if the workdir is unclean (uncommited changes). | -| `gitCommitBranchName(sha: string, cwd?: string): string or undefined` | Get the branch name for the commit id. | -| `gitAllFilesChangedSinceSha(sha: string, cwd?: string): Array` | Get all files changed sinc branch, commit id etc. | -| `getDivergedCommit(sha: string, cwd?: string): string or undefined` | Get the diverged commit from the given sha (main). | -| `getCommitsSince(cwd?: string, since?: string, relative?: string): Array` | Get the commits since the given sha (main) for a particular package. | -| `getAllFilesChangedSinceBranch(package_info: Array, branch: string, cwd?: string): Array` | Get all the files changed for a branch (main). | -| `getLastKnownPublishTagInfoForPackage(package_info: PackageInfo, cwd?: string): Array` | Get the last known publish tag info for a particular package. | -| `getLastKnownPublishTagInfoForAllPackages(package_info: Array, cwd?: string): Array` | Get the last known publish tag info for all packages. | -| `getRemoteOrLocalTags(cwd?: string, local?: boolean): Array` | Get all the tags in the remote or local repository. | -| `getConventionalForPackage(package_info: PackageInfo, no_fetch_all?: boolean cwd?: string, conventional_options?: ConventionalPackageOptions): ConventionalPackage` | Get the conventional commits for a particular package, changelog output and package info. | -| `getBumps(options: BumpOptions): Array` | Output bumps version for packages and it's dependencies | +| Function | Description | +| ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- | +| `getProjectRootPath(root?: string): string or undefined` | Get the root path of the project. | +| `getDefinedPackageManager(root?: string): string or undefined` | Get the package manager defined in the project. | +| `detectPackageManager(root: string): PackageManager or undefined` | Detect the package manager defined in the project. | +| `getPackages(cwd?: string): Array` | Get the list of packages in the monorepo. | +| `getChangedPackages(sha?: string, cwd: string): Array` | Get the list of packages that have changed since the given sha ('main'). | +| `gitFetchAll(cwd?: string, fetch_tags?: boolean): boolean` | Execute a `fetch` command to get the latest changes from the remote repository. You can also retrieve tags | +| `gitCommit(message: string, body?: string, footer?: string cwd?: string): boolean` | Commit the changes. | +| `gitTag(tag: string, message?: string, cwd?: string): boolean` | Tag the repository with the given tag. | +| `gitPush(cwd?: string, follow_tags?: boolean): boolean` | Push the changes to the remote repository, including optional tags. | +| `gitCurrentBranch(cwd?: string): string or undefined` | Get the current branch name. | +| `gitCurrentSha(cwd?: string): string` | Get's the current commit id. | +| `gitPreviousSha(cwd?: string): string or undefined` | Get's the previous commit id. | +| `gitFirstSha(cwd?: string, branch?: string): string or undefined` | Get's the first commit id in a branch. Compare is done between branch..Head, and it should be used as main..HEAD | +| `isWorkdirUnclean(cwd?: string): boolean` | Check if the workdir is unclean (uncommited changes). | +| `gitCommitBranchName(sha: string, cwd?: string): string or undefined` | Get the branch name for the commit id. | +| `gitAllFilesChangedSinceSha(sha: string, cwd?: string): Array` | Get all files changed sinc branch, commit id etc. | +| `getDivergedCommit(sha: string, cwd?: string): string or undefined` | Get the diverged commit from the given sha (main). | +| `getCommitsSince(cwd?: string, since?: string, relative?: string): Array` | Get the commits since the given sha (main) for a particular package. | +| `getAllFilesChangedSinceBranch(package_info: Array, branch: string, cwd?: string): Array` | Get all the files changed for a branch (main). | +| `getLastKnownPublishTagInfoForPackage(package_info: PackageInfo, cwd?: string): Array` | Get the last known publish tag info for a particular package. | +| `getLastKnownPublishTagInfoForAllPackages(package_info: Array, cwd?: string): Array` | Get the last known publish tag info for all packages. | +| `getRemoteOrLocalTags(cwd?: string, local?: boolean): Array` | Get all the tags in the remote or local repository. | +| `getConventionalForPackage(package_info: PackageInfo, no_fetch_all?: boolean cwd?: string, conventional_options?: ConventionalPackageOptions): ConventionalPackage` | Get the conventional commits for a particular package, changelog output and package info. | +| `getBumps(options: BumpOptions): Array` | Output bumps version for packages and it's dependencies | +| `initChanges(cwd?: string, change_options?: ChangesOptions): ChangesFileData` | Creat changes file or retrieve is data if already exist | +| `add_change(change: Change, cwd?: string): boolean` | Adds a new change to the change file | +| `remove_change(branch_name: String, cwd?: string): boolean` | Removes the change from the changes files. | +| `change_exist(branch_name: string, cwd?: string): boolean` | Check if change already exist. | +| `get_change(branch_name: string, cwd?: string): Array` | Get the list of changes for the branch. | +| `get_changes(cwd?: string): Changes` | Get all changes. | ## Develop requirements