Skip to content

Commit

Permalink
simplify hook APIs for easier future extensibility
Browse files Browse the repository at this point in the history
  • Loading branch information
hipstersmoothie committed Oct 26, 2020
1 parent e2d4f11 commit 26c47de
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 42 deletions.
4 changes: 4 additions & 0 deletions docs/pages/docs/plugins/changelog-hooks.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ The hooks it provides allow you to customize everything about how the changelog
This is where you hook into the changelog's hooks.
See examples below.

```ts
auto.hooks.onCreateChangelog.tapPromise('Giphy', (changelog, { bump }) => {});
```

## addToBody

Add extra content to your changelogs.
Expand Down
52 changes: 28 additions & 24 deletions docs/pages/docs/plugins/release-lifecycle-hooks.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,10 @@ You must push the tags to github!
This hooks is required for plugin that facilitate publishing.

```ts
auto.hooks.publish.tapPromise("NPM", async (version: SEMVER) => {
auto.hooks.publish.tapPromise("NPM", async ({ bump }) => {
await execPromise("npm", [
"version",
version,
bump,
"-m",
"Bump version to: %s [skip ci]",
]);
Expand Down Expand Up @@ -187,19 +187,26 @@ You can either return a string value of just the version or an object containing
- `details` - The body of the details element

```ts
auto.hooks.canary.tapPromise(this.name, async ({ bump, canaryIdentifier, dryRun, quiet }) => {
const lastRelease = await auto.git!.getLatestRelease();
const current = await auto.getCurrentVersion(lastRelease);
const nextVersion = inc(current, bump as ReleaseType);
const isScopedPackage = name.match(/@\S+\/\S+/);
const canaryVersion = `${nextVersion}-canary${canaryIdentifier}`;

await execPromise("npm", ["version", canaryVersion, "--no-git-tag-version"]);
await execPromise("npm", ["publish", "--tag", "canary"]);

auto.logger.verbose.info("Successfully published canary version");
return canaryVersion;
});
auto.hooks.canary.tapPromise(
this.name,
async ({ bump, canaryIdentifier, dryRun, quiet }) => {
const lastRelease = await auto.git!.getLatestRelease();
const current = await auto.getCurrentVersion(lastRelease);
const nextVersion = inc(current, bump as ReleaseType);
const isScopedPackage = name.match(/@\S+\/\S+/);
const canaryVersion = `${nextVersion}-canary${canaryIdentifier}`;

await execPromise("npm", [
"version",
canaryVersion,
"--no-git-tag-version",
]);
await execPromise("npm", ["publish", "--tag", "canary"]);

auto.logger.verbose.info("Successfully published canary version");
return canaryVersion;
}
);
```

`canary` version should not produce any of the following:
Expand Down Expand Up @@ -230,7 +237,7 @@ import {

auto.hooks.next.tapPromise(
this.name,
async (preReleaseVersions, bump, { dryRun }) => {
async (preReleaseVersions, { bump, dryRun }) => {
const branch = getCurrentBranch() || "";
const lastRelease = await auto.git.getLatestRelease();
const current =
Expand Down Expand Up @@ -328,18 +335,15 @@ _Other examples:_

Ran after the `shipit` command has run.

- `newVersion` - The new version that was release
- `commits` - the commits in the release
- `data`
- `newVersion` - The new version that was release
- `commits` - the commits in the release
- `context` - The type of release that was created (`latest`, `next`, `canary`, or `old`)

```ts
auto.hooks.afterShipIt.tap(
"MyPlugin",
async (newVersion, commits, { context }) => {
// do something
}
);
auto.hooks.afterShipIt.tap("MyPlugin", async ({ context }) => {
// do something
});
```

_Other examples:_
Expand Down
44 changes: 33 additions & 11 deletions packages/core/src/auto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ interface BeforeShipitContext extends DryRunOption {
}

interface NextContext extends DryRunOption {
/** The bump to apply to the version */
bump: SEMVER;
/** The commits in the next release */
commits: IExtendedCommit[];
/** The release notes for all the commits in the next release */
Expand All @@ -151,9 +153,11 @@ export interface IAutoHooks {
/** Ran after the `shipit` command has run. */
afterShipIt: AsyncParallelHook<
[
string | undefined,
IExtendedCommit[],
{
/** The version published in the shipit run */
newVersion: string | undefined;
/** The commits the version was published for */
commitsInRelease: IExtendedCommit[];
/** The type of release made by shipit */
context: ShipitContext;
}
Expand Down Expand Up @@ -214,7 +218,15 @@ export interface IAutoHooks {
* This is where you hook into the changelog's hooks.
* This hook is exposed for convenience during `this.hooks.onCreateRelease` and at the root `this.hooks`
*/
onCreateChangelog: SyncHook<[Changelog, SEMVER | undefined]>;
onCreateChangelog: SyncHook<
[
Changelog,
{
/** The bump the changelog will make */
bump: SEMVER | undefined;
}
]
>;
/** Version the package. This is a good opportunity to `git tag` the release also. */
version: AsyncParallelHook<
[
Expand All @@ -228,13 +240,20 @@ export interface IAutoHooks {
/** Ran after the package has been versioned. */
afterVersion: AsyncParallelHook<[DryRunOption]>;
/** Publish the package to some package distributor. You must push the tags to github! */
publish: AsyncParallelHook<[SEMVER]>;
publish: AsyncParallelHook<
[
{
/** The semver bump that was applied in the version hook */
bump: SEMVER;
}
]
>;
/** Used to publish a canary release. In this hook you get the semver bump and the unique canary postfix ID. */
canary: AsyncSeriesBailHook<
[
DryRunOption &
QuietOption & {
/** The bump to apply to the version */
/** The bump being applied to the version */
bump: SEMVER;
/** The post-version identifier to add to the version */
canaryIdentifier: string;
Expand All @@ -258,7 +277,7 @@ export interface IAutoHooks {
* and an array of next versions that been released. If you make another
* next release be sure to add it the the array.
*/
next: AsyncSeriesWaterfallHook<[string[], SEMVER, NextContext]>;
next: AsyncSeriesWaterfallHook<[string[], NextContext]>;
/** Ran after the package has been published. */
afterPublish: AsyncParallelHook<[]>;
/** Ran after the package has been published. */
Expand Down Expand Up @@ -392,8 +411,8 @@ export default class Auto {
this.hooks.onCreateRelease.tap("Link onCreateChangelog", (release) => {
release.hooks.onCreateChangelog.tap(
"Link onCreateChangelog",
(changelog, version) => {
this.hooks.onCreateChangelog.call(changelog, version);
(changelog, bump) => {
this.hooks.onCreateChangelog.call(changelog, { bump });
}
);
});
Expand Down Expand Up @@ -1317,7 +1336,8 @@ export default class Auto {
}

this.logger.verbose.info(`Calling "next" hook with: ${bump}`);
const result = await this.hooks.next.promise([], bump, {
const result = await this.hooks.next.promise([], {
bump,
commits,
fullReleaseNotes,
releaseNotes,
Expand Down Expand Up @@ -1484,7 +1504,9 @@ export default class Auto {
}

const { newVersion, commitsInRelease, context } = publishInfo;
await this.hooks.afterShipIt.promise(newVersion, commitsInRelease, {
await this.hooks.afterShipIt.promise({
newVersion,
commitsInRelease,
context,
});
}
Expand Down Expand Up @@ -1584,7 +1606,7 @@ export default class Auto {

if (!options.dryRun) {
this.logger.verbose.info("Calling publish hook");
await this.hooks.publish.promise(bump);
await this.hooks.publish.promise({ bump });
this.logger.verbose.info("Calling after publish hook");
await this.hooks.afterPublish.promise();
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/utils/make-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const makeHooks = (): IAutoHooks => ({
beforeShipIt: new AsyncSeriesHook(["context"]),
afterAddToChangelog: new AsyncSeriesHook(["context"]),
beforeCommitChangelog: new AsyncSeriesHook(["context"]),
afterShipIt: new AsyncParallelHook(["version", "commits", "context"]),
afterShipIt: new AsyncParallelHook(["context"]),
makeRelease: new AsyncSeriesBailHook(["releaseInfo"]),
afterRelease: new AsyncParallelHook(["releaseInfo"]),
onCreateRelease: new SyncHook(["options"]),
Expand All @@ -35,7 +35,7 @@ export const makeHooks = (): IAutoHooks => ({
publish: new AsyncParallelHook(["version"]),
afterPublish: new AsyncParallelHook(),
canary: new AsyncSeriesBailHook(["canaryContext"]),
next: new AsyncSeriesWaterfallHook(["preReleaseVersions", "bump", "context"]),
next: new AsyncSeriesWaterfallHook(["preReleaseVersions", "context"]),
});

/** Make the hooks for "Release" */
Expand Down
2 changes: 1 addition & 1 deletion plugins/docker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export default class DockerPlugin implements IPlugin {

auto.hooks.next.tapPromise(
this.name,
async (preReleaseVersions, bump, { dryRun }) => {
async (preReleaseVersions, { bump, dryRun }) => {
if (!auto.git) {
return preReleaseVersions;
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/git-tag/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export default class GitTagPlugin implements IPlugin {

auto.hooks.next.tapPromise(
this.name,
async (preReleaseVersions, bump, { dryRun }) => {
async (preReleaseVersions, { bump, dryRun }) => {
if (!auto.git) {
return preReleaseVersions;
}
Expand Down
6 changes: 3 additions & 3 deletions plugins/npm/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ export default class NPMPlugin implements IPlugin {

auto.hooks.onCreateChangelog.tap(
this.name,
(changelog, version = SEMVER.patch) => {
(changelog, { bump = SEMVER.patch }) => {
changelog.hooks.renderChangelogLine.tapPromise(
"NPM - Monorepo",
async ([commit, line]) => {
Expand All @@ -742,7 +742,7 @@ export default class NPMPlugin implements IPlugin {
this.releaseType !== "next" &&
getLernaJson().version === "independent",
logger: auto.logger,
version,
version: bump,
});

const section = changedPackages?.length
Expand Down Expand Up @@ -1148,7 +1148,7 @@ export default class NPMPlugin implements IPlugin {

auto.hooks.next.tapPromise(
this.name,
async (preReleaseVersions, bump, { dryRun }) => {
async (preReleaseVersions, { bump, dryRun }) => {
if (this.setRcToken) {
await setTokenOnCI(auto.logger);
auto.logger.verbose.info("Set CI NPM_TOKEN");
Expand Down

0 comments on commit 26c47de

Please sign in to comment.