Skip to content

Commit c374d1c

Browse files
committed
feat: push workspaces to remote on successful publish
1 parent 960f43f commit c374d1c

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

packages/semantic-release-ws/src/plugin/common/tag-workspace.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Context, tag } from "@wrench/semantic-release";
2-
import { Workspace } from "../../types";
2+
import { Signale } from "signale";
3+
import { CommonOptions, Workspace } from "../../types";
34
import { createWorkspaceLogger } from "../../util";
45

56
export function warnNotCreatingTag(workspace: Workspace, owner: Context): boolean {
@@ -12,6 +13,13 @@ export function warnNotCreatingTag(workspace: Workspace, owner: Context): boolea
1213
return true;
1314
}
1415

16+
export function warnNoGitPush(options: CommonOptions, logger: Signale): boolean {
17+
if (options.dryRun) logger.warn(`Skip 'git push' in dry-run mode`);
18+
else if (options.git === false) logger.warn(`Skip 'git push' since git disabled`);
19+
else return false;
20+
return true;
21+
}
22+
1523
export async function tagWorkspace(workspace: Workspace, owner: Context) {
1624
const {logger, env} = owner;
1725
const {nextRelease, cwd} = workspace;

packages/semantic-release-ws/src/plugin/publish.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { PublishContext, Release } from "@wrench/semantic-release";
2-
import { WsConfiguration } from "../types";
3-
import { callWorkspacesOf, WorkspacesHooks } from "../util";
4-
1+
import { PublishContext, push, Release } from "@wrench/semantic-release";
2+
import { CommonOptions, Workspace, WsConfiguration } from "../types";
3+
import { callWorkspacesOf, createWorkspaceLogger, WorkspacesHooks } from "../util";
54
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
5+
import { tagWorkspace, warnNoGitPush } from "./common";
6+
67
export async function publish(input: WsConfiguration, context: PublishContext) {
78
return callWorkspacesOf("publish", context, hooks);
89
}
@@ -21,4 +22,25 @@ const hooks: WorkspacesHooks<"publish"> = {
2122
return flat.length && flat as any;
2223
}
2324
},
25+
26+
/** Push workspaces tags and changes to remote repository. */
27+
postProcessWorkspaces(workspaces: Workspace[], outputs: never, output: never, owner: PublishContext): Promise<unknown> {
28+
if (warnNoGitPush(owner.options as CommonOptions, owner.logger))
29+
return;
30+
31+
const {env} = owner;
32+
const pending = [];
33+
const repos = new Set<string>();
34+
35+
// push distinct remotes
36+
for (const workspace of workspaces) {
37+
const {options, cwd} = workspace;
38+
const {repositoryUrl} = options;
39+
if (!repos.has(repositoryUrl) && repos.add(repositoryUrl))
40+
if (!warnNoGitPush(options, createWorkspaceLogger(workspace, owner)))
41+
pending.push(push(repositoryUrl, {cwd, env}));
42+
}
43+
44+
return Promise.all(pending);
45+
},
2446
};

packages/semantic-release/src/expose/git.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { Release } from "../types";
33

44
const git = require("semantic-release/lib/git");
55

6-
export function tag(release: Release, options?: Options) {
6+
export function tag(release: Release, options?: Options): Promise<void> {
77
return git.tag(release, options);
88
}
9+
10+
export function push(url: string, options?: Options): Promise<void> {
11+
return git.push(url, options);
12+
}

0 commit comments

Comments
 (0)