Skip to content

Commit b1d2f7b

Browse files
authored
fix: pass cwd in more places before running commands (#266)
1 parent ec2618f commit b1d2f7b

File tree

4 files changed

+25
-18
lines changed

4 files changed

+25
-18
lines changed

src/commands/default.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default async function defaultMain(args: Argv) {
2929
});
3030

3131
if (args.clean) {
32-
const dirty = await getCurrentGitStatus();
32+
const dirty = await getCurrentGitStatus(cwd);
3333
if (dirty) {
3434
consola.error("Working directory is not clean.");
3535
process.exit(1);
@@ -39,7 +39,7 @@ export default async function defaultMain(args: Argv) {
3939
const logger = consola.create({ stdout: process.stderr });
4040
logger.info(`Generating changelog for ${config.from || ""}...${config.to}`);
4141

42-
const rawCommits = await getGitDiff(config.from, config.to);
42+
const rawCommits = await getGitDiff(config.from, config.to, config.cwd);
4343

4444
// Parse commits as conventional commits
4545
const commits = parseCommits(rawCommits, config)

src/config.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,11 @@ export async function resolveChangelogConfig(
102102
cwd: string
103103
) {
104104
if (!config.from) {
105-
config.from = await getLastGitTag();
105+
config.from = await getLastGitTag(cwd);
106106
}
107107

108108
if (!config.to) {
109-
config.to = await getCurrentGitRef();
109+
config.to = await getCurrentGitRef(cwd);
110110
}
111111

112112
if (config.output) {

src/git.ts

+20-13
Original file line numberDiff line numberDiff line change
@@ -27,41 +27,48 @@ export interface GitCommit extends RawGitCommit {
2727
isBreaking: boolean;
2828
}
2929

30-
export async function getLastGitTag() {
30+
export async function getLastGitTag(cwd?: string) {
3131
try {
32-
return execCommand("git describe --tags --abbrev=0")?.split("\n").at(-1);
32+
return execCommand("git describe --tags --abbrev=0", cwd)
33+
?.split("\n")
34+
.at(-1);
3335
} catch {
3436
// Ignore
3537
}
3638
}
3739

38-
export function getCurrentGitBranch() {
39-
return execCommand("git rev-parse --abbrev-ref HEAD");
40+
export function getCurrentGitBranch(cwd?: string) {
41+
return execCommand("git rev-parse --abbrev-ref HEAD", cwd);
4042
}
4143

42-
export function getCurrentGitTag() {
43-
return execCommand("git tag --points-at HEAD");
44+
export function getCurrentGitTag(cwd?: string) {
45+
return execCommand("git tag --points-at HEAD", cwd);
4446
}
4547

46-
export function getCurrentGitRef() {
47-
return getCurrentGitTag() || getCurrentGitBranch();
48+
export function getCurrentGitRef(cwd?: string) {
49+
return getCurrentGitTag(cwd) || getCurrentGitBranch(cwd);
4850
}
4951

5052
export function getGitRemoteURL(cwd: string, remote = "origin") {
51-
return execCommand(`git --work-tree="${cwd}" remote get-url "${remote}"`);
53+
return execCommand(
54+
`git --work-tree="${cwd}" remote get-url "${remote}"`,
55+
cwd
56+
);
5257
}
5358

54-
export async function getCurrentGitStatus() {
55-
return execCommand("git status --porcelain");
59+
export async function getCurrentGitStatus(cwd?: string) {
60+
return execCommand("git status --porcelain", cwd);
5661
}
5762

5863
export async function getGitDiff(
5964
from: string | undefined,
60-
to = "HEAD"
65+
to = "HEAD",
66+
cwd?: string
6167
): Promise<RawGitCommit[]> {
6268
// https://git-scm.com/docs/pretty-formats
6369
const r = execCommand(
64-
`git --no-pager log "${from ? `${from}...` : ""}${to}" --pretty="----%n%s|%h|%an|%ae%n%b" --name-status`
70+
`git --no-pager log "${from ? `${from}...` : ""}${to}" --pretty="----%n%s|%h|%an|%ae%n%b" --name-status`,
71+
cwd
6572
);
6673
return r
6774
.split("----\n")

src/package.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,5 @@ export async function npmPublish(config: ChangelogConfig) {
5454
args.push("--provenance");
5555
}
5656

57-
return execCommand(`npm publish ${args.join(" ")}`);
57+
return execCommand(`npm publish ${args.join(" ")}`, config.cwd);
5858
}

0 commit comments

Comments
 (0)