From af60de1c410a2725627b4eb5da9628d0f5cf6d09 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sun, 2 Mar 2025 20:26:02 +0000 Subject: [PATCH 1/2] fix: pass `cwd` in more places before running commands --- src/commands/default.ts | 4 ++-- src/config.ts | 4 ++-- src/git.ts | 28 +++++++++++++++------------- src/package.ts | 2 +- 4 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/commands/default.ts b/src/commands/default.ts index e3c8ce40..6ea7894e 100644 --- a/src/commands/default.ts +++ b/src/commands/default.ts @@ -29,7 +29,7 @@ export default async function defaultMain(args: Argv) { }); if (args.clean) { - const dirty = await getCurrentGitStatus(); + const dirty = await getCurrentGitStatus(cwd); if (dirty) { consola.error("Working directory is not clean."); process.exit(1); @@ -39,7 +39,7 @@ export default async function defaultMain(args: Argv) { const logger = consola.create({ stdout: process.stderr }); logger.info(`Generating changelog for ${config.from || ""}...${config.to}`); - const rawCommits = await getGitDiff(config.from, config.to); + const rawCommits = await getGitDiff(config.from, config.to, config.cwd); // Parse commits as conventional commits const commits = parseCommits(rawCommits, config) diff --git a/src/config.ts b/src/config.ts index 099c7e50..25f4c71a 100644 --- a/src/config.ts +++ b/src/config.ts @@ -102,11 +102,11 @@ export async function resolveChangelogConfig( cwd: string ) { if (!config.from) { - config.from = await getLastGitTag(); + config.from = await getLastGitTag(cwd); } if (!config.to) { - config.to = await getCurrentGitRef(); + config.to = await getCurrentGitRef(cwd); } if (config.output) { diff --git a/src/git.ts b/src/git.ts index ddef33a6..747907c8 100644 --- a/src/git.ts +++ b/src/git.ts @@ -27,41 +27,43 @@ export interface GitCommit extends RawGitCommit { isBreaking: boolean; } -export async function getLastGitTag() { +export async function getLastGitTag(cwd?: string) { try { - return execCommand("git describe --tags --abbrev=0")?.split("\n").at(-1); + return execCommand("git describe --tags --abbrev=0", cwd)?.split("\n").at(-1); } catch { // Ignore } } -export function getCurrentGitBranch() { - return execCommand("git rev-parse --abbrev-ref HEAD"); +export function getCurrentGitBranch(cwd?: string) { + return execCommand("git rev-parse --abbrev-ref HEAD", cwd); } -export function getCurrentGitTag() { - return execCommand("git tag --points-at HEAD"); +export function getCurrentGitTag(cwd?: string) { + return execCommand("git tag --points-at HEAD", cwd); } -export function getCurrentGitRef() { - return getCurrentGitTag() || getCurrentGitBranch(); +export function getCurrentGitRef(cwd?: string) { + return getCurrentGitTag(cwd) || getCurrentGitBranch(cwd); } export function getGitRemoteURL(cwd: string, remote = "origin") { - return execCommand(`git --work-tree="${cwd}" remote get-url "${remote}"`); + return execCommand(`git --work-tree="${cwd}" remote get-url "${remote}"`, cwd); } -export async function getCurrentGitStatus() { - return execCommand("git status --porcelain"); +export async function getCurrentGitStatus(cwd?: string) { + return execCommand("git status --porcelain", cwd); } export async function getGitDiff( from: string | undefined, - to = "HEAD" + to = "HEAD", + cwd?: string ): Promise { // https://git-scm.com/docs/pretty-formats const r = execCommand( - `git --no-pager log "${from ? `${from}...` : ""}${to}" --pretty="----%n%s|%h|%an|%ae%n%b" --name-status` + `git --no-pager log "${from ? `${from}...` : ""}${to}" --pretty="----%n%s|%h|%an|%ae%n%b" --name-status`, + cwd ); return r .split("----\n") diff --git a/src/package.ts b/src/package.ts index 4f142e84..2a4e7ec8 100644 --- a/src/package.ts +++ b/src/package.ts @@ -54,5 +54,5 @@ export async function npmPublish(config: ChangelogConfig) { args.push("--provenance"); } - return execCommand(`npm publish ${args.join(" ")}`); + return execCommand(`npm publish ${args.join(" ")}`, config.cwd); } From ae692297cca976d30dda72d50ed5b6f306e02da2 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sun, 2 Mar 2025 20:26:29 +0000 Subject: [PATCH 2/2] chore: lint --- src/git.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/git.ts b/src/git.ts index 747907c8..72087cf1 100644 --- a/src/git.ts +++ b/src/git.ts @@ -29,7 +29,9 @@ export interface GitCommit extends RawGitCommit { export async function getLastGitTag(cwd?: string) { try { - return execCommand("git describe --tags --abbrev=0", cwd)?.split("\n").at(-1); + return execCommand("git describe --tags --abbrev=0", cwd) + ?.split("\n") + .at(-1); } catch { // Ignore } @@ -48,7 +50,10 @@ export function getCurrentGitRef(cwd?: string) { } export function getGitRemoteURL(cwd: string, remote = "origin") { - return execCommand(`git --work-tree="${cwd}" remote get-url "${remote}"`, cwd); + return execCommand( + `git --work-tree="${cwd}" remote get-url "${remote}"`, + cwd + ); } export async function getCurrentGitStatus(cwd?: string) {