From ca8f9351c3a68d2017e09ba4cba47ad5435dc169 Mon Sep 17 00:00:00 2001 From: Julian Waller Date: Fri, 15 Apr 2022 14:49:27 +0100 Subject: [PATCH] feat: esm (electron) --- .eslintrc.js => .eslintrc.cjs | 0 electron_entrypoint.cjs | 10 ++++++ package.json | 4 +-- src/electron.ts | 61 ++++++++++++++++++----------------- 4 files changed, 44 insertions(+), 31 deletions(-) rename .eslintrc.js => .eslintrc.cjs (100%) create mode 100644 electron_entrypoint.cjs diff --git a/.eslintrc.js b/.eslintrc.cjs similarity index 100% rename from .eslintrc.js rename to .eslintrc.cjs diff --git a/electron_entrypoint.cjs b/electron_entrypoint.cjs new file mode 100644 index 0000000..3314745 --- /dev/null +++ b/electron_entrypoint.cjs @@ -0,0 +1,10 @@ +// Electron doesn't support ESM main files, so we need to use this file to be our conversion point +;(async function () { + try { + // Pass electron as a parameter as it can't be imported in esm files + await import('./dist/electron.js') + } catch (e) { + console.error(`Failed to start: ${e}`) + process.exit(1) + } +})() diff --git a/package.json b/package.json index d21edae..a720150 100644 --- a/package.json +++ b/package.json @@ -15,14 +15,14 @@ "url": "https://github.com/bitfocus/companion-satellite/issues" }, "homepage": "https://github.com/bitfocus/companion-satellite#readme", - "main": "dist/electron.js", + "main": "electron_entrypoint.cjs", "type": "module", "license": "MIT", "private": true, "scripts": { "prepare": "husky install", "dev": "yarn ts-node-esm src/main.ts", - "dev-electron": "yarn build:main && electron dist/electron.js", + "dev-electron": "yarn build:main && electron electron_entrypoint.cjs", "dist:prepare:sharp": "cd node_modules/sharp && rimraf vendor && node install/libvips && node install/dll-copy", "electron-rebuild": "yarn dist:prepare:sharp && electron-builder install-app-deps", "build": "rimraf dist && yarn build:main", diff --git a/src/electron.ts b/src/electron.ts index fbc504d..cc6e72a 100644 --- a/src/electron.ts +++ b/src/electron.ts @@ -1,20 +1,21 @@ -// eslint-disable-next-line node/no-unpublished-import -import { app, Tray, Menu, MenuItem, dialog } from 'electron' -import * as path from 'path' -// eslint-disable-next-line node/no-unpublished-import -import * as electronStore from 'electron-store' -// eslint-disable-next-line node/no-unpublished-import -import * as prompt from 'electron-prompt' -// eslint-disable-next-line node/no-unpublished-import -import openAboutWindow from 'electron-about-window' -import { DeviceManager } from './devices' -import { CompanionSatelliteClient } from './client' -import { DEFAULT_PORT } from './lib' +import electronStore from 'electron-store' +import prompt from 'electron-prompt' +import { DeviceManager } from './devices.js' +import { CompanionSatelliteClient } from './client.js' +import { DEFAULT_PORT } from './lib.js' +import { fileURLToPath } from 'url' + +import { createRequire } from 'module' +const require = createRequire(import.meta.url) + +// Hack to resolve import not working correctly.. +const openAboutWindow: typeof import('electron-about-window')['default'] = require('electron-about-window').default +const electron: typeof import('electron') = require('electron') const store = new electronStore() -let tray: Tray | undefined +let tray: import('electron').Tray | undefined -app.on('window-all-closed', () => { +electron.app.on('window-all-closed', () => { // Block default behaviour of exit on close }) @@ -39,44 +40,46 @@ function tryConnect() { } } -app.whenReady().then(function () { +electron.app.whenReady().then(function () { console.log('App ready') tryConnect() - tray = new Tray( - process.platform == 'darwin' - ? path.join(__dirname, '../assets', 'trayTemplate.png') - : path.join(__dirname, '../assets', 'icon.png') + tray = new electron.Tray( + fileURLToPath( + process.platform == 'darwin' + ? new URL('../assets/trayTemplate.png', import.meta.url) + : new URL('../assets/icon.png', import.meta.url) + ) ) - const menu = new Menu() + const menu = new electron.Menu() menu.append( - new MenuItem({ + new electron.MenuItem({ label: 'Change Host', click: changeHost, }) ) menu.append( - new MenuItem({ + new electron.MenuItem({ label: 'Change Port', click: changePort, }) ) menu.append( - new MenuItem({ + new electron.MenuItem({ label: 'Scan devices', click: trayScanDevices, }) ) menu.append( - new MenuItem({ + new electron.MenuItem({ label: 'About', click: trayAbout, }) ) menu.append( - new MenuItem({ + new electron.MenuItem({ label: 'Quit', click: trayQuit, }) @@ -135,7 +138,7 @@ function changePort() { function trayQuit() { console.log('quit click') - dialog + electron.dialog .showMessageBox({ title: 'Companion Satellite', message: 'Are you sure you want to quit Companion Satellite?', @@ -149,7 +152,7 @@ function trayQuit() { client.disconnect(), devices.close(), ]) - app.quit() + electron.app.quit() } }) .catch((e) => { @@ -163,9 +166,9 @@ function trayScanDevices() { } function trayAbout() { - console.log('about click') + console.log('about click', openAboutWindow) openAboutWindow({ - icon_path: path.join(__dirname, '../assets', 'icon.png'), + icon_path: fileURLToPath(new URL('../assets/icon.png', import.meta.url)), product_name: 'Companion Satellite', use_inner_html: true, description: 'Satellite Streamdeck connector for Bitfocus Companion
Supports 2.2.0 and newer',