Skip to content

Commit

Permalink
feat: add param to require clean working dir (unjs#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnCampionJr committed Apr 3, 2023
1 parent 0a5d6c8 commit 80c7b15
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ npx changelogen@latest [...args] [--dir <dir>]
- `--from`: Start commit reference. When not provided, **latest git tag** will be used as default.
- `--to`: End commit reference. When not provided, **latest commit in HEAD** will be used as default.
- `--dir`: Path to git repository. When not provided, **current working directory** will be used as as default.
- `--clean`: Determine if the working directory is clean and if it is not clean, exit.
- `--output`: Changelog file name to create or update. Defaults to `CHANGELOG.md` and resolved relative to dir. Use `--no-output` to write to console only.
- `--bump`: Determine semver change and update version in `package.json`.
- `--release`. Bumps version in `package.json` and creates commit and git tags using local `git`. You can disable commit using `--no-commit` and tag using `--no-tag`.
Expand Down
9 changes: 9 additions & 0 deletions src/commands/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { execa } from "execa";
import {
loadChangelogConfig,
getGitDiff,
getCurrentGitStatus,
parseCommits,
bumpVersion,
generateMarkDown,
Expand All @@ -25,6 +26,14 @@ export default async function defaultMain(args: Argv) {
newVersion: args.r,
});

if (args.clean) {
const dirty = await getCurrentGitStatus();
if (dirty) {
consola.error("Working directory is not clean.");
process.exit(1);
}
}

const logger = consola.create({ stdout: process.stderr });
logger.info(`Generating changelog for ${config.from || ""}...${config.to}`);

Expand Down
4 changes: 4 additions & 0 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ export async function getGitRemoteURL(cwd: string, remote = "origin") {
]);
}

export async function getCurrentGitStatus() {
return await execCommand("git", ["status", "--porcelain"]);
}

export async function getGitDiff(
from: string | undefined,
to = "HEAD"
Expand Down

0 comments on commit 80c7b15

Please sign in to comment.