Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion packages/api/core/spec/fast/util/hook.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { ForgeHookFn, ResolvedForgeConfig } from '@electron-forge/shared-types';
import { describe, expect, it, vi } from 'vitest';

import { runHook, runMutatingHook } from '../../../src/util/hook.js';
import {
getHookListrTasks,
runHook,
runMutatingHook,
} from '../../../src/util/hook.js';

const fakeConfig = {
pluginInterface: {
Expand Down Expand Up @@ -35,6 +39,46 @@ describe('runHook', () => {
await runHook({ hooks: { preMake: fn }, ...fakeConfig }, 'preMake');
expect(fn).toHaveBeenCalledOnce();
});

it('should pass null as the task parameter when running outside listr', async () => {
const fn = vi.fn().mockResolvedValue(undefined);
await runHook({ hooks: { preMake: fn }, ...fakeConfig }, 'preMake');
expect(fn).toHaveBeenCalledWith(expect.anything(), null);
});
});

describe('getHookListrTasks', () => {
// A minimal mock of childTrace that preserves the (childTrace, ctx, task) => ... calling convention
const fakeChildTrace: any = (_opts: any, fn: any) => {
return (...args: any[]) => fn(fakeChildTrace, ...args);
};

it('should pass the listr task to the hook function', async () => {
const fn = vi.fn().mockResolvedValue(undefined);
const config = {
...fakeConfig,
hooks: { generateAssets: fn },
pluginInterface: {
...fakeConfig.pluginInterface,
getHookListrTasks: vi.fn().mockResolvedValue([]),
},
} as unknown as ResolvedForgeConfig;

const tasks = await getHookListrTasks(
fakeChildTrace,
config,
'generateAssets',
'darwin',
'x64',
);

expect(tasks).toHaveLength(1);

const fakeTask = { output: '' };
await (tasks[0].task as any)({}, fakeTask);

expect(fn).toHaveBeenCalledWith(config, 'darwin', 'x64', fakeTask);
});
});

describe('runMutatingHook', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/api/core/src/api/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export default autoTrace(

return delayTraceTillSignal(
childTrace,
task.newListr<never>(
task.newListr(
publishers.map((publisher) => ({
title: `${chalk.cyan(`[publisher-${publisher.name}]`)} Running the ${chalk.yellow('publish')} command`,
task: childTrace<Parameters<ForgeListrTaskFn>>(
Expand Down
4 changes: 3 additions & 1 deletion packages/api/core/src/util/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const runHook = async <Hook extends keyof ForgeSimpleHookSignatures>(
await (hooks[hookName] as ForgeSimpleHookFn<Hook>)(
forgeConfig,
...hookArgs,
null,
);
}
}
Expand Down Expand Up @@ -54,10 +55,11 @@ export const getHookListrTasks = async <
category: '@electron-forge/hooks',
extraDetails: { hook: hookName },
},
async () => {
async (_childTrace, _ctx, task) => {
await (hooks[hookName] as ForgeSimpleHookFn<Hook>)(
forgeConfig,
...hookArgs,
task,
);
},
),
Expand Down
4 changes: 2 additions & 2 deletions packages/api/core/src/util/plugin-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export default class PluginInterface implements IForgePluginInterface {
if (hooks) {
if (typeof hooks === 'function') hooks = [hooks];
for (const hook of hooks) {
await hook(this.config, ...hookArgs);
await hook(this.config, ...hookArgs, null);
}
}
}
Expand Down Expand Up @@ -142,7 +142,7 @@ export default class PluginInterface implements IForgePluginInterface {
...(hookArgs as any[]),
);
} else {
await hook(this.config, ...hookArgs);
await hook(this.config, ...hookArgs, task);
}
},
),
Expand Down
5 changes: 4 additions & 1 deletion packages/utils/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ export type ForgeHookName = keyof (ForgeSimpleHookSignatures &
ForgeMutatingHookSignatures);
export type ForgeSimpleHookFn<Hook extends keyof ForgeSimpleHookSignatures> = (
forgeConfig: ResolvedForgeConfig,
...args: ForgeSimpleHookSignatures[Hook]
...args: [
...ForgeSimpleHookSignatures[Hook],
task: ForgeListrTask<never> | null,
]
) => Promise<Listr | void>;
export type ForgeMutatingHookFn<
Hook extends keyof ForgeMutatingHookSignatures,
Expand Down
Loading