Skip to content

Commit 68127be

Browse files
committedSep 19, 2024
refactor: update execCommand
additions to #222
1 parent 9998f10 commit 68127be

File tree

4 files changed

+18
-37
lines changed

4 files changed

+18
-37
lines changed
 

‎src/commands/default.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,12 @@ export default async function defaultMain(args: Argv) {
119119
const filesToAdd = [config.output, "package.json"].filter(
120120
(f) => f && typeof f === "string"
121121
) as string[];
122-
execCommand("git", ["add", ...filesToAdd], { cwd });
122+
execCommand(`git add ${filesToAdd.map((f) => `"${f}"`).join(" ")}`, cwd);
123123
const msg = config.templates.commitMessage.replaceAll(
124124
"{{newVersion}}",
125125
config.newVersion
126126
);
127-
execCommand("git", ["commit", "-m", msg], { cwd });
127+
execCommand(`git commit -m "${msg}"`, cwd);
128128
}
129129
if (args.tag !== false) {
130130
const msg = config.templates.tagMessage.replaceAll(
@@ -136,13 +136,12 @@ export default async function defaultMain(args: Argv) {
136136
config.newVersion
137137
);
138138
execCommand(
139-
"git",
140-
["tag", ...(config.signTags ? ["-s"] : []), "-am", msg, body],
141-
{ cwd }
139+
`git tag ${config.signTags ? "-s" : ""} -am "${msg}" "${body}"`,
140+
cwd
142141
);
143142
}
144143
if (args.push === true) {
145-
execCommand("git", ["push", "--follow-tags"], { cwd });
144+
execCommand("git push --follow-tags", cwd);
146145
}
147146
if (args.github !== false && config.repo?.provider === "github") {
148147
await githubRelease(config, {

‎src/exec.ts

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
1-
import {
2-
execSync,
3-
type ExecOptionsWithStringEncoding,
4-
} from "node:child_process";
1+
import { execSync } from "node:child_process";
52

6-
export function execCommand(
7-
cmd: string,
8-
args: string[],
9-
opts?: Omit<ExecOptionsWithStringEncoding, "encoding">
10-
) {
11-
return execSync(`${cmd} ${args.join(" ")}`, { encoding: "utf8", ...opts });
3+
export function execCommand(cmd: string, cwd?: string) {
4+
return execSync(cmd, { encoding: "utf8", cwd }).trim();
125
}

‎src/git.ts

+8-19
Original file line numberDiff line numberDiff line change
@@ -29,51 +29,40 @@ export interface GitCommit extends RawGitCommit {
2929

3030
export async function getLastGitTag() {
3131
try {
32-
return execCommand("git", ["describe", "--tags", "--abbrev=0"])
33-
?.split("\n")
34-
.at(-1);
32+
return execCommand("git describe --tags --abbrev=0")?.split("\n").at(-1);
3533
} catch {
3634
// Ignore
3735
}
3836
}
3937

4038
export function getCurrentGitBranch() {
41-
return execCommand("git", ["rev-parse", "--abbrev-ref", "HEAD"]);
39+
return execCommand("git rev-parse --abbrev-ref HEAD");
4240
}
4341

4442
export function getCurrentGitTag() {
45-
return execCommand("git", ["tag", "--points-at", "HEAD"]);
43+
return execCommand("git tag --points-at HEAD");
4644
}
4745

4846
export function getCurrentGitRef() {
4947
return getCurrentGitTag() || getCurrentGitBranch();
5048
}
5149

5250
export function getGitRemoteURL(cwd: string, remote = "origin") {
53-
return execCommand("git", [
54-
`--work-tree=${cwd}`,
55-
"remote",
56-
"get-url",
57-
remote,
58-
]);
51+
return execCommand(`git --work-tree="${cwd}" remote get-url "${remote}"`);
5952
}
6053

6154
export async function getCurrentGitStatus() {
62-
return execCommand("git", ["status", "--porcelain"]);
55+
return execCommand("git status --porcelain");
6356
}
6457

6558
export async function getGitDiff(
6659
from: string | undefined,
6760
to = "HEAD"
6861
): Promise<RawGitCommit[]> {
6962
// https://git-scm.com/docs/pretty-formats
70-
const r = execCommand("git", [
71-
"--no-pager",
72-
"log",
73-
`${from ? `${from}...` : ""}${to}`,
74-
'--pretty="----%n%s|%h|%an|%ae%n%b"',
75-
"--name-status",
76-
]);
63+
const r = execCommand(
64+
`git --no-pager log "${from ? `${from}...` : ""}${to}" --pretty="----%n%s|%h|%an|%ae%n%b" --name-status`
65+
);
7766
return r
7867
.split("----\n")
7968
.splice(1)

‎src/package.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export async function npmPublish(config: ChangelogConfig) {
4343
}
4444

4545
if (config.publish.tag) {
46-
args.push("--tag", config.publish.tag);
46+
args.push("--tag", `"${config.publish.tag}"`);
4747
}
4848

4949
if (
@@ -54,5 +54,5 @@ export async function npmPublish(config: ChangelogConfig) {
5454
args.push("--provenance");
5555
}
5656

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

0 commit comments

Comments
 (0)