From 647b32fdca765705ab1ba5be8c696b158e8c989d Mon Sep 17 00:00:00 2001 From: UncleDave <3244206+UncleDave@users.noreply.github.com> Date: Sat, 26 Nov 2022 19:32:37 +0100 Subject: [PATCH] feat(npm): use version commit message from lerna.json --- plugins/npm/__tests__/npm.test.ts | 86 +++++++++++++++++++++++++++++++ plugins/npm/src/index.ts | 21 ++++++-- 2 files changed, 103 insertions(+), 4 deletions(-) diff --git a/plugins/npm/__tests__/npm.test.ts b/plugins/npm/__tests__/npm.test.ts index c8e58de52..266417a7a 100644 --- a/plugins/npm/__tests__/npm.test.ts +++ b/plugins/npm/__tests__/npm.test.ts @@ -880,6 +880,92 @@ describe("publish", () => { ]); }); + test("monorepo - should use commit message from lerna.json", async () => { + mockFs({ + "lerna.json": ` + { + "command": { + "version": { + "message": "[skip ci] Custom version commit message" + } + } + } + `, + ...monorepoPackagesOnFs, + }); + + const plugin = new NPMPlugin(); + const hooks = makeHooks(); + + plugin.apply({ + config: { prereleaseBranches: ["next"] }, + hooks, + remote: "origin", + baseBranch: "main", + logger: dummyLog(), + } as Auto.Auto); + + monorepoPackages.mockReturnValueOnce(monorepoPackagesResult); + execPromise.mockReturnValueOnce("1.0.0"); + + await hooks.version.promise({ bump: Auto.SEMVER.patch }); + expect(execPromise).toHaveBeenNthCalledWith(2, "npx", [ + "lerna", + "version", + "1.0.1", + "--force-publish", + "--no-commit-hooks", + "--yes", + "--no-push", + "-m", + "'\"[skip ci] Custom version commit message\"'", + false, + ]); + }); + + test("monorepo - should ensure commit message from lerna.json contains [skip ci]", async () => { + mockFs({ + "lerna.json": ` + { + "command": { + "version": { + "message": "Custom version commit message" + } + } + } + `, + ...monorepoPackagesOnFs, + }); + + const plugin = new NPMPlugin(); + const hooks = makeHooks(); + + plugin.apply({ + config: { prereleaseBranches: ["next"] }, + hooks, + remote: "origin", + baseBranch: "main", + logger: dummyLog(), + } as Auto.Auto); + + monorepoPackages.mockReturnValueOnce(monorepoPackagesResult); + execPromise.mockReturnValueOnce("1.0.0"); + + await hooks.version.promise({ bump: Auto.SEMVER.patch }); + expect(execPromise).toHaveBeenNthCalledWith(2, "npx", [ + "lerna", + "version", + "1.0.1", + "--force-publish", + "--no-commit-hooks", + "--yes", + "--no-push", + "-m", + "'\"Custom version commit message [skip ci]\"'", + false, + ]); + }); + test("should publish private scoped packages to private", async () => { const plugin = new NPMPlugin(); const hooks = makeHooks(); diff --git a/plugins/npm/src/index.ts b/plugins/npm/src/index.ts index af23b08a7..2208405fc 100644 --- a/plugins/npm/src/index.ts +++ b/plugins/npm/src/index.ts @@ -958,7 +958,8 @@ export default class NPMPlugin implements IPlugin { if (isMonorepo()) { auto.logger.verbose.info("Detected monorepo, using lerna"); - const monorepoVersion = getLernaJson().version; + const lernaJson = getLernaJson(); + const monorepoVersion = lernaJson.version; const isIndependent = monorepoVersion === "independent"; if (dryRun) { @@ -995,6 +996,20 @@ export default class NPMPlugin implements IPlugin { ? useVersion || bump : useVersion || (await bumpLatest(getMonorepoPackage(), bump)) || bump; + let commitMessage = isIndependent + ? "Bump independent versions [skip ci]" + : "Bump version to: %s [skip ci]"; + + const customCommitMessage = lernaJson.command?.version?.message; + + if (typeof customCommitMessage === "string") { + commitMessage = customCommitMessage.trim(); + + if (customCommitMessage.indexOf("[skip ci]") === -1) { + commitMessage += " [skip ci]"; + } + } + await execPromise("npx", [ "lerna", "version", @@ -1004,9 +1019,7 @@ export default class NPMPlugin implements IPlugin { "--yes", "--no-push", "-m", - isIndependent - ? `'"Bump independent versions [skip ci]"'` - : `'"Bump version to: %s [skip ci]"'`, + `'"${commitMessage}"'`, this.exact && "--exact", ...verboseArgs, ]);