From 12a0d545df75fdbd58833304556b5884197a5f48 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Thu, 19 Sep 2024 14:32:46 +0200 Subject: [PATCH] refactor: update `execCommand` additions to #222 --- src/commands/default.ts | 9 ++++----- src/exec.ts | 3 +-- src/git.ts | 27 ++++++++------------------- 3 files changed, 13 insertions(+), 26 deletions(-) diff --git a/src/commands/default.ts b/src/commands/default.ts index 185c909..cc7db8c 100644 --- a/src/commands/default.ts +++ b/src/commands/default.ts @@ -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( @@ -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, { diff --git a/src/exec.ts b/src/exec.ts index 3852b8e..eeddb07 100644 --- a/src/exec.ts +++ b/src/exec.ts @@ -5,8 +5,7 @@ import { export function execCommand( cmd: string, - args: string[], opts?: Omit ) { - return execSync(`${cmd} ${args.join(" ")}`, { encoding: "utf8", ...opts }); + return execSync(cmd, { encoding: "utf8", ...opts }).trim(); } diff --git a/src/git.ts b/src/git.ts index 873074b..823ae66 100644 --- a/src/git.ts +++ b/src/git.ts @@ -29,20 +29,18 @@ 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() { @@ -50,16 +48,11 @@ export function getCurrentGitRef() { } 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( @@ -67,13 +60,9 @@ export async function getGitDiff( to = "HEAD" ): 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", - ]); + 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)