-
-
Notifications
You must be signed in to change notification settings - Fork 630
feat: add Fuses plugin #3132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add Fuses plugin #3132
Changes from all commits
88c0343
caeda9b
b369ca2
76758a9
9ae9d9a
e39b49c
59ab522
c19afc9
04f27ae
ac1d56f
5db23d9
820ccf1
929432e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| @electron-forge/plugin-fuses | ||
| ===== | ||
|
|
||
| This plugin allows flipping [Electron Fuses](https://github.com/electron/fuses) when packaging your app with Electron Forge. | ||
|
|
||
| ## Usage | ||
|
|
||
| Install `@electron-forge/plugin-fuses` and `@electron/fuses` as dev dependencies and add this plugin to the `plugins` array in your Forge configuration:: | ||
| ```shell | ||
| # Yarn | ||
| yarn add --dev @electron-forge/plugin-fuses @electron/fuses | ||
|
|
||
| # npm | ||
| npm i -D @electron-forge/plugin-fuses @electron/fuses | ||
| ``` | ||
|
|
||
| ```js | ||
| // forge.config.js | ||
|
|
||
| const { FusesPlugin } = require('@electron-forge/plugin-fuses'); | ||
| const { FuseV1Options, FuseVersion } = require('@electron/fuses'); | ||
|
|
||
| const forgeConfig = { | ||
| plugins: [ | ||
| new FusesPlugin({ | ||
| version: FuseVersion.V1, | ||
| [FuseV1Options.RunAsNode]: false, | ||
| // ...any other options supported by @electron/fuses | ||
| }), | ||
| ], | ||
| }; | ||
|
|
||
| module.exports = forgeConfig; | ||
| ``` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| { | ||
| "name": "@electron-forge/plugin-fuses", | ||
| "version": "6.0.4", | ||
| "description": "A plugin for flipping Electron Fuses in Electron Forge", | ||
| "repository": "https://github.com/electron/forge", | ||
| "author": "Erik Moura <erikian@erikian.dev>", | ||
| "license": "MIT", | ||
| "main": "dist/FusesPlugin.js", | ||
| "files": [ | ||
| "dist", | ||
| "package.json", | ||
| "README.md" | ||
| ], | ||
| "typings": "dist/FusesPlugin.d.ts", | ||
| "scripts": { | ||
| "test": "xvfb-maybe mocha --config ../../../.mocharc.js test/**/*_spec*.ts" | ||
| }, | ||
| "devDependencies": { | ||
| "@electron/fuses": ">=1.0.0", | ||
| "@malept/cross-spawn-promise": "^2.0.0", | ||
| "chai": "^4.3.3", | ||
| "fs-extra": "^10.0.0", | ||
| "mocha": "^9.0.1", | ||
| "xvfb-maybe": "^0.2.1" | ||
| }, | ||
| "peerDependencies": { | ||
| "@electron/fuses": ">=1.0.0" | ||
| }, | ||
| "engines": { | ||
| "node": ">= 14.17.5" | ||
| }, | ||
| "dependencies": { | ||
| "@electron-forge/plugin-base": "6.0.4", | ||
| "@electron-forge/shared-types": "6.0.4" | ||
| }, | ||
| "publishConfig": { | ||
| "access": "public" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import path from 'path'; | ||
|
|
||
| import { namedHookWithTaskFn, PluginBase } from '@electron-forge/plugin-base'; | ||
| import { ForgeMultiHookMap } from '@electron-forge/shared-types'; | ||
| import { flipFuses, FuseConfig } from '@electron/fuses'; | ||
|
|
||
| import { getElectronExecutablePath } from './util/getElectronExecutablePath'; | ||
|
|
||
| export default class FusesPlugin extends PluginBase<FuseConfig> { | ||
| name = 'fuses'; | ||
|
|
||
| fusesConfig = {} as FuseConfig; | ||
|
|
||
| constructor(fusesConfig: FuseConfig) { | ||
| super(fusesConfig); | ||
|
|
||
| this.fusesConfig = fusesConfig; | ||
| } | ||
|
|
||
| getHooks(): ForgeMultiHookMap { | ||
| return { | ||
| packageAfterCopy: namedHookWithTaskFn<'packageAfterCopy'>(async (listrTask, resolvedForgeConfig, resourcesPath, electronVersion, platform, arch) => { | ||
| const { fusesConfig } = this; | ||
|
|
||
| if (Object.keys(fusesConfig).length) { | ||
| const pathToElectronExecutable = getElectronExecutablePath({ | ||
| appName: ['darwin', 'mas'].includes(platform) ? 'Electron' : 'electron', | ||
| basePath: path.resolve(resourcesPath, '../..'), | ||
| platform, | ||
| }); | ||
|
|
||
| const osxSignConfig = resolvedForgeConfig.packagerConfig.osxSign; | ||
| const hasOSXSignConfig = (typeof osxSignConfig === 'object' && Boolean(Object.keys(osxSignConfig).length)) || Boolean(osxSignConfig); | ||
|
|
||
| await flipFuses(pathToElectronExecutable, { | ||
| resetAdHocDarwinSignature: !hasOSXSignConfig && platform === 'darwin' && arch === 'arm64', | ||
| ...this.fusesConfig, | ||
| }); | ||
| } | ||
| }, 'Flipping Fuses'), | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| export { FusesPlugin }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import path from 'path'; | ||
|
|
||
| import { ForgePlatform } from '@electron-forge/shared-types'; | ||
|
|
||
| type GetElectronExecutablePathParams = { | ||
| appName: string; | ||
| basePath: string; | ||
| platform: ForgePlatform; | ||
| }; | ||
|
|
||
| export function getElectronExecutablePath({ appName, basePath, platform }: GetElectronExecutablePathParams): string { | ||
| return path.join( | ||
| basePath, | ||
| ['darwin', 'mas'].includes(platform) ? path.join('MacOS', appName) : [appName, process.platform === 'win32' ? '.exe' : ''].join('') | ||
| ); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import fs from 'fs'; | ||
| import path from 'path'; | ||
|
|
||
| import { CrossSpawnOptions, spawn } from '@malept/cross-spawn-promise'; | ||
| import { expect } from 'chai'; | ||
| import fsExtra from 'fs-extra'; | ||
|
|
||
| import { getElectronExecutablePath } from '../src/util/getElectronExecutablePath'; | ||
|
|
||
| describe('FusesPlugin', () => { | ||
| const appPath = path.join(__dirname, 'fixtures', 'app'); | ||
|
|
||
| const spawnOptions: CrossSpawnOptions = { | ||
| cwd: appPath, | ||
| shell: true, | ||
| }; | ||
|
|
||
| const packageJSON = JSON.parse( | ||
| fs.readFileSync(path.join(appPath, 'package.json'), { | ||
| encoding: 'utf-8', | ||
| }) | ||
| ); | ||
|
|
||
| const { name: appName } = packageJSON; | ||
|
|
||
| // @TODO get rid of this once https://github.com/electron/forge/pull/3123 lands | ||
| const platformArchSuffix = `${process.platform}-x64`; | ||
|
|
||
| const outDir = path.join(appPath, 'out', `${appName}-${platformArchSuffix}`); | ||
|
|
||
| before(async () => { | ||
| delete process.env.TS_NODE_PROJECT; | ||
| await spawn('yarn', ['install'], spawnOptions); | ||
| }); | ||
|
|
||
| after(async () => { | ||
| await fsExtra.remove(path.resolve(outDir, '../')); | ||
|
|
||
| // @TODO this can be removed once the mock app installs a published version of @electron-forge/plugin-fuses instead of a local package | ||
| await fsExtra.remove(path.join(__dirname, './fixtures/app/node_modules')); | ||
| }); | ||
|
|
||
| it('should flip Fuses', async () => { | ||
| await spawn('yarn', ['package'], spawnOptions); | ||
|
|
||
| const electronExecutablePath = getElectronExecutablePath({ | ||
| appName, | ||
| basePath: path.join(outDir, ...(process.platform === 'darwin' ? [`${appName}.app`, 'Contents'] : [])), | ||
| platform: process.platform, | ||
| }); | ||
|
|
||
| /** | ||
| * If the `RunAsNode` fuse had not been flipped, | ||
| * this would return the Node.js version (e.g. `v14.16.0`) | ||
| * instead of the `console.log` from `main.js`. | ||
| */ | ||
| const output = ( | ||
| await spawn(electronExecutablePath, ['-v'], { | ||
| env: { | ||
| ELECTRON_RUN_AS_NODE: '1', | ||
| }, | ||
| }) | ||
| ).trim(); | ||
|
|
||
| expect(output).to.equals('The Fuses plugin is working'); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import path from 'path'; | ||
|
|
||
| import { FusesPlugin } from '@electron-forge/plugin-fuses'; | ||
| import { ForgeConfig } from '@electron-forge/shared-types'; | ||
| import { FuseV1Options, FuseVersion } from '@electron/fuses'; | ||
| import fsExtra from 'fs-extra'; | ||
|
|
||
| const forgeConfig: ForgeConfig = { | ||
| packagerConfig: { | ||
| afterComplete: [ | ||
| // makes tests a bit simpler by having a single output directory in every platform/arch | ||
| async (packagedAppLocation, _electronVersion, _targetPlatform, _targetArch, done) => { | ||
| const parentDir = path.resolve(packagedAppLocation, '..'); | ||
| await fsExtra.move(packagedAppLocation, path.join(parentDir, 'fuses-test-app'), { | ||
| overwrite: true, | ||
| }); | ||
|
|
||
| done(); | ||
| }, | ||
| ], | ||
| }, | ||
|
|
||
| plugins: [ | ||
| new FusesPlugin({ | ||
| version: FuseVersion.V1, | ||
| [FuseV1Options.RunAsNode]: false, | ||
| }), | ||
| ], | ||
| }; | ||
|
|
||
| export default forgeConfig; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| { | ||
| "name": "fuses-test-app", | ||
| "version": "1.0.0", | ||
| "main": "./src/main.js", | ||
| "scripts": { | ||
| "package": "electron-forge package" | ||
| }, | ||
| "dependencies": { | ||
| "@electron-forge/plugin-fuses": "file:./../../../../fuses", | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if there's a better way to test unreleased plugins |
||
| "@electron-forge/shared-types": "6.0.4", | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ESLint throws a |
||
| "@electron/fuses": "^1.6.1", | ||
| "fs-extra": "^10.0.0" | ||
| }, | ||
| "devDependencies": { | ||
| "@electron-forge/cli": "6.0.4", | ||
| "electron": "12.0.0" | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| const { app } = require('electron'); | ||
|
|
||
| console.log('The Fuses plugin is working'); | ||
|
|
||
| app.exit(0); |
Uh oh!
There was an error while loading. Please reload this page.