Skip to content

Commit

Permalink
Add git custom hook
Browse files Browse the repository at this point in the history
  • Loading branch information
yoannmoinet committed Feb 21, 2025
1 parent 52560cf commit 459a5ed
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export type CustomHooks = {
init?: HookFn<[GlobalContext]>;
buildReport?: HookFn<[BuildReport]>;
bundlerReport?: HookFn<[BundlerReport]>;
git?: HookFn<[RepositoryData]>;
};

export type PluginOptions = Assign<
Expand Down
1 change: 1 addition & 0 deletions packages/plugins/git/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const getGitPlugins = (options: Options, context: GlobalContext): PluginO
// Add git information to the context.
const repositoryData = await getRepositoryData(await newSimpleGit(context.cwd));
context.git = repositoryData;
await context.asyncHook('git', context.git);
} catch (e: any) {
// We don't want to have the build fail for this.
log.error(`Could not get git information: ${e.message}`);
Expand Down
17 changes: 17 additions & 0 deletions packages/tests/src/unit/plugins/git/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ describe('Git Plugin', () => {

// Intercept contexts to verify it at the moment they're used.
const gitReports: Record<string, RepositoryData | undefined> = {};
const gitHookReports: Record<string, RepositoryData | undefined> = {};
// Need to store it here as the mock gets cleared between tests (and beforeAll).
let nbCallsToGetRepositoryData = 0;
let cleanup: CleanupFn;
Expand All @@ -61,6 +62,16 @@ describe('Git Plugin', () => {
// We need sourcemaps to trigger the git plugin.
sourcemaps: getSourcemapsConfiguration(),
},
customPlugins: (opts, context) => {
return [
{
name: 'custom-test-hook-plugin',
git(repoData) {
gitHookReports[context.bundler.fullName] = repoData;
},
},
];
},
};

uploadSourcemapsMocked.mockImplementation((options, context, log) => {
Expand All @@ -84,6 +95,12 @@ describe('Git Plugin', () => {
expect(nbCallsToGetRepositoryData).toBe(BUNDLERS.length);
});

test.each(BUNDLERS)('[$name|$version] Should call the git hook.', async ({ name }) => {
const gitReport = gitHookReports[name];
expect(gitReport).toBeDefined();
expect(gitReport).toMatchObject(mockGitData);
});

test.each(BUNDLERS)(
'[$name|$version] Should add data to the context.',
async ({ name }) => {
Expand Down

0 comments on commit 459a5ed

Please sign in to comment.