Skip to content

Commit

Permalink
Merge pull request #121 from sliit-foss/feat/commander
Browse files Browse the repository at this point in the history
Feat!: automatic-versioning - integrated commander.js
  • Loading branch information
Akalanka47000 authored Dec 16, 2023
2 parents b3956ce + 8dfbec3 commit cf331a9
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 60 deletions.
3 changes: 2 additions & 1 deletion packages/automatic-versioning/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
},
"dependencies": {
"@actions/exec": "1.1.1",
"@colors/colors": "1.5.0"
"@colors/colors": "1.5.0",
"commander": "11.1.0"
},
"author": "SLIIT FOSS",
"license": "MIT",
Expand Down
27 changes: 13 additions & 14 deletions packages/automatic-versioning/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ yarn bump-version
- Fix - bump patch version
```

## Disable commit <br/>
## Skip commit <br/>

- By default automatic-versioning will commit the newly incremented version to source control. To disable this behavior, add the following to your script: "--no-commit"<br/>
- By default automatic-versioning will commit the newly incremented version to source control. To disable this behavior, add the following to your script: "--skip-commit"<br/>

```bash
npx automatic-versioning --name=<package_name> --no-commit
npx automatic-versioning --name=<package_name> --skip-commit
```

## Disable version bumping for specific commit<br/>
Expand All @@ -107,21 +107,13 @@ yarn bump-version
git commit -m "Feat: some feature --no-bump"
```

## Disable --no-bump commit message edit <br/>

- By default automatic-versioning will edit the commit message in no-bump commits and remove the no-bump part from the commit message. Sometimes such as in the case of monorepos, this can prove to be a problem. To disable this behavior, add the following to your script: "--no-commit-edit"<br/>

```bash
npx automatic-versioning --name=<package_name> --no-commit-edit
```

## Custom app directory to run incrementing script<br/>
## Custom app directory to run versioning script<br/>

```bash
npx automatic-versioning --name=<package_name> --rootDir=<custom_dir>
npx automatic-versioning --name=<package_name> --root=<custom_dir>
```

## Recursively search commit history to find version bump trigger<br/>
## Recursively search commit history to find a supporting prefix to trigger a version bump<br/>

```bash
npx automatic-versioning --name=<package_name> --recursive
Expand Down Expand Up @@ -152,3 +144,10 @@ yarn bump-version
```bash
npx automatic-versioning --name=<package_name> --ignore-prefixes=ci,docs
```

---

## Migration from V1 to V2

- `--no-commit-edit` option has been removed as commits no longer cause problems with the versioning script
- `--no-commit` option has been renamed to `--skip-commit`
77 changes: 45 additions & 32 deletions packages/automatic-versioning/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,59 @@

/* eslint-disable no-console */

import path from "path";

import defaultRunner from "./types/default";
import tagBasedRunner from "./types/tag-based";
import { default as path } from "path";
import { program, Option } from "commander";
import { version } from "../package.json";
import { defaultRunner, tagBasedRunner } from "./types";

require("@colors/colors");

const args = process.argv.slice(2);
program
.name("automatic-versioning")
.description("CLI for automated commit based semantic versioning with excellent support for monorepositories")
.version(version);

program
.addOption(
new Option("-n, --name <string>", "name of the library being versioned").default("@sliit-foss/automatic-versioning")
)
.option("-r, --root <string>", "root directory to use when executing the script")
.option("--skip-commit", "do not commit the incremented version")
.option("--tag-based", "run versioning based on git tags");

[
new Option("--recursive", "recursively search for a matching commit prefix"),
new Option("--prerelease-tag <string>", "prerelease tag to use when running on a prerelease branch"),
new Option("--prerelease-branch <string>", "run prereleases on this branch"),
new Option(
"--ignore-prefixes <string>",
"comma separated list of commit prefixes to ignore when searching for a matching prefix"
)
].forEach((option) => program.addOption(option.conflicts("tagBased")));

const opts = program.parse().opts();

const defaultRootDir = "../../../../";

let prereleaseTag,
prereleaseBranch,
ignorePrefixes = [];
let name = "@sliit-foss/automatic-versioning";
let rootDir = defaultRootDir;
let noCommitEdit = false,
noCommit = false,
recursive = false;

args.forEach((arg) => {
if (arg.includes("--name=")) name = arg.replace("--name=", "");
if (arg.includes("--rootDir=")) rootDir += arg.replace("--rootDir=", "");
if (arg.includes("--no-commit-edit")) noCommitEdit = true;
if (arg.includes("--no-commit")) noCommit = true;
if (arg.includes("--recursive")) recursive = true;
if (arg.includes("--prerelease-tag=")) prereleaseTag = arg.replace("--prerelease-tag=", "");
if (arg.includes("--prerelease-branch=")) prereleaseBranch = arg.replace("--prerelease-branch=", "");
if (arg.includes("--ignore-prefixes=")) ignorePrefixes = arg.replace("--ignore-prefixes=", "")?.split(",") ?? [];
});

console.log(`Running version bump for ${name}`.green);

if (rootDir !== defaultRootDir) {
const parentDir = path.resolve(__dirname, rootDir);
opts.root ??= defaultRootDir;
opts.ignorePrefixes = opts.ignorePrefixes?.split(",") ?? [];

console.log(`Running versioning script for ${opts.name}`.green);

if (opts.root !== defaultRootDir) {
const parentDir = path.resolve(__dirname, opts.root);
process.chdir(parentDir);
}

if (args.includes("--tag-based")) {
tagBasedRunner(name, noCommit);
if (opts.tagBased) {
tagBasedRunner(opts.name, opts.skipCommit);
} else {
defaultRunner(name, noCommit, noCommitEdit, recursive, prereleaseTag, prereleaseBranch, ignorePrefixes);
defaultRunner(
opts.name,
opts.skipCommit,
opts.recursive,
opts.prereleaseTag,
opts.prereleaseBranch,
opts.ignorePrefixes
);
}
14 changes: 5 additions & 9 deletions packages/automatic-versioning/src/types/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const getCommitPrefix = async (recursive, ignorePrefixes, n = 1) => {
return getCommitPrefix(recursive, ignorePrefixes, n + 1);
};

const runner = (name, noCommit, noCommitEdit, recursive = false, prereleaseTag, prereleaseBranch, ignorePrefixes) => {
const runner = (name, noCommit, recursive = false, prereleaseTag, prereleaseBranch, ignorePrefixes) => {
run("git show --first-parent ./").then(async (diff) => {
if (diff) {
console.log(`Diff found, running versioning for ${name}`.green);
Expand Down Expand Up @@ -90,14 +90,10 @@ const runner = (name, noCommit, noCommitEdit, recursive = false, prereleaseTag,
}
});
} else {
if (noCommitEdit) {
console.log(`No bump found in commit message, skipping version bump`.yellow);
} else {
console.log(`No bump found in commit message, skipping version bump and editing commit message`.yellow);
run(`git commit --amend -m "${commitMessage.replace(/--no-bump/g, "")}"`).then(() => {
console.log("Successfully edited commit message".green);
});
}
console.log(`No bump found in commit message, skipping version bump and editing commit message`.yellow);
run(`git commit --amend -m "${commitMessage.replace(/--no-bump/g, "")}"`).then(() => {
console.log("Successfully edited commit message".green);
});
}
} else {
console.log(`No diff found, skipping version bump for ${name}`.yellow);
Expand Down
2 changes: 2 additions & 0 deletions packages/automatic-versioning/src/types/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as defaultRunner } from "./default";
export { default as tagBasedRunner } from "./tag-based";
31 changes: 27 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cf331a9

Please sign in to comment.