Skip to content

Commit

Permalink
Bundle etcher-util with main app
Browse files Browse the repository at this point in the history
  • Loading branch information
dfunckt committed Nov 9, 2023
1 parent 478a7a8 commit 0a396bf
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 25 deletions.
17 changes: 9 additions & 8 deletions forge.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { MakerDMG } from '@electron-forge/maker-dmg';
import { MakerAppImage } from '@reforged/maker-appimage';
import { AutoUnpackNativesPlugin } from '@electron-forge/plugin-auto-unpack-natives';
import { WebpackPlugin } from '@electron-forge/plugin-webpack';
import { ResourcePlugin } from 'electron-forge-resource-plugin';

import { mainConfig, rendererConfig } from './webpack.config';

Expand All @@ -30,13 +31,6 @@ const config: ForgeConfig = {

// osxSign: {},
// osxNotarize: {},

// One or more files to be copied directly into the app's `Contents/Resources` directory
// for macOS target platforms, and the `resources` directory for other target platforms.
// The resources directory can be referenced in code via `process.resourcesPath`.
extraResource: [
// TODO: child writer
],
},
rebuildConfig: {},
makers: [
Expand Down Expand Up @@ -152,8 +146,15 @@ const config: ForgeConfig = {
],
},
}),
new ResourcePlugin({
env: 'ETCHER_UTIL_BIN_PATH',
path: `out/sidecar/bin/etcher-util${process.platform === 'win32' ? '.exe' : ''}`,
build: {
command: 'npm rebuild mountutils && tsc --project tsconfig.sidecar.json && pkg out/sidecar/util/api.js -c pkg-sidecar.json --target node18 --output out/sidecar/bin/etcher-util',
sources: './lib/util/',
},
}),
],
};

export default config;

7 changes: 3 additions & 4 deletions lib/gui/app/modules/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import * as os from 'os';
import * as path from 'path';
import * as packageJSON from '../../../../package.json';
import * as permissions from '../../../shared/permissions';
import { getAppPath } from '../../../shared/get-app-path';
import * as errors from '../../../shared/errors';

const THREADS_PER_CPU = 16;
Expand All @@ -27,8 +26,8 @@ const THREADS_PER_CPU = 16;
// the stdout maxBuffer size to be exceeded when flashing
ipc.config.silent = true;

function writerArgv(): string[] {
let entryPoint = path.join(getAppPath(), 'generated', 'etcher-util');
async function writerArgv(): Promise<string[]> {
let entryPoint = await window.etcher.getEtcherUtilPath();
// AppImages run over FUSE, so the files inside the mount point
// can only be accessed by the user that mounted the AppImage.
// This means we can't re-spawn Etcher as root from the same
Expand Down Expand Up @@ -75,7 +74,7 @@ async function spawnChild({
IPC_SERVER_ID: string;
IPC_SOCKET_ROOT: string;
}) {
const argv = writerArgv();
const argv = await writerArgv();
const env = writerEnv(IPC_CLIENT_ID, IPC_SERVER_ID, IPC_SOCKET_ROOT);
if (withPrivileges) {
return await permissions.elevateCommand(argv, {
Expand Down
10 changes: 10 additions & 0 deletions lib/gui/app/preload.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
// See the Electron documentation for details on how to use preload scripts:
// https://www.electronjs.org/docs/latest/tutorial/process-model#preload-scripts

import * as webapi from '../webapi';

declare global {
interface Window {
etcher: typeof webapi;
}
}

window['etcher'] = webapi;
17 changes: 17 additions & 0 deletions lib/gui/etcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,23 @@ electron.app.on('before-quit', () => {
process.exit(EXIT_CODES.SUCCESS);
});

// this is replaced at build-time with the path to etcher-util
declare const ETCHER_UTIL_BIN_PATH: string;

electron.ipcMain.handle('get-util-path', () => {
// Workaround the fact that the Resource forge plugin hardcodes
// the resource path to `resources` which is only valid on Linux
// and Windows builds -- it fails on macOS.
//
// We also can't just fallback to `process.resourcesPath` because
// that will always point to Electron's resource folder, which in
// dev builds using the Electron binary from node modules is not
// at all what we want, since our build artifacts reside in `/out`.
return process.env.NODE_ENV === 'development'
? ETCHER_UTIL_BIN_PATH
: ETCHER_UTIL_BIN_PATH.replace('resources', process.resourcesPath);
});

async function main(): Promise<void> {
if (!electron.app.requestSingleInstanceLock()) {
electron.app.quit();
Expand Down
13 changes: 13 additions & 0 deletions lib/gui/webapi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// Anything exported from this module will become available to the
// renderer process via preload. They're accessible as `window.etcher.foo()`.
//

import { ipcRenderer } from 'electron';

// FIXME: this is a workaround for the renderer to be able to find the etcher-util
// binary. We should instead export a function that asks the main process to launch
// the binary itself.
export async function getEtcherUtilPath(): Promise<string> {
return await ipcRenderer.invoke('get-util-path');
}
10 changes: 0 additions & 10 deletions lib/pkg-sidekick.json

This file was deleted.

19 changes: 19 additions & 0 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
"url": "[email protected]:balena-io/etcher.git"
},
"scripts": {
"build:rebuild-mountutils": "cd node_modules/mountutils && npm rebuild",
"build:sidecar": "npm run build:rebuild-mountutils && tsc --project tsconfig.sidecar.json && pkg build/util/api.js -c pkg-sidecar.json --target node18 --output generated/etcher-util",
"flowzone-preinstall-linux": "sudo apt-get update && sudo apt-get install -y xvfb libudev-dev && cat < electron-builder.yml | yq e .deb.depends[] - | xargs -L1 echo | sed 's/|//g' | xargs -L1 sudo apt-get --ignore-missing install || true",
"flowzone-preinstall-macos": "true",
"flowzone-preinstall-windows": "npx node-gyp install",
Expand Down Expand Up @@ -106,6 +104,7 @@
"chai": "4.3.7",
"css-loader": "5.2.7",
"electron": "^25.8.2",
"electron-forge-resource-plugin": "^1.0.0-alpha.6",
"electron-mocha": "^11.0.2",
"electron-notarize": "1.2.2",
"electron-rebuild": "^3.2.9",
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.sidecar.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"moduleResolution": "Node",
"resolveJsonModule": true,
"isolatedModules": true,
"outDir": "build"
"outDir": "out/sidecar"
},
"include": ["lib/util"]
}

0 comments on commit 0a396bf

Please sign in to comment.