Skip to content
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

:electron: Electron package Typescript starting point #2880

Merged
merged 51 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
ba236c5
ts electron start
MikesGlitch Jun 13, 2024
21bb9bc
bits
MikesGlitch Jun 14, 2024
376efc6
remove comments
MikesGlitch Jun 14, 2024
152a68f
it works now
MikesGlitch Jun 15, 2024
f3ad072
reverting some bits to minimise changes in this pr
MikesGlitch Jun 16, 2024
feae66a
some types
MikesGlitch Jun 16, 2024
da030a9
updates
MikesGlitch Jun 16, 2024
44cab82
add release notes
MikesGlitch Jun 16, 2024
fe6ab8a
fix type
MikesGlitch Jun 16, 2024
955e26d
ensuring the build is removed when cleaning
MikesGlitch Jun 16, 2024
4aedf0b
removed modules
MikesGlitch Jun 16, 2024
78dd41f
fix the scripts
MikesGlitch Jun 16, 2024
e4ae91a
update release notes
MikesGlitch Jun 16, 2024
f9c040d
fix missing icons
MikesGlitch Jun 16, 2024
bddb0a9
copy the dir damnit
MikesGlitch Jun 16, 2024
761bf9a
copyfiles to the rescue
MikesGlitch Jun 16, 2024
aff758a
comment
MikesGlitch Jun 16, 2024
fabdeb9
remove old code
MikesGlitch Jun 16, 2024
b69a2da
update to build a dist folder instead of client-build outside the folder
MikesGlitch Jun 18, 2024
965826d
fix merge
MikesGlitch Jun 18, 2024
e78dd76
icons too. Forgotten after changup
MikesGlitch Jun 18, 2024
66fd34b
ts electron start
MikesGlitch Jun 13, 2024
0aaa2d2
bits
MikesGlitch Jun 14, 2024
0569f49
remove comments
MikesGlitch Jun 14, 2024
acb22b8
it works now
MikesGlitch Jun 15, 2024
0068630
reverting some bits to minimise changes in this pr
MikesGlitch Jun 16, 2024
b27224a
some types
MikesGlitch Jun 16, 2024
65ee306
updates
MikesGlitch Jun 16, 2024
b7861e4
add release notes
MikesGlitch Jun 16, 2024
2a25d20
fix type
MikesGlitch Jun 16, 2024
ab82c2b
ensuring the build is removed when cleaning
MikesGlitch Jun 16, 2024
3f4c20d
removed modules
MikesGlitch Jun 16, 2024
6d8ee91
fix the scripts
MikesGlitch Jun 16, 2024
1b66946
update release notes
MikesGlitch Jun 16, 2024
ff39d54
fix missing icons
MikesGlitch Jun 16, 2024
ff34566
copy the dir damnit
MikesGlitch Jun 16, 2024
7a855f3
copyfiles to the rescue
MikesGlitch Jun 16, 2024
4d2532a
comment
MikesGlitch Jun 16, 2024
3d6c72b
remove old code
MikesGlitch Jun 16, 2024
a773770
update to build a dist folder instead of client-build outside the folder
MikesGlitch Jun 18, 2024
9d0d9e6
fix merge
MikesGlitch Jun 18, 2024
f320dae
icons too. Forgotten after changup
MikesGlitch Jun 18, 2024
c787dd2
Merge branch 'maintenance/ts-electron' of https://github.com/MikesGli…
MikesGlitch Jun 23, 2024
84967fd
remove requires
MikesGlitch Jun 23, 2024
a7af91e
Merge branch 'master' of https://github.com/MikesGlitch/actual into m…
MikesGlitch Jun 23, 2024
f145be0
Merge branch 'master' of https://github.com/MikesGlitch/actual into m…
MikesGlitch Jun 27, 2024
b434fd5
cleaning up menu
MikesGlitch Jun 27, 2024
b924d05
extra safety
MikesGlitch Jun 27, 2024
18f902e
satisfy the type
MikesGlitch Jun 27, 2024
8509e72
Merge branch 'master' into maintenance/ts-electron
MikesGlitch Jul 3, 2024
d856160
keep client-build in gitignore
MikesGlitch Jul 3, 2024
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ packages/api/dist
packages/api/@types
packages/crdt/dist
packages/desktop-electron/client-build
packages/desktop-electron/build
MikesGlitch marked this conversation as resolved.
Show resolved Hide resolved
packages/desktop-electron/.electron-symbols
packages/desktop-electron/dist
packages/desktop-electron/loot-core
Expand Down
13 changes: 7 additions & 6 deletions packages/desktop-electron/bin/update-client
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

ROOT=`dirname $0`/..

rm -rf ${ROOT}/client-build
cp -r ${ROOT}/../desktop-client/build ${ROOT}/client-build
rm -rf ${ROOT}/build
mkdir -p ${ROOT}/build
cp -r ${ROOT}/../desktop-client/build ${ROOT}/build/client-build
MikesGlitch marked this conversation as resolved.
Show resolved Hide resolved

# Remove the embedded backend for the browser version. Will improve
# this process
rm -rf ${ROOT}/client-build/data
rm -rf ${ROOT}/client-build/*kcab.*
rm -rf ${ROOT}/client-build/*.wasm
rm -rf ${ROOT}/client-build/*.map
rm -rf ${ROOT}/build/client-build/data
rm -rf ${ROOT}/build/client-build/*kcab.*
rm -rf ${ROOT}/build/client-build/*.wasm
rm -rf ${ROOT}/build/client-build/*.map
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
/* eslint-disable import/order */
// (I have no idea why the imports are like this. Not touching them.)
const isDev = require('electron-is-dev');
const fs = require('fs');
import fs from 'fs';
import NodeModule from 'module';
import path from 'path';

require('module').globalPaths.push(__dirname + '/..');

const {
import {
app,
ipcMain,
BrowserWindow,
Expand All @@ -14,8 +11,28 @@ const {
shell,
protocol,
utilityProcess,
} = require('electron');
const promiseRetry = require('promise-retry');
UtilityProcess,
} from 'electron';
import isDev from 'electron-is-dev';
import promiseRetry from 'promise-retry';

import about from './about';
import getMenu from './menu';
import updater from './updater';
import {
get as getWindowState,
listen as listenToWindowState,
} from './window-state';

import './setRequireHook';

import './security';

const Module: typeof NodeModule & { globalPaths: string[] } =
// eslint-disable-next-line @typescript-eslint/no-explicit-any
NodeModule as unknown as any;

Module.globalPaths.push(__dirname + '/..');

// This allows relative URLs to be resolved to app:// which makes
// local assets load correctly
Expand All @@ -25,16 +42,6 @@ protocol.registerSchemesAsPrivileged([

global.fetch = require('node-fetch');

const about = require('./about');
const getMenu = require('./menu');
const updater = require('./updater');

require('./security');

const path = require('path');

require('./setRequireHook');

if (!isDev || !process.env.ACTUAL_DOCUMENT_DIR) {
process.env.ACTUAL_DOCUMENT_DIR = app.getPath('documents');
}
Expand All @@ -43,15 +50,12 @@ if (!isDev || !process.env.ACTUAL_DATA_DIR) {
process.env.ACTUAL_DATA_DIR = app.getPath('userData');
}

// eslint-disable-next-line import/extensions
const WindowState = require('./window-state.js');

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let clientWin;
let serverProcess;
let clientWin: BrowserWindow | null;
let serverProcess: UtilityProcess | null;

updater.onEvent((type, data) => {
updater.onEvent((type: string, data: Record<string, string> | string) => {
// Notify both the app and the about window
if (clientWin) {
clientWin.webContents.send(type, data);
Expand Down Expand Up @@ -99,7 +103,7 @@ function createBackgroundProcess() {
}

async function createWindow() {
const windowState = await WindowState.get();
const windowState = await getWindowState();

// Create the browser window.
const win = new BrowserWindow({
Expand All @@ -113,7 +117,6 @@ async function createWindow() {
nodeIntegrationInWorker: false,
nodeIntegrationInSubFrames: false,
contextIsolation: true,
enableRemoteModule: false,
preload: __dirname + '/preload.js',
MikesGlitch marked this conversation as resolved.
Show resolved Hide resolved
},
});
Expand All @@ -123,7 +126,7 @@ async function createWindow() {
win.webContents.openDevTools();
}

const unlistenToState = WindowState.listen(win, windowState);
const unlistenToState = listenToWindowState(win, windowState);

if (isDev) {
win.loadURL(`file://${__dirname}/loading.html`);
Expand All @@ -148,9 +151,11 @@ async function createWindow() {
});

win.on('focus', async () => {
const url = clientWin.webContents.getURL();
if (url.includes('app://') || url.includes('localhost:')) {
clientWin.webContents.executeJavaScript('__actionsForMenu.focused()');
if (clientWin) {
const url = clientWin.webContents.getURL();
if (url.includes('app://') || url.includes('localhost:')) {
clientWin.webContents.executeJavaScript('__actionsForMenu.focused()');
}
}
});

Expand Down Expand Up @@ -183,29 +188,28 @@ async function createWindow() {
clientWin = win;
}

function isExternalUrl(url) {
function isExternalUrl(url: string) {
return !url.includes('localhost:') && !url.includes('app://');
}

function updateMenu(budgetId) {
function updateMenu(budgetId?: string) {
const isBudgetOpen = !!budgetId;
const menu = getMenu(isDev, createWindow);
const menu = getMenu(isDev, createWindow, budgetId);
const file = menu.items.filter(item => item.label === 'File')[0];
const fileItems = file.submenu.items;
const fileItems = file.submenu?.items || [];
fileItems
.filter(item => item.label === 'Load Backup...')
.forEach(item => {
item.enabled = isBudgetOpen;
item.budgetId = budgetId;
});
MikesGlitch marked this conversation as resolved.
Show resolved Hide resolved

const tools = menu.items.filter(item => item.label === 'Tools')[0];
tools.submenu.items.forEach(item => {
tools.submenu?.items.forEach(item => {
item.enabled = isBudgetOpen;
});

const edit = menu.items.filter(item => item.label === 'Edit')[0];
const editItems = edit.submenu.items;
const editItems = edit.submenu?.items || [];
editItems
.filter(item => item.label === 'Undo' || item.label === 'Redo')
.map(item => (item.enabled = isBudgetOpen));
Expand Down Expand Up @@ -312,7 +316,7 @@ ipcMain.handle(
async (event, { title, defaultPath, fileContents }) => {
const fileLocation = await dialog.showSaveDialog({ title, defaultPath });

return new Promise((resolve, reject) => {
return new Promise<void>((resolve, reject) => {
if (fileLocation) {
fs.writeFile(fileLocation.filePath, fileContents, error => {
return reject(error);
Expand Down Expand Up @@ -344,8 +348,9 @@ ipcMain.on('screenshot', () => {
const width = 1100;

// This is for the main screenshot inside the frame
clientWin.setSize(width, Math.floor(width * (427 / 623)));
// clientWin.setSize(width, Math.floor(width * (495 / 700)));
if (clientWin) {
clientWin.setSize(width, Math.floor(width * (427 / 623)));
}
}
});

Expand All @@ -366,14 +371,15 @@ ipcMain.on('apply-update', () => {
updater.apply();
});

ipcMain.on('update-menu', (event, budgetId) => {
ipcMain.on('update-menu', (event, budgetId?: string) => {
updateMenu(budgetId);
});

ipcMain.on('set-theme', theme => {
const obj = { theme };

clientWin.webContents.executeJavaScript(
`window.__actionsForMenu && window.__actionsForMenu.saveGlobalPrefs(${obj})`,
);
if (clientWin) {
clientWin.webContents.executeJavaScript(
`window.__actionsForMenu && window.__actionsForMenu.saveGlobalPrefs(${obj})`,
);
}
});
6 changes: 3 additions & 3 deletions packages/desktop-electron/menu.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { Menu, ipcMain, app, shell } = require('electron');

function getMenu(isDev, createWindow) {
function getMenu(isDev, createWindow, budgetId) {
const template = [
{
label: 'File',
Expand All @@ -9,10 +9,10 @@ function getMenu(isDev, createWindow) {
label: 'Load Backup...',
enabled: false,
click(item, focusedWindow) {
if (focusedWindow) {
if (focusedWindow && budgetId) {
if (focusedWindow.webContents.getTitle() === 'Actual') {
focusedWindow.webContents.executeJavaScript(
`__actionsForMenu.replaceModal('load-backup', { budgetId: '${item.budgetId}' })`,
`__actionsForMenu.replaceModal('load-backup', { budgetId: '${budgetId}' })`,
);
}
}
Expand Down
16 changes: 11 additions & 5 deletions packages/desktop-electron/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
"scripts": {
"clean": "rm -rf dist",
"update-client": "bin/update-client",
"build": "electron-builder",
"watch": "cross-env ACTUAL_DOCUMENT_DIR=\"../../data\" ACTUAL_DATA_DIR=\"../../data\" electron ."
"build": "yarn build:dist && electron-builder",
"build:dist": "tsc --p tsconfig.dist.json && yarn copy-static-html",
"copy-static-html": "copyfiles --exclude 'build/**/*' **/*.html icons/**/* build",
"watch": "yarn build:dist && cross-env ACTUAL_DOCUMENT_DIR=\"../../data\" ACTUAL_DATA_DIR=\"../../data\" electron ."
},
"main": "index.js",
"main": "build/index.js",
"build": {
"appId": "com.actualbudget.actual",
"files": [
Expand All @@ -19,7 +21,8 @@
"!**/*.js.map",
"!node_modules/@jlongster/sql.js",
"!node_modules/absurd-sql",
"!node_modules/better-sqlite3/{benchmark,src,bin,docs,deps,build/Release/obj,build/Release/sqlite3.a,build/Release/test_extension.node}"
"!node_modules/better-sqlite3/{benchmark,src,bin,docs,deps,build/Release/obj,build/Release/sqlite3.a,build/Release/test_extension.node}",
"build"
],
"publish": {
"provider": "github",
Expand Down Expand Up @@ -63,8 +66,11 @@
"devDependencies": {
"@electron/notarize": "2.2.0",
"@electron/rebuild": "3.6.0",
"@types/copyfiles": "^2",
"copyfiles": "^2.4.1",
"cross-env": "^7.0.3",
"electron": "30.0.6",
"electron-builder": "24.13.3"
"electron-builder": "24.13.3",
"typescript": "^5.0.2"
}
}
14 changes: 14 additions & 0 deletions packages/desktop-electron/tsconfig.dist.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
// Using ES2021 because that’s the newest version where
// the latest Node 16.x release supports all of the features
"target": "ES2021",
"module": "CommonJS",
"noEmit": false,
"declaration": true,
"outDir": "build",
},
"include": ["."],
"exclude": ["**/node_modules/*", "build/**/*"]
}
6 changes: 6 additions & 0 deletions upcoming-release-notes/2880.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Maintenance
authors: [MikesGlitch]
---

Refactoring desktop-electron package to use typescript
Loading
Loading