Skip to content

Commit

Permalink
feat: esm (electron)
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Apr 15, 2022
1 parent 0b4d7e3 commit ca8f935
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 31 deletions.
File renamed without changes.
10 changes: 10 additions & 0 deletions electron_entrypoint.cjs
Original file line number Diff line number Diff line change
@@ -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)
}
})()
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
61 changes: 32 additions & 29 deletions src/electron.ts
Original file line number Diff line number Diff line change
@@ -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<SatelliteConfig>()
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
})

Expand All @@ -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,
})
Expand Down Expand Up @@ -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?',
Expand All @@ -149,7 +152,7 @@ function trayQuit() {
client.disconnect(),
devices.close(),
])
app.quit()
electron.app.quit()
}
})
.catch((e) => {
Expand All @@ -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 <br />Supports 2.2.0 and newer',
Expand Down

0 comments on commit ca8f935

Please sign in to comment.