Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat add afterRun hook #2182

Merged
merged 1 commit into from Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/pages/docs/plugins/configuration-hooks.mdx
Expand Up @@ -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) => {
...
}
});
```
2 changes: 2 additions & 0 deletions packages/cli/src/run.ts
Expand Up @@ -136,6 +136,8 @@ export async function execute(command: string, args: ApiOptions) {
}

process.exit(1);
} finally {
await auto.teardown();
}
}

Expand Down
20 changes: 20 additions & 0 deletions packages/core/src/__tests__/auto.test.ts
Expand Up @@ -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", () => {
Expand Down
14 changes: 14 additions & 0 deletions packages/core/src/auto.ts
Expand Up @@ -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`. */
Expand Down Expand Up @@ -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<string> {
const [, configuredRemote = "origin"] = await on(
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/utils/make-hooks.ts
Expand Up @@ -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"]),
Expand Down