Skip to content

Commit

Permalink
generic refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
MarshallOfSound committed Jun 27, 2024
1 parent 2cf6d60 commit d65e18c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 27 deletions.
4 changes: 2 additions & 2 deletions packages/api/core/helper/dynamic-import.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const url = require('url');
const fs = require('fs');

exports.dynamicImport = function dynamicImport(path) {
exports.dynamicImport = async function dynamicImport(path) {
try {
return import(fs.existsSync(path) ? url.pathToFileURL(path) : path);
return await import(fs.existsSync(path) ? url.pathToFileURL(path) : path);
} catch (error) {
return Promise.reject(error);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/api/core/src/api/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ const d = debug('electron-forge:packager');
/**
* Resolves hooks if they are a path to a file (instead of a `Function`).
*/
function resolveHooks<F = HookFunction>(hooks: (string | F)[] | undefined, dir: string) {
async function resolveHooks<F = HookFunction>(hooks: (string | F)[] | undefined, dir: string) {
if (hooks) {
return Promise.all(hooks.map(async (hook) => (typeof hook === 'string' ? ((await importSearch<F>(dir, [hook])) as F) : hook)));
return await Promise.all(hooks.map(async (hook) => (typeof hook === 'string' ? ((await importSearch<F>(dir, [hook])) as F) : hook)));
}

return [];
Expand Down
2 changes: 1 addition & 1 deletion packages/api/core/src/util/forge-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export default async (dir: string): Promise<ResolvedForgeConfig> => {
const templateObj = { ...packageJSON, year: new Date().getFullYear() };
renderConfigTemplate(dir, templateObj, resolvedForgeConfig);

resolvedForgeConfig.pluginInterface = new PluginInterface(dir, resolvedForgeConfig);
resolvedForgeConfig.pluginInterface = await PluginInterface.create(dir, resolvedForgeConfig);

resolvedForgeConfig = await runMutatingHook(resolvedForgeConfig, 'resolveForgeConfig', resolvedForgeConfig);

Expand Down
56 changes: 34 additions & 22 deletions packages/api/core/src/util/plugin-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,42 @@ function isForgePlugin(plugin: IForgePlugin | unknown): plugin is IForgePlugin {
}

export default class PluginInterface implements IForgePluginInterface {
private plugins: Promise<IForgePlugin>[];
private plugins: IForgePlugin[] = [];
private _pluginPromise: Promise<void> = Promise.resolve();

private config: ResolvedForgeConfig;

constructor(dir: string, forgeConfig: ResolvedForgeConfig) {
this.plugins = forgeConfig.plugins.map(async (plugin) => {
if (isForgePlugin(plugin)) {
return plugin;
}
static async create(dir: string, forgeConfig: ResolvedForgeConfig): Promise<PluginInterface> {
const int = new PluginInterface(dir, forgeConfig);
await int._pluginPromise;
return int;
}

if (typeof plugin === 'object' && 'name' in plugin && 'config' in plugin) {
const { name: pluginName, config: opts } = plugin;
if (typeof pluginName !== 'string') {
throw new Error(`Expected plugin[0] to be a string but found ${pluginName}`);
private constructor(dir: string, forgeConfig: ResolvedForgeConfig) {
this._pluginPromise = Promise.all(
forgeConfig.plugins.map(async (plugin): Promise<IForgePlugin> => {
if (isForgePlugin(plugin)) {
return plugin;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const Plugin = await importSearch<any>(dir, [pluginName]);
if (!Plugin) {
throw new Error(`Could not find module with name: ${pluginName}. Make sure it's listed in the devDependencies of your package.json`);

if (typeof plugin === 'object' && 'name' in plugin && 'config' in plugin) {
const { name: pluginName, config: opts } = plugin;
if (typeof pluginName !== 'string') {
throw new Error(`Expected plugin[0] to be a string but found ${pluginName}`);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const Plugin = await importSearch<any>(dir, [pluginName]);
if (!Plugin) {
throw new Error(`Could not find module with name: ${pluginName}. Make sure it's listed in the devDependencies of your package.json`);
}
return new Plugin(opts);
}
return new Plugin(opts);
}

throw new Error(`Expected plugin to either be a plugin instance or a { name, config } object but found ${JSON.stringify(plugin)}`);
throw new Error(`Expected plugin to either be a plugin instance or a { name, config } object but found ${JSON.stringify(plugin)}`);
})
).then((plugins) => {
this.plugins = plugins;
return;
});
// TODO: fix hack
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -61,15 +73,15 @@ export default class PluginInterface implements IForgePluginInterface {
});

for (const plugin of this.plugins) {
void plugin.then((plugin) => plugin.init(dir, forgeConfig));
plugin.init(dir, forgeConfig);
}

this.triggerHook = this.triggerHook.bind(this);
this.overrideStartLogic = this.overrideStartLogic.bind(this);
}

async triggerHook<Hook extends keyof ForgeSimpleHookSignatures>(hookName: Hook, hookArgs: ForgeSimpleHookSignatures[Hook]): Promise<void> {
for await (const plugin of this.plugins) {
for (const plugin of this.plugins) {
if (typeof plugin.getHooks === 'function') {
let hooks = plugin.getHooks()[hookName] as ForgeSimpleHookFn<Hook>[] | ForgeSimpleHookFn<Hook>;
if (hooks) {
Expand All @@ -89,7 +101,7 @@ export default class PluginInterface implements IForgePluginInterface {
): Promise<ForgeListrTaskDefinition[]> {
const tasks: ForgeListrTaskDefinition[] = [];

for await (const plugin of this.plugins) {
for (const plugin of this.plugins) {
if (typeof plugin.getHooks === 'function') {
let hooks = plugin.getHooks()[hookName] as ForgeSimpleHookFn<Hook>[] | ForgeSimpleHookFn<Hook>;
if (hooks) {
Expand Down Expand Up @@ -123,7 +135,7 @@ export default class PluginInterface implements IForgePluginInterface {
...item: ForgeMutatingHookSignatures[Hook]
): Promise<ForgeMutatingHookSignatures[Hook][0]> {
let result: ForgeMutatingHookSignatures[Hook][0] = item[0];
for await (const plugin of this.plugins) {
for (const plugin of this.plugins) {
if (typeof plugin.getHooks === 'function') {
let hooks = plugin.getHooks()[hookName] as ForgeMutatingHookFn<Hook>[] | ForgeMutatingHookFn<Hook>;
if (hooks) {
Expand All @@ -140,7 +152,7 @@ export default class PluginInterface implements IForgePluginInterface {
async overrideStartLogic(opts: StartOptions): Promise<StartResult> {
let newStartFn;
const claimed: string[] = [];
for await (const plugin of this.plugins) {
for (const plugin of this.plugins) {
if (typeof plugin.startLogic === 'function' && plugin.startLogic !== PluginBase.prototype.startLogic) {
claimed.push(plugin.name);
newStartFn = plugin.startLogic;
Expand Down

0 comments on commit d65e18c

Please sign in to comment.