Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import './main/serial-printer';
import './main/usb-printer';
import './main/printer-discovery';
import './main/basePath';
import './main/appVersion';
import './main/open-external-url';

// enabled logging when in development
Expand All @@ -30,7 +28,7 @@
// }

// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) {

Check warning on line 31 in src/index.ts

View workflow job for this annotation

GitHub Actions / test

A `require()` style import is forbidden
app.quit();
}

Expand Down
9 changes: 0 additions & 9 deletions src/main/appVersion.ts

This file was deleted.

13 changes: 0 additions & 13 deletions src/main/basePath.ts

This file was deleted.

6 changes: 5 additions & 1 deletion src/main/window.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import * as path from 'path';

import { BrowserWindow, shell } from 'electron';
import { app, BrowserWindow, shell } from 'electron';
import serve from 'electron-serve';

import { logger as log } from './log';
import { isDevelopment } from './util';

// Keep in sync with src/preload.ts.
const APP_VERSION_ARG_PREFIX = '--wcpos-app-version=';

// Set up electron-serve
let loadURL: (window: BrowserWindow) => void;

Expand Down Expand Up @@ -35,6 +38,7 @@ export const createWindow = (): void => {
sandbox: false, // Required for preload script to work
nodeIntegration: false, // Prevent Node.js integration for security reasons
contextIsolation: true, // Protect against prototype pollution
additionalArguments: [`${APP_VERSION_ARG_PREFIX}${app.getVersion()}`],
},
backgroundColor: '#fff',
});
Expand Down
27 changes: 21 additions & 6 deletions src/preload.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import assert from 'node:assert/strict';
import Module from 'node:module';
import path from 'node:path';

const exposures: Record<string, any> = {};
const onCalls: {
Expand All @@ -12,6 +13,8 @@ const removeListenerCalls: {
channel: string;
listener: (...args: unknown[]) => void;
}[] = [];
const APP_VERSION_ARG_PREFIX = '--wcpos-app-version=';
const mockResourcesPath = path.join('/mock', 'resources');

const electronMock = {
contextBridge: {
Expand All @@ -21,12 +24,6 @@ const electronMock = {
},
ipcRenderer: {
sendSync(channel: string) {
if (channel === 'getBasePathSync') {
return '/mock-base-path';
}
if (channel === 'getAppVersionSync') {
return '0.0.0-test';
}
throw new Error(`Unexpected sendSync channel: ${channel}`);
},
send() {},
Expand Down Expand Up @@ -77,12 +74,30 @@ async function waitFor(condition: () => boolean, message: string) {

async function main() {
try {
Object.defineProperty(process, 'resourcesPath', {
value: mockResourcesPath,
configurable: true,
});
process.argv.push(`${APP_VERSION_ARG_PREFIX}0.0.0-test`);
// eslint-disable-next-line @typescript-eslint/no-require-imports
require('./preload');
} finally {
mutableModule._load = originalLoad;
}

const exposedElectron = exposures.electron;
assert.ok(exposedElectron, 'preload should expose window.electron');
assert.equal(
exposedElectron.basePath,
`file://${path.join(mockResourcesPath, 'dist')}`,
'preload should expose the resources dist base path without a trailing slash'
);
assert.equal(
exposedElectron.version,
'0.0.0-test',
'preload should expose the app version from Electron additionalArguments'
);

const exposedIpcRenderer = exposures.ipcRenderer;
assert.ok(exposedIpcRenderer, 'preload should expose window.ipcRenderer');
assert.equal(
Expand Down
15 changes: 12 additions & 3 deletions src/preload.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import path from 'path';

import { contextBridge, ipcRenderer, IpcRendererEvent } from 'electron';

import {
Expand All @@ -7,16 +9,23 @@ import {
serializeRxdbIpcMessage,
} from './rxdb-ipc-attachments';

// Keep in sync with src/main/window.ts.
const APP_VERSION_ARG_PREFIX = '--wcpos-app-version=';
const versionArg = process.argv.find((arg) => arg.startsWith(APP_VERSION_ARG_PREFIX));
const version = versionArg ? versionArg.slice(APP_VERSION_ARG_PREFIX.length) : '0.0.0';

// No trailing slash: the Expo bundle puts the slash at the start of split asset paths.
const basePath = `file://${path.join(process.resourcesPath, 'dist')}`;

/**
* Expose app info to the renderer process.
*
* @NOTE - These are synchronous calls, they will block the thread, but they're quick calls.
* basePath is needed for the bundle splitting to work correctly.
* version is needed for app-info utility to report correct electron version.
*/
contextBridge.exposeInMainWorld('electron', {
basePath: ipcRenderer.sendSync('getBasePathSync'),
version: ipcRenderer.sendSync('getAppVersionSync'),
basePath,
version,
});

// Must match IPC_RENDERER_KEY_PREFIX from 'rxdb/plugins/electron' used in src/main/rxdb-storage.ts
Expand Down
Loading