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
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
20 changes: 14 additions & 6 deletions packages/api/core/src/util/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,29 @@ export const getHookListrTasks = async <
d(`hook triggered: ${hookName}`);
if (typeof hooks[hookName] === 'function') {
d('calling hook:', hookName, 'with args:', hookArgs);
const hook = hooks[hookName] as ForgeSimpleHookFn<Hook>;
const hasName = !!(hook as any).__hookName;
tasks.push({
title: `Running ${chalk.yellow(hookName)} hook from forgeConfig`,
title:
(hook as any).__hookName ||
`Running ${chalk.yellow(hookName)} hook from forgeConfig`,
task: childTrace(
{
name: 'forge-config-hook',
category: '@electron-forge/hooks',
extraDetails: { hook: hookName },
},
async () => {
await (hooks[hookName] as ForgeSimpleHookFn<Hook>)(
forgeConfig,
...hookArgs,
);
async (_, __, task) => {
if (hasName) {
await hook.call(task, forgeConfig, ...hookArgs);
} else {
await hook(forgeConfig, ...hookArgs);
}
},
),
rendererOptions: hasName
? { bottomBar: Infinity, persistentOutput: true }
: {},
});
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/plugin/base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"node": ">= 22.12.0"
},
"dependencies": {
"@electron-forge/core-utils": "workspace:*",
"@electron-forge/shared-types": "workspace:*"
},
"publishConfig": {
Expand Down
34 changes: 1 addition & 33 deletions packages/plugin/base/src/Plugin.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import {
ForgeHookFn,
ForgeHookName,
ForgeListrTask,
ForgeMultiHookMap,
IForgePlugin,
ResolvedForgeConfig,
StartOptions,
StartResult,
} from '@electron-forge/shared-types';

export { createHookWithTask } from '@electron-forge/core-utils';
export { StartOptions };

export default abstract class Plugin<C> implements IForgePlugin {
Expand Down Expand Up @@ -43,34 +41,4 @@ export default abstract class Plugin<C> implements IForgePlugin {
}
}

/* eslint-disable @typescript-eslint/no-explicit-any */
/**
*
* This is a filthy hack around TypeScript to allow internal hooks in our
* internal plugins to have some level of access to the "Task" that Listr2 runs.
* Specifically the ability to set a custom task name and receive the task
*
* This method is not type-safe internally, but is type-safe for consumers.
*
* @internal
*/
export const namedHookWithTaskFn = <Hook extends ForgeHookName>(
hookFn: <Ctx = never>(
task: ForgeListrTask<Ctx> | null,
...args: Parameters<ForgeHookFn<Hook>>
) => ReturnType<ForgeHookFn<Hook>>,
name: string,
): ForgeHookFn<Hook> => {
function namedHookWithTaskInner(
this: ForgeListrTask<any> | null,
...args: any[]
) {
return (hookFn as any)(this, ...args);
}
const fn = namedHookWithTaskInner as any;
fn.__hookName = name;
return fn;
};
/* eslint-enable @typescript-eslint/no-explicit-any */

export { Plugin as PluginBase };
4 changes: 2 additions & 2 deletions packages/plugin/fuses/src/FusesPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'node:path';

import { flipFuses, FuseConfig } from '@electron/fuses';
import { namedHookWithTaskFn, PluginBase } from '@electron-forge/plugin-base';
import { createHookWithTask, PluginBase } from '@electron-forge/plugin-base';
import { ForgeMultiHookMap, ForgePlatform } from '@electron-forge/shared-types';

import { getElectronExecutablePath } from './util/getElectronExecutablePath.js';
Expand All @@ -19,7 +19,7 @@ export default class FusesPlugin extends PluginBase<FuseConfig> {

getHooks(): ForgeMultiHookMap {
return {
packageAfterCopy: namedHookWithTaskFn<'packageAfterCopy'>(
packageAfterCopy: createHookWithTask<'packageAfterCopy'>(
async (
listrTask,
resolvedForgeConfig,
Expand Down
6 changes: 3 additions & 3 deletions packages/plugin/vite/src/VitePlugin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { spawn } from 'node:child_process';
import path from 'node:path';

import { namedHookWithTaskFn, PluginBase } from '@electron-forge/plugin-base';
import { createHookWithTask, PluginBase } from '@electron-forge/plugin-base';
import chalk from 'chalk';
import debug from 'debug';
import fs from 'fs-extra';
Expand Down Expand Up @@ -140,7 +140,7 @@ export default class VitePlugin extends PluginBase<VitePluginConfig> {
getHooks = (): ForgeMultiHookMap => {
return {
preStart: [
namedHookWithTaskFn<'preStart'>(async (task) => {
createHookWithTask<'preStart'>(async (task) => {
if (VitePlugin.alreadyStarted) return;
VitePlugin.alreadyStarted = true;

Expand Down Expand Up @@ -182,7 +182,7 @@ export default class VitePlugin extends PluginBase<VitePluginConfig> {
}, 'Preparing Vite bundles'),
],
prePackage: [
namedHookWithTaskFn<'prePackage'>(async (task) => {
createHookWithTask<'prePackage'>(async (task) => {
this.isProd = true;
await fs.remove(this.baseDir);

Expand Down
6 changes: 3 additions & 3 deletions packages/plugin/webpack/src/WebpackPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
getElectronVersion,
listrCompatibleRebuildHook,
} from '@electron-forge/core-utils';
import { namedHookWithTaskFn, PluginBase } from '@electron-forge/plugin-base';
import { createHookWithTask, PluginBase } from '@electron-forge/plugin-base';
import {
ForgeArch,
ForgeMultiHookMap,
Expand Down Expand Up @@ -195,7 +195,7 @@ export default class WebpackPlugin extends PluginBase<WebpackPluginConfig> {
getHooks(): ForgeMultiHookMap {
return {
preStart: [
namedHookWithTaskFn<'preStart'>(async (task) => {
createHookWithTask<'preStart'>(async (task) => {
if (this.alreadyStarted) return;
this.alreadyStarted = true;

Expand Down Expand Up @@ -230,7 +230,7 @@ export default class WebpackPlugin extends PluginBase<WebpackPluginConfig> {
}, 'Preparing webpack bundles'),
],
prePackage: [
namedHookWithTaskFn<'prePackage'>(
createHookWithTask<'prePackage'>(
async (task, config, platform, arch) => {
if (!task) {
throw new Error(
Expand Down
44 changes: 44 additions & 0 deletions packages/utils/core-utils/src/hook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {
ForgeHookFn,
ForgeHookName,
ForgeListrTask,
} from '@electron-forge/shared-types';

/* eslint-disable @typescript-eslint/no-explicit-any */
/**
* Wraps a hook function to receive the Listr2 task object as the first argument,
* enabling `task.output` and `task.title` updates from within the hook. Also sets
* a custom display name for the task in the Listr2 renderer.
*
* @example
* ```ts
* import { createHookWithTask } from '@electron-forge/core-utils';
*
* export default {
* hooks: {
* prePackage: createHookWithTask(async (task, config, platform, arch) => {
* task.title = 'Preparing native modules';
* task.output = 'Compiling...';
* }, 'Custom prePackage step'),
* },
* };
* ```
*/
export const createHookWithTask = <Hook extends ForgeHookName>(
hookFn: <Ctx = never>(
task: ForgeListrTask<Ctx> | null,
...args: Parameters<ForgeHookFn<Hook>>
) => ReturnType<ForgeHookFn<Hook>>,
name: string,
): ForgeHookFn<Hook> => {
function namedHookWithTaskInner(
this: ForgeListrTask<any> | null,
...args: any[]
) {
return (hookFn as any)(this, ...args);
}
const fn = namedHookWithTaskInner as any;
fn.__hookName = name;
return fn;
};
/* eslint-enable @typescript-eslint/no-explicit-any */
1 change: 1 addition & 0 deletions packages/utils/core-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './hook.js';
export * from './rebuild.js';
export * from './electron-version.js';
export * from './package-manager.js';
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@electron-forge/plugin-base@workspace:packages/plugin/base"
dependencies:
"@electron-forge/core-utils": "workspace:*"
"@electron-forge/shared-types": "workspace:*"
languageName: unknown
linkType: soft
Expand Down