forge icon indicating copy to clipboard operation
forge copied to clipboard

A way to set the Config using the @electron-forge/core ForgeAPI

Open IIIMADDINIII opened this issue 1 year ago • 2 comments

Pre-flight checklist

  • [X] I have read the contribution documentation for this project.
  • [X] I agree to follow the code of conduct that this project uses.
  • [X] I have searched the issue tracker for a feature request that matches the one I want to file, without success.

Problem description

I integrated electron forge in to my custom build system and i use the ForgeAPI from the core module. Because i wand to automatically generate the config by my build system i need to use nasty hacks to provide the configuration object to the ForgeAPI.

Proposed solution

My suggestion is, to extend the ForgeAPI Options (MakeOptions, StartOptions, etc.) with an optional config parameter. This parameter would be used instead of trying to find it in the package.json or searching for a forge.config.js file.

Alternatives considered

Currently i monkeypatch fs-extra and hack in to the require cache. A simplified version would be this:

import fs from 'fs-extra';
import * as module from "module";
import { api } from "@electron-forge/core";
import { resolve } from "path";
const require = module.createRequire(import.meta.url);

async function runPackage(config) {
  const origExists = fs.pathExists;
  const target = resolve(process.cwd(), "forge.config.js");
  fs.pathExists = function pathExists(file, cb) {
    if (file === target) {
      return Promise.resolve(true);
    }
    return origExists(file, cb);
  };
  require.cache[target] = { exports: config };
  module._pathCache[target + "\x00"] = target;
  await api.package({});
}

Serializing the config to disk is also no option for me, because i load the Plugins by my self (custom plugins bundled with the build tool) and only provide a reference to the instance.

Additional information

No response

IIIMADDINIII avatar Feb 25 '24 18:02 IIIMADDINIII

This is a somewhat unique use case, you can't override the forge config / package json via forge hooks because you need a config to be loaded first to register those hooks 🤔

Maybe a registerForgeConfigForDirectory exposed API that we can check when trying to resolve forge configs. I don't like the idea of slapping an optional config?: Config argument to every exposed API 🙃

MarshallOfSound avatar Feb 26 '24 09:02 MarshallOfSound

A registerForgeConfigForDirectory API would be fine for me. But then there should be a way to unregister the config again (Maybe calling registerForgeConfigForDirectory with null or undefinied).

I'm happy to implement this my self and create an pull request. As far as i saw it, only the Default Export of util/forge-config.ts and maybe also the util/resolve-dir.ts. My fist idea to implementing this would be to create a global Map in forge-config.ts. Does it make sense, tho raise an Error if a config is registered in a folder, where a config can be found? Or should the registered config overpower an local config or the other way around?

IIIMADDINIII avatar Feb 26 '24 18:02 IIIMADDINIII