Skip to content

Commit

Permalink
feat: bump pre version (#70)
Browse files Browse the repository at this point in the history
* feat: bump pre version

* feat: use pkg-types to read/write pkg version

* reverse: use path.resolve

* small updates and simplification

---------

Co-authored-by: Pooya Parsa <pooya@pi0.io>
  • Loading branch information
aa900031 and pi0 authored Mar 28, 2023
1 parent fbc32cc commit 602a440
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 16 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ npx changelogen@latest [...args] [--dir <dir>]
- `--major`: Bump as a semver-major version
- `--minor`: Bump as a semver-minor version
- `--patch`: Bump as a semver-patch version
- `--premajor`: Bump as a semver-premajor version, can set id with string.
- `--preminor`: Bump as a semver-preminor version, can set id with string.
- `--prepatch`: Bump as a semver-prepatch version, can set id with string.
- `--prerelease`: Bump as a semver-prerelease version, can set id with string.

### `changelogen gh release`

Expand Down Expand Up @@ -87,6 +91,7 @@ Made with 💛
Published under [MIT License](./LICENSE).

<!-- Badges -->

[npm-version-src]: https://img.shields.io/npm/v/changelogen?style=flat&colorA=18181B&colorB=F0DB4F
[npm-version-href]: https://npmjs.com/package/changelogen
[npm-downloads-src]: https://img.shields.io/npm/dm/changelogen?style=flat&colorA=18181B&colorB=F0DB4F
Expand Down
37 changes: 28 additions & 9 deletions src/commands/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
parseCommits,
bumpVersion,
generateMarkDown,
BumpVersionOptions,
} from "..";
import { githubRelease } from "./github";

Expand Down Expand Up @@ -38,15 +39,8 @@ export default async function defaultMain(args: Argv) {

// Bump version optionally
if (args.bump || args.release) {
let type;
if (args.major) {
type = "major";
} else if (args.minor) {
type = "minor";
} else if (args.patch) {
type = "patch";
}
const newVersion = await bumpVersion(commits, config, { type });
const bumpOptions = _getBumpVersionOptions(args);
const newVersion = await bumpVersion(commits, config, bumpOptions);
if (!newVersion) {
consola.error("Unable to bump version based on changes.");
process.exit(1);
Expand Down Expand Up @@ -120,3 +114,28 @@ export default async function defaultMain(args: Argv) {
}
}
}

function _getBumpVersionOptions(args: Argv): BumpVersionOptions {
for (const type of [
"major",
"premajor",
"minor",
"preminor",
"patch",
"prepatch",
"prerelease",
] as const) {
const value = args[type];
if (value) {
if (type.startsWith("pre")) {
return {
type,
preid: typeof value === "string" ? value : "",
};
}
return {
type,
};
}
}
}
25 changes: 18 additions & 7 deletions src/semver.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { promises as fsp } from "node:fs";
import { resolve } from "node:path";
import semver from "semver";
import consola from "consola";
import { readPackageJSON, writePackageJSON } from "pkg-types";
import type { ChangelogConfig } from "./config";
import type { GitCommit } from "./git";

export type SemverBumpType = "major" | "minor" | "patch";
export type SemverBumpType =
| "major"
| "premajor"
| "minor"
| "preminor"
| "patch"
| "prepatch"
| "prerelease";

export function determineSemverChange(
commits: GitCommit[],
Expand All @@ -27,17 +34,21 @@ export function determineSemverChange(
return hasMajor ? "major" : hasMinor ? "minor" : hasPatch ? "patch" : null;
}

export type BumpVersionOptions = {
type?: SemverBumpType;
preid?: string;
};

export async function bumpVersion(
commits: GitCommit[],
config: ChangelogConfig,
opts: { type?: SemverBumpType } = {}
opts: BumpVersionOptions = {}
): Promise<string | false> {
let type = opts.type || determineSemverChange(commits, config) || "patch";
const originalType = type;

const pkgPath = resolve(config.cwd, "package.json");
const pkg =
JSON.parse(await fsp.readFile(pkgPath, "utf8").catch(() => "{}")) || {};
const pkg = await readPackageJSON(pkgPath);
const currentVersion = pkg.version || "0.0.0";

if (currentVersion.startsWith("0.")) {
Expand All @@ -52,7 +63,7 @@ export async function bumpVersion(
pkg.version = config.newVersion;
} else if (type) {
// eslint-disable-next-line import/no-named-as-default-member
pkg.version = semver.inc(currentVersion, type);
pkg.version = semver.inc(currentVersion, type, opts.preid);
config.newVersion = pkg.version;
}

Expand All @@ -63,7 +74,7 @@ export async function bumpVersion(
consola.info(
`Bumping version from ${currentVersion} to ${pkg.version} (${originalType})`
);
await fsp.writeFile(pkgPath, JSON.stringify(pkg, null, 2) + "\n", "utf8");
await writePackageJSON(pkgPath, pkg);

return pkg.version;
}

0 comments on commit 602a440

Please sign in to comment.