-
-
Notifications
You must be signed in to change notification settings - Fork 523
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: better internal type safety for hooks
- Loading branch information
1 parent
e404bf1
commit dfcaadc
Showing
15 changed files
with
202 additions
and
1,916 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,47 @@ | ||
import { ResolvedForgeConfig } from '@electron-forge/shared-types'; | ||
import { | ||
ForgeMutatingHookFn, | ||
ForgeMutatingHookSignatures, | ||
ForgeSimpleHookFn, | ||
ForgeSimpleHookSignatures, | ||
ResolvedForgeConfig, | ||
} from '@electron-forge/shared-types'; | ||
import debug from 'debug'; | ||
|
||
const d = debug('electron-forge:hook'); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export const runHook = async (forgeConfig: ResolvedForgeConfig, hookName: string, ...hookArgs: any[]): Promise<void> => { | ||
export const runHook = async <Hook extends keyof ForgeSimpleHookSignatures>( | ||
forgeConfig: ResolvedForgeConfig, | ||
hookName: Hook, | ||
...hookArgs: ForgeSimpleHookSignatures[Hook] | ||
): Promise<void> => { | ||
const { hooks } = forgeConfig; | ||
if (hooks) { | ||
d(`hook triggered: ${hookName}`); | ||
if (typeof hooks[hookName] === 'function') { | ||
d('calling hook:', hookName, 'with args:', hookArgs); | ||
await hooks[hookName](forgeConfig, ...hookArgs); | ||
await (hooks[hookName] as ForgeSimpleHookFn<Hook>)(forgeConfig, ...hookArgs); | ||
} | ||
} | ||
await forgeConfig.pluginInterface.triggerHook(hookName, hookArgs); | ||
}; | ||
|
||
export async function runMutatingHook<T>(forgeConfig: ResolvedForgeConfig, hookName: string, item: T): Promise<T> { | ||
export async function runMutatingHook<Hook extends keyof ForgeMutatingHookSignatures>( | ||
forgeConfig: ResolvedForgeConfig, | ||
hookName: Hook, | ||
...item: ForgeMutatingHookSignatures[Hook] | ||
): Promise<ForgeMutatingHookSignatures[Hook][0]> { | ||
const { hooks } = forgeConfig; | ||
if (hooks) { | ||
d(`hook triggered: ${hookName}`); | ||
if (typeof hooks[hookName] === 'function') { | ||
d('calling mutating hook:', hookName, 'with item:', item); | ||
const result = await hooks[hookName](forgeConfig, item); | ||
d('calling mutating hook:', hookName, 'with item:', item[0]); | ||
const hook = hooks[hookName] as ForgeMutatingHookFn<Hook>; | ||
const result = await hook(forgeConfig, ...item); | ||
if (typeof result !== 'undefined') { | ||
item = result; | ||
item[0] = result; | ||
} | ||
} | ||
} | ||
return forgeConfig.pluginInterface.triggerMutatingHook(hookName, item); | ||
return forgeConfig.pluginInterface.triggerMutatingHook(hookName, item[0]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 6 additions & 7 deletions
13
packages/plugin/auto-unpack-natives/src/AutoUnpackNativesPlugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.