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 68127be
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 37 deletions.
11 changes: 5 additions & 6 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}"`).join(" ")}`, 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],
{ cwd }
`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
13 changes: 3 additions & 10 deletions src/exec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import {
execSync,
type ExecOptionsWithStringEncoding,
} from "node:child_process";
import { execSync } from "node:child_process";

export function execCommand(
cmd: string,
args: string[],
opts?: Omit<ExecOptionsWithStringEncoding, "encoding">
) {
return execSync(`${cmd} ${args.join(" ")}`, { encoding: "utf8", ...opts });
export function execCommand(cmd: string, cwd?: string) {
return execSync(cmd, { encoding: "utf8", cwd }).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
4 changes: 2 additions & 2 deletions src/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export async function npmPublish(config: ChangelogConfig) {
}

if (config.publish.tag) {
args.push("--tag", config.publish.tag);
args.push("--tag", `"${config.publish.tag}"`);
}

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

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

0 comments on commit 68127be

Please sign in to comment.