Skip to content

Commit

Permalink
refactor: update execCommand
Browse files Browse the repository at this point in the history
additions to #222
  • Loading branch information
pi0 committed Sep 19, 2024
1 parent 9998f10 commit 12a0d54
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 26 deletions.
9 changes: 4 additions & 5 deletions src/commands/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,12 @@ export default async function defaultMain(args: Argv) {
const filesToAdd = [config.output, "package.json"].filter(
(f) => f && typeof f === "string"
) as string[];
execCommand("git", ["add", ...filesToAdd], { cwd });
execCommand(`git add ${filesToAdd.map((f) => `"${f}"`)}`, { cwd });
const msg = config.templates.commitMessage.replaceAll(
"{{newVersion}}",
config.newVersion
);
execCommand("git", ["commit", "-m", msg], { cwd });
execCommand(`git commit -m "${msg}"`, { cwd });
}
if (args.tag !== false) {
const msg = config.templates.tagMessage.replaceAll(
Expand All @@ -136,13 +136,12 @@ export default async function defaultMain(args: Argv) {
config.newVersion
);
execCommand(
"git",
["tag", ...(config.signTags ? ["-s"] : []), "-am", msg, body],
`git tag ${config.signTags ? "-s" : ""} -am "${msg}" "${body}"`,
{ cwd }
);
}
if (args.push === true) {
execCommand("git", ["push", "--follow-tags"], { cwd });
execCommand("git push --follow-tags", { cwd });
}
if (args.github !== false && config.repo?.provider === "github") {
await githubRelease(config, {
Expand Down
3 changes: 1 addition & 2 deletions src/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import {

export function execCommand(
cmd: string,
args: string[],
opts?: Omit<ExecOptionsWithStringEncoding, "encoding">
) {
return execSync(`${cmd} ${args.join(" ")}`, { encoding: "utf8", ...opts });
return execSync(cmd, { encoding: "utf8", ...opts }).trim();
}
27 changes: 8 additions & 19 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,51 +29,40 @@ export interface GitCommit extends RawGitCommit {

export async function getLastGitTag() {
try {
return execCommand("git", ["describe", "--tags", "--abbrev=0"])
?.split("\n")
.at(-1);
return execCommand("git describe --tags --abbrev=0")?.split("\n").at(-1);
} catch {
// Ignore
}
}

export function getCurrentGitBranch() {
return execCommand("git", ["rev-parse", "--abbrev-ref", "HEAD"]);
return execCommand("git rev-parse --abbrev-ref HEAD");
}

export function getCurrentGitTag() {
return execCommand("git", ["tag", "--points-at", "HEAD"]);
return execCommand("git tag --points-at HEAD");
}

export function getCurrentGitRef() {
return getCurrentGitTag() || getCurrentGitBranch();
}

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}"`);
}

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

export async function getGitDiff(
from: string | undefined,
to = "HEAD"
): Promise<RawGitCommit[]> {
// 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",
]);
const r = execCommand(
`git --no-pager log "${from ? `${from}...` : ""}${to}" --pretty="----%n%s|%h|%an|%ae%n%b" --name-status`
);
return r
.split("----\n")
.splice(1)
Expand Down

0 comments on commit 12a0d54

Please sign in to comment.