Skip to content

Commit

Permalink
feat(core): add afterRun hook
Browse files Browse the repository at this point in the history
  • Loading branch information
jBouyoud committed Feb 9, 2023
1 parent 4e7c06f commit 895cfae
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
12 changes: 12 additions & 0 deletions docs/pages/docs/plugins/configuration-hooks.mdx
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 895cfae

Please sign in to comment.