electron / forge

:electron: A complete tool for building and publishing Electron applications
https://electronforge.io
MIT License
6.49k stars 519 forks source link

Feature Request: Support file association properties in forge.config.js #3339

Open KevinHughes opened 1 year ago

KevinHughes commented 1 year ago

Pre-flight checklist

Problem description

Other electron build systems allow the developer to specify custom file extensions for their built apps. For instance, if I wrote an app that was a editor for "Foo" files, I would want to be able to launch a file with the extension .foo in the system that would then open the application with that file. As a developer I could then handle the file's contents via the app open-file event handler. In addition, .foo files should have their own specific icon.

Currently providing this information via platform-specific configuration files (such as for Windows, OSX, and Linux) is cumbersome and the method for doing so is not always well documented, making cross-platform applications that much more difficult to create.

Proposed solution

I would suggest a set of forge configuration properties such as:

module.exports = {
  packagerConfig: {
    "fileAssociations": [
      {
        "ext": "foo",
        "name": "Foo File",
        "icon": "assets/icon/foofile.icns"
      }
    ],
  }
}

...where one or more file associations can be specified.

Refer to the fileAssociations section of https://www.electron.build/configuration/configuration.html for more information. Note that their set of configuration properties allows this feature to be used in OSX, Windows, and Linux applications.

Alternatives considered

Currently in order to implement this in OSX, a separate Info.plist file must be provided that contains the correct metadata to enable one or more file associations to work. In Windows a separate file containing file association properties must be generated as well. This is cumbersome for developers, who must currently deal with many sets of documentation to implement this feature on multiple platforms.

Additional information

No response

vandervidi commented 7 months ago

Im waiting for this too! It was so easy to do with electron-builder..

@KevinHughes Can you explain how to setup file association with a info.plist file? i assume there are more configurations to setup and its not just as easy as only creating the info.plist file with the proper settings.

davidcann commented 7 months ago

Can you explain how to setup file association with a info.plist file?

@vandervidi – For Info.plist, you can use the extendInfo option to add anything, like this:

module.exports = {
  packagerConfig: {
    extendInfo: {
      CFBundleDocumentTypes: [
        {
          CFBundleTypeName: "MyTextEditor",
          CFBundleTypeRole: "Editor",
          LSTypeIsPackage: true,
          LSHandlerRank: "Default",
          CFBundleTypeExtensions: ["txt"],
        },
      ],
    }
  }
};
mmarczell-graphisoft commented 4 months ago

Windows would be tricky because "regular" win32 applications have to register these in the registry while Microsoft Store applications have to declare them in the appx manifest.

fras2560 commented 2 months ago

For Windows if you are using the Wix-Maker you can do the following:

interface ExtendedMakerWixConfig extends MakerWixConfig {
  // see https://github.com/electron/forge/issues/3673
  // this is an undocumented property of electron-wix-msi
  associateExtensions?: string;
}
...
{
  config: {
     icon: path.join(process.cwd(), 'assets', 'icon.ico'), // I needed this path to be absolute
     associateExtensions: 'ext',
  },
  beforeCreate: (creator: WixCreator) => {
    // manually convert app icon to some other icon
    // manipulate the fileAssociationTemplate 
  },
} satisfies ExtendedMakerWixConfig,