Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: config.subDir #97

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"pkg-types": "^1.0.2",
"scule": "^1.0.0",
"semver": "^7.5.0",
"ufo": "^1.1.1",
"yaml": "^2.2.2"
},
"devDependencies": {
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions src/commands/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ export default async function defaultMain(args: Argv) {
to: args.to,
output: args.output,
newVersion: args.r,
subDir: args.subDir,
});

const logger = consola.create({ stdout: process.stderr });
logger.info(`Generating changelog for ${config.from || ""}...${config.to}`);

const rawCommits = await getGitDiff(config.from, config.to);
const rawCommits = await getGitDiff(config.from, config.to, config.subDir);

// Parse commits as conventional commits
const commits = parseCommits(rawCommits, config).filter(
Expand Down Expand Up @@ -86,9 +87,10 @@ export default async function defaultMain(args: Argv) {
// Commit and tag changes for release mode
if (args.release) {
if (args.commit !== false) {
const filesToAdd = [config.output, "package.json"].filter(
(f) => f && typeof f === "string"
) as string[];
const filesToAdd = [
config.output,
resolve(config.cwd, config.subDir, "package.json"),
].filter((f) => f && typeof f === "string") as string[];
await execa("git", ["add", ...filesToAdd], { cwd });
const msg = config.templates.commitMessage.replaceAll(
"{{newVersion}}",
Expand Down
9 changes: 7 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface ChangelogConfig {
tagMessage?: string;
tagBody?: string;
};
subDir: string;
}

const getDefaultConfig = () =>
Expand Down Expand Up @@ -54,6 +55,7 @@ const getDefaultConfig = () =>
tagMessage: "v{{newVersion}}",
tagBody: "v{{newVersion}}",
},
subDir: "/",
};

export async function loadChangelogConfig(
Expand Down Expand Up @@ -84,8 +86,11 @@ export async function loadChangelogConfig(
if (!config.output) {
config.output = false;
} else if (config.output) {
config.output =
config.output === true ? defaults.output : resolve(cwd, config.output);
config.output = resolve(
cwd,
config.subDir,
config.output === true ? (defaults.output as string) : config.output
);
}

if (!config.repo) {
Expand Down
13 changes: 9 additions & 4 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,21 @@ export async function getGitRemoteURL(cwd: string, remote = "origin") {

export async function getGitDiff(
from: string | undefined,
to = "HEAD"
to = "HEAD",
dir?: string
): Promise<RawGitCommit[]> {
// https://git-scm.com/docs/pretty-formats
const r = await execCommand("git", [
const args = [
"--no-pager",
"log",
`${from ? `${from}...` : ""}${to}`,
'--pretty="----%n%s|%h|%an|%ae%n%b"',
"--name-status",
]);
];
if (dir) {
args.push("--", dir);
}
// https://git-scm.com/docs/pretty-formats
const r = await execCommand("git", args.filter(Boolean));
return r
.split("----\n")
.splice(1)
Expand Down
7 changes: 5 additions & 2 deletions src/github.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { existsSync, promises as fsp } from "node:fs";
import { homedir } from "node:os";
import { $fetch, FetchOptions } from "ofetch";
import { join } from "pathe";
import { join, relative } from "pathe";
import { joinURL } from "ufo";
import { ChangelogConfig } from "./config";

export interface GithubOptions {
Expand Down Expand Up @@ -38,9 +39,11 @@ export async function getGithubReleaseByTag(
}

export async function getGithubChangelog(config: ChangelogConfig) {
const filePath = relative(config.cwd, config.output as string);
const urlPath = joinURL(config.repo.repo, "main", filePath);
return await githubFetch(
config,
`https://raw.githubusercontent.com/${config.repo.repo}/main/CHANGELOG.md`
`https://raw.githubusercontent.com/${urlPath}`
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/semver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export async function bumpVersion(
let type = opts.type || determineSemverChange(commits, config) || "patch";
const originalType = type;

const pkgPath = resolve(config.cwd, "package.json");
const pkgPath = resolve(config.cwd, config.subDir, "package.json");
const pkg = await readPackageJSON(pkgPath);
const currentVersion = pkg.version || "0.0.0";

Expand Down
8 changes: 8 additions & 0 deletions test/git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ describe("git", () => {
);
});

test("getGitDiff with dir should work", async () => {
const COMMIT_INITIAL = "4554fc49265ac532b14c89cec15e7d21bb55d48b";
const COMMIT_VER002 = "38d7ba15dccc3a44931bf8bf0abaa0d4d96603eb";
expect(
(await getGitDiff(COMMIT_INITIAL, COMMIT_VER002, "./test")).length
).toBe(1);
});

test("parse", async () => {
const COMMIT_FROM = "1cb15d5dd93302ebd5ff912079ed584efcc6703b";
const COMMIT_TO = "3828bda8c45933396ddfa869d671473231ce3c95";
Expand Down
34 changes: 34 additions & 0 deletions test/github.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { afterEach, describe, expect, test, vi } from "vitest";
import { $fetch } from "ofetch";
import { getGithubChangelog } from "../src/github";
import { ChangelogConfig } from "../src";

vi.mock("ofetch", () => ({
$fetch: vi.fn(),
}));

describe("github", () => {
afterEach(() => {
vi.mocked($fetch).mockClear();
});

test("getGithubChangelog should work", async () => {
await getGithubChangelog({
cwd: "/bar",
output: "/bar/CHANGELOG.md",
repo: {
domain: "github.com",
repo: "foo",
},
tokens: {
github: undefined,
},
} as ChangelogConfig);

expect($fetch).toBeCalledTimes(1);
expect($fetch).toBeCalledWith(
`https://raw.githubusercontent.com/foo/main/CHANGELOG.md`,
expect.anything()
);
});
});