Skip to content

Commit

Permalink
feat: builder add win arm64
Browse files Browse the repository at this point in the history
  • Loading branch information
Venipa committed Nov 5, 2024
1 parent ae1dd99 commit 98a3de6
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 20 deletions.
2 changes: 2 additions & 0 deletions electron-builder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ win:
target:
- target: nsis
arch: x64
- target: nsis
arch: arm64
icon: build/icon.ico
nsis:
artifactName: ${name}-${version}_${arch}-setup.${ext}
Expand Down
9 changes: 3 additions & 6 deletions electron.vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import vue from "@vitejs/plugin-vue";
import { BytecodeOptions, bytecodePlugin, defineConfig, externalizeDepsPlugin } from "electron-vite";
import { defineConfig, externalizeDepsPlugin } from "electron-vite";
import { merge } from "lodash-es";
import { resolve } from "path";
import { UserConfigExport } from "vite";
import svgLoader from 'vite-svg-loader';
const bytecodeOptions: BytecodeOptions = {
transformArrowFunctions: false,
}
const resolveOptions: UserConfigExport = {
resolve: {
alias: {
Expand All @@ -24,7 +21,7 @@ const externalizedEsmDeps = ["lodash-es", "@faker-js/faker", "@trpc-limiter/memo
export default defineConfig({
main: {
...resolveOptions,
plugins: [externalizeDepsPlugin({ exclude: [...externalizedEsmDeps] }), bytecodePlugin(bytecodeOptions)],
plugins: [externalizeDepsPlugin({ exclude: [...externalizedEsmDeps] })],
build: {
rollupOptions: {
input: {
Expand All @@ -40,7 +37,7 @@ export default defineConfig({
},
preload: {
...resolveOptions,
plugins: [externalizeDepsPlugin({ exclude: [...externalizedEsmDeps] }), bytecodePlugin(bytecodeOptions)],
plugins: [externalizeDepsPlugin({ exclude: [...externalizedEsmDeps] })],
build: {
rollupOptions: {
input: {
Expand Down
1 change: 0 additions & 1 deletion src/main/api/apiWorkerHelper.ts

This file was deleted.

10 changes: 5 additions & 5 deletions src/main/api/createApiWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ import ApiProvider from "@main/plugins/apiProvider.plugin";
import SettingsProvider from "@main/plugins/settingsProvider.plugin";
import { API_ROUTES } from "@main/utils/eventNames";
import { WorkerAgent } from "@main/utils/worker";
import { apiWorkerModuleId } from "@main/workerPaths";
import logger from "@shared/utils/Logger";
import { BrowserWindow } from "electron";
import modulePathId from "./main?modulePath";
export interface ApiWorker {
send(name: string, ...args: any[]): void;
invoke<T = any>(name: string, ...args: any[]): Promise<T>;
initialize(settings: SettingsProvider["instance"]): Promise<number>;
destroy(): Promise<void>;
}
const agent = () => new WorkerAgent(apiWorkerModuleId);
export const createApiWorker = async (
api: ApiProvider,
parent?: BrowserWindow,
): Promise<ApiWorker> => {
logger.debug("Worker Added", modulePathId);
let worker: WorkerAgent<{ name: string; data?: any }, any> | null = new WorkerAgent(modulePathId);
let worker: WorkerAgent<{ name: string; data?: any }, any> | null = agent();
if (parent) parent.on("close", () => worker!.requestExit());
const apiMap = {
"api/routes": api.getRoutes,
Expand Down Expand Up @@ -65,8 +65,8 @@ export const createApiWorker = async (
}
async destroy() {
if (!worker) return;
await worker.runOperation({ name: "destroy" });
await worker.requestExit();
worker.runOperation({ name: "destroy" });
worker.requestExit();
worker = null;
}
})();
Expand Down
2 changes: 0 additions & 2 deletions src/main/api/main.ts → src/main/api/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { createId as cuid } from "@paralleldrive/cuid2";
import createApp, { json, Router } from "express";
import expressWs from "express-ws";

import { apiChannelName } from "./apiWorkerHelper";

import type { SettingsStore } from "@main/plugins/settingsProvider.plugin";
import { createLogger } from "@shared/utils/console";
Expand Down Expand Up @@ -132,7 +131,6 @@ const sendMessage = async (name: string, ...args: any[]) => {
});
clients.forEach((x) => x.send(data, { binary: false }));
};
const evName = apiChannelName;
const functionCollection = {
close,
destroy: close,
Expand Down
2 changes: 1 addition & 1 deletion src/main/plugins/apiProvider.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default class ApiProvider extends BaseProvider implements AfterInit, OnDe
if (this._thread) await this._thread.destroy();
const config = this.settingsProvider;
if (!config.instance?.api?.enabled) return;
this._thread = await createApiWorker(this.getProvider("api"), this.windowContext.main);
this._thread = await createApiWorker(this, this.windowContext.main);
const tpid = await this._thread.initialize(this.settingsProvider.instance);
this.logger.debug("running thread pid: " + tpid);
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/utils/worker/protocol.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { join } from "node:path";
export { default as workerModuleId } from "./worker-context?modulePath";

export const workerModuleId = join(__dirname, "worker");

export type WorkerData = {
operationModuleId: string;
Expand Down
9 changes: 6 additions & 3 deletions src/main/utils/worker/worker-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ type WorkerEventSubscriptionMethod = (

export class WorkerAgent<TInput, TOutput> {
private readonly worker: Worker;

constructor(operationModuleId: string, logToConsole = false) {
constructor(operationModuleIdOrWorker: string | Worker, logToConsole = false) {
if (operationModuleIdOrWorker instanceof Worker) {
this.worker = operationModuleIdOrWorker;
return;
}
const workerData: WorkerData = {
operationModuleId,
operationModuleId: operationModuleIdOrWorker,
logToConsole
};

Expand Down
95 changes: 95 additions & 0 deletions src/main/utils/worker/worker-context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { createLogger } from "@shared/utils/console";
import { exit } from "node:process";
import { parentPort, workerData } from "node:worker_threads";
import { MessageFromWorker, MessageToWorker, WorkerData } from "./protocol";

const { operationModuleId, logToConsole } = workerData as WorkerData;

const logger = logToConsole ? createLogger(operationModuleId) : undefined;

logger?.info(`###> Trying to load module '${operationModuleId}'...`);

// eslint-disable-next-line @typescript-eslint/no-var-requires
const operation = require(operationModuleId);

logger?.info(`###> Module loaded!`);

parentPort?.on("message", (message: MessageToWorker) => {
switch (message.type) {
case "operationInput":
logger?.info(`###> Now running the operation!`);
runOperation(message.value, message.correlationId);
return;

case "end":
logger?.info(`###> Now exiting worker thread!`);
setImmediate(() => exit(0));
return;
}
});

function runOperation(input: unknown, correlationId?: string): void {
let operationOutput: unknown;

try {
operationOutput = operation(input);
} catch (err: any) {
logger?.error(
`###> Error when calling the worker's operation: `,
err
);

const message: MessageFromWorker = {
correlationId,
type: "error",
formattedError: err
};
parentPort?.postMessage(message);

return;
}

logger?.info(`###> The operation result is: ${operationOutput}`);

if (!(operationOutput instanceof Promise)) {
logger?.info(`###> Sending the result to the parent!`);

const message: MessageFromWorker = {
correlationId,
type: "operationOutput",
value: operationOutput
};
parentPort?.postMessage(message);

return;
}

logger?.info(`###> Waiting for the promise to resolve...`);

operationOutput
.then(value => {
logger?.info(
`###> Promise successful! Sending the result to the parent: ${value}`
);

const message: MessageFromWorker = {
correlationId,
type: "operationOutput",
value
};
parentPort?.postMessage(message);
})
.catch(operationError => {
logger?.error(
`###> Promise failed! Sending an error message to the parent! The error is: `,
operationError
);

const message: MessageFromWorker = {
correlationId,
type: "error",
formattedError: operationError
};
parentPort?.postMessage(message);
});
}
2 changes: 2 additions & 0 deletions src/main/workerPaths.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as apiWorkerModuleId } from "./api/worker?modulePath";

0 comments on commit 98a3de6

Please sign in to comment.