From 895cfaed87787b66f9d8365427c61df6afa936ee Mon Sep 17 00:00:00 2001 From: Julien Bouyoud Date: Mon, 30 May 2022 18:06:25 +0200 Subject: [PATCH] feat(core): add `afterRun` hook --- .../docs/plugins/configuration-hooks.mdx | 12 +++++++++++ packages/cli/src/run.ts | 2 ++ packages/core/src/__tests__/auto.test.ts | 20 +++++++++++++++++++ packages/core/src/auto.ts | 14 +++++++++++++ packages/core/src/utils/make-hooks.ts | 1 + 5 files changed, 49 insertions(+) diff --git a/docs/pages/docs/plugins/configuration-hooks.mdx b/docs/pages/docs/plugins/configuration-hooks.mdx index baf3a251d..10d027e2e 100644 --- a/docs/pages/docs/plugins/configuration-hooks.mdx +++ b/docs/pages/docs/plugins/configuration-hooks.mdx @@ -139,3 +139,15 @@ auto.hooks.validateConfig.tapPromise("test", (name, options) => { } }); ``` + +## afterRun + +Happens after any command run. +This is a great place to trigger post-actions. + +```ts +auto.hooks.afterRun.tapPromise("afterCheck", async (config) => { + ... + } +}); +``` diff --git a/packages/cli/src/run.ts b/packages/cli/src/run.ts index 2ac702430..2484c0429 100644 --- a/packages/cli/src/run.ts +++ b/packages/cli/src/run.ts @@ -136,6 +136,8 @@ export async function execute(command: string, args: ApiOptions) { } process.exit(1); + } finally { + await auto.teardown(); } } diff --git a/packages/core/src/__tests__/auto.test.ts b/packages/core/src/__tests__/auto.test.ts index 44878d494..ef3df3196 100644 --- a/packages/core/src/__tests__/auto.test.ts +++ b/packages/core/src/__tests__/auto.test.ts @@ -1601,6 +1601,26 @@ describe("Auto", () => { expect(spy).not.toHaveBeenCalled(); }); }); + + describe("teardown", () => { + test("should throw when not initialized", async () => { + const auto = new Auto({ ...defaults, plugins: [] }); + auto.logger = dummyLog(); + + await expect(auto.teardown()).rejects.not.toBeUndefined(); + }); + + test("should call afterRun hooks", async () => { + const auto = new Auto({ ...defaults, plugins: [] }); + auto.logger = dummyLog(); + + const afterRun = jest.fn(); + auto.hooks.afterRun.tap("test", afterRun); + await auto.loadConfig(); + + await expect(auto.teardown()).resolves.toBeUndefined(); + }); + }); }); describe("hooks", () => { diff --git a/packages/core/src/auto.ts b/packages/core/src/auto.ts index b6d5ce0b6..795bc839a 100644 --- a/packages/core/src/auto.ts +++ b/packages/core/src/auto.ts @@ -151,6 +151,8 @@ export interface IAutoHooks { validateConfig: ValidatePluginHook; /** Happens before anything is done. This is a great place to check for platform specific secrets. */ beforeRun: AsyncSeriesHook<[LoadedAutoRc]>; + /** Happens after everything else is done. This is a great place to trigger post-actions. */ + afterRun: AsyncSeriesHook<[LoadedAutoRc]>; /** Happens before `shipit` is run. This is a great way to throw an error if a token or key is not present. */ beforeShipIt: AsyncSeriesHook<[BeforeShipitContext]>; /** Ran before the `changelog` command commits the new release notes to `CHANGELOG.md`. */ @@ -663,6 +665,18 @@ export default class Auto { return config; } + /** + * Gracefully teardown auto + */ + async teardown() { + if (!this.config) { + throw this.createErrorMessage(); + } + + this.logger.verbose.success("Teardown `auto`"); + await this.hooks.afterRun.promise(this.config); + } + /** Determine the remote we have auth to push to. */ private async getRemote(): Promise { const [, configuredRemote = "origin"] = await on( diff --git a/packages/core/src/utils/make-hooks.ts b/packages/core/src/utils/make-hooks.ts index c2abab99e..12df2540b 100644 --- a/packages/core/src/utils/make-hooks.ts +++ b/packages/core/src/utils/make-hooks.ts @@ -14,6 +14,7 @@ import { InteractiveInitHooks } from "../init"; /** Make the hooks for "auto" */ export const makeHooks = (): IAutoHooks => ({ beforeRun: new AsyncSeriesHook(["config"]), + afterRun: new AsyncSeriesHook(["config"]), modifyConfig: new AsyncSeriesWaterfallHook(["config"]), validateConfig: new AsyncSeriesBailHook(["name", "options"]), beforeShipIt: new AsyncSeriesHook(["context"]),