Skip to content

Commit

Permalink
fix(semantic-release-config): fix commits in package changelogs
Browse files Browse the repository at this point in the history
  • Loading branch information
deopea-david committed Oct 17, 2023
1 parent c8a64d8 commit 6a17d6d
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 56 deletions.
53 changes: 27 additions & 26 deletions package-lock.json

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

6 changes: 4 additions & 2 deletions packages/conventional-changelog/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ const config: Config = {
],
};

const options: conventionalConfig.ResolvedConfig & { config: typeof config } = {
const options = (): conventionalConfig.ResolvedConfig & {
config: typeof config;
} => ({
...conventionalConfig(config),
config,
};
});

export = options;
2 changes: 2 additions & 0 deletions packages/conventional-changelog/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
},
"peerDependencies": {
"conventional-changelog": "^5.1.0",
"conventional-changelog-conventionalcommits": "^7.0.0",
"typescript": "^5.0.0"
},
"devDependencies": {
"@types/conventional-changelog": "^3.1.3",
"@types/conventional-changelog-config-spec": "^2.1.3",
"@types/node": "^20.8.4",
"conventional-changelog-conventionalcommits": "*",
"typescript": "*"
},
"keywords": [
Expand Down
64 changes: 36 additions & 28 deletions packages/semantic-release-config/src/release-workspaces.cts
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,43 @@ import type {
Commit,
GenerateNotesContext,
GlobalConfig,
Options,
} from "semantic-release";

import path = require("path");
import pkgUp = require("read-pkg-up");
import simpleGit = require("simple-git");
import utils = require("./utils.cjs");

const pkg: NormalizedReadResult | undefined = pkgUp.sync();
const pkgDir = pkg && path.dirname(pkg.path);
const git = simpleGit.simpleGit();

const verifyConditions = () => {
if (!pkg) throw new Error("package.json unavailable");
};
let _commitsCache: Record<string, Array<Commit & { include: boolean }>> = {};
const getPackageCommits = async (commits: readonly Commit[]) =>
(_commitsCache[pkg!.packageJson.name] ??= await Promise.all(
commits.map(async (cmt) => {
const res = await git.show(["--stat", cmt.hash, pkgDir!]);
return {
...cmt,
include: Boolean(res),
};
}),
).then((cs) => cs.filter((c) => c.include)));

let commitsToInclude: Array<Commit & { include: boolean }> | null = null;
const wrapLifecycle =
(lifecycle: string) =>
(lifecycle: "analyzeCommits" | "generateNotes") =>
async (
config: GlobalConfig,
config: Options,
{
logger,
commits,
commits: allCommits,
...ctx
}: (AnalyzeCommitsContext | GenerateNotesContext) & {
options: GlobalConfig;
},
) => {
commitsToInclude ??= await Promise.all(
commits.map(async (cmt) => {
const res = await git.show(["--stat", cmt.hash, pkgDir!]);
logger.debug("Include commit:", cmt.hash, Boolean(res));

return {
...cmt,
include: Boolean(res),
};
}),
).then((cs) => cs.filter((c) => c.include));
const commitsToInclude = await getPackageCommits(allCommits);

if (!commitsToInclude?.length) {
logger.log("Release does not include changes for package");
Expand All @@ -52,16 +51,24 @@ const wrapLifecycle =
let res: unknown;
for (const plugin of ctx.options.plugins) {
const pluginName = typeof plugin === "string" ? plugin : plugin[0];
// const pluginOptions =
// typeof plugin === "string"
// ? {}
// : (plugin[1] as Record<string, unknown>);

const module = await new Function(`return import("${pluginName}");`)();
const module = await utils.dynamicImport(pluginName);
if (!module[lifecycle]) continue;

logger.success("Release workspaces", "loaded plugin", pluginName);
res = await module[lifecycle]?.(config, {
...ctx,
logger,
commits,
});
res = await module[lifecycle]?.(
// TODO: add in plugin options
config,
{
...ctx,
logger,
commits: commitsToInclude,
},
);
logger.success(
"Release workspaces",
"completed step",
Expand All @@ -71,14 +78,15 @@ const wrapLifecycle =
);
}

if (lifecycle === "generateNotes") {
logger.log(res);
}
return res;
};

const verifyConditions = () => {
if (!pkg) throw new Error("package.json unavailable");
};

const onComplete = () => {
commitsToInclude = null;
_commitsCache = {};
};

export = {
Expand Down
22 changes: 22 additions & 0 deletions packages/semantic-release-config/src/utils.cts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type {
AnalyzeCommitsContext,
GenerateNotesContext,
Options,
} from "semantic-release";

// eslint-disable-next-line @typescript-eslint/no-implied-eval
const dynamicImport = new Function("moduleId", `return import(moduleId);`) as (
moduleId: string,
) => Promise<
Record<
string,
(
options: Options,
context: AnalyzeCommitsContext | GenerateNotesContext,
) => Promise<unknown>
>
>;

export = {
dynamicImport,
};

0 comments on commit 6a17d6d

Please sign in to comment.