refactor(forge): switch to TypeScript

This commit is contained in:
Elian Doran 2025-06-19 15:38:35 +03:00
parent 0744a85421
commit 248f6d6a7d
No known key found for this signature in database
6 changed files with 43 additions and 19 deletions

View File

@ -12,7 +12,7 @@ on:
paths: paths:
- .github/actions/build-electron/* - .github/actions/build-electron/*
- .github/workflows/nightly.yml - .github/workflows/nightly.yml
- forge.config.cjs - forge.config.ts
concurrency: concurrency:
group: ${{ github.workflow }}-${{ github.ref }} group: ${{ github.workflow }}-${{ github.ref }}

View File

@ -1,11 +1,11 @@
const path = require("path"); import path from "path";
const fs = require("fs-extra"); import fs from "fs-extra";
const { LOCALES } = require("@triliumnext/commons"); import { LOCALES } from "@triliumnext/commons";
import { PRODUCT_NAME } from "../src/app-info.js";
const ELECTRON_FORGE_DIR = __dirname; const ELECTRON_FORGE_DIR = __dirname;
const EXECUTABLE_NAME = "trilium"; // keep in sync with server's package.json -> packagerConfig.executableName const EXECUTABLE_NAME = "trilium"; // keep in sync with server's package.json -> packagerConfig.executableName
const { PRODUCT_NAME } = require("../src/app-info.js");
const APP_ICON_PATH = path.join(ELECTRON_FORGE_DIR, "app-icon"); const APP_ICON_PATH = path.join(ELECTRON_FORGE_DIR, "app-icon");
const extraResourcesForPlatform = getExtraResourcesForPlatform(); const extraResourcesForPlatform = getExtraResourcesForPlatform();
@ -147,13 +147,13 @@ module.exports = {
const isMac = (process.platform === "darwin"); const isMac = (process.platform === "darwin");
let localesToKeep = LOCALES let localesToKeep = LOCALES
.filter(locale => !locale.contentOnly) .filter(locale => !locale.contentOnly)
.map(locale => locale.electronLocale); .map(locale => locale.electronLocale) as string[];
if (!isMac) { if (!isMac) {
localesToKeep = localesToKeep.map(locale => locale.replace("_", "-")) localesToKeep = localesToKeep.map(locale => locale.replace("_", "-"))
} }
const keptLocales = new Set(); const keptLocales = new Set();
const removedLocales = []; const removedLocales: string[] = [];
const extension = (isMac ? ".lproj" : ".pak"); const extension = (isMac ? ".lproj" : ".pak");
for (const outputPath of packageResult.outputPaths) { for (const outputPath of packageResult.outputPaths) {
@ -169,39 +169,39 @@ module.exports = {
console.log(`No locales directory found in '${localeDir}'.`); console.log(`No locales directory found in '${localeDir}'.`);
process.exit(2); process.exit(2);
} }
const files = fs.readdirSync(localeDir); const files = fs.readdirSync(localeDir);
for (const file of files) { for (const file of files) {
if (!file.endsWith(extension)) { if (!file.endsWith(extension)) {
continue; continue;
} }
let localeName = path.basename(file, extension); let localeName = path.basename(file, extension);
if (localeName === "en-US" && !isMac) { if (localeName === "en-US" && !isMac) {
// If the locale is "en-US" on Windows, we treat it as "en". // If the locale is "en-US" on Windows, we treat it as "en".
// This is because the Windows version of Electron uses "en-US.pak" instead of "en.pak". // This is because the Windows version of Electron uses "en-US.pak" instead of "en.pak".
localeName = "en"; localeName = "en";
} }
if (localesToKeep.includes(localeName)) { if (localesToKeep.includes(localeName)) {
keptLocales.add(localeName); keptLocales.add(localeName);
continue; continue;
} }
const filePath = path.join(localeDir, file); const filePath = path.join(localeDir, file);
if (isMac) { if (isMac) {
fs.rm(filePath, { recursive: true }); fs.rm(filePath, { recursive: true });
} else { } else {
fs.unlinkSync(filePath); fs.unlinkSync(filePath);
} }
removedLocales.push(file); removedLocales.push(file);
} }
} }
} }
console.log(`Removed unused locale files: ${removedLocales.join(", ")}`); console.log(`Removed unused locale files: ${removedLocales.join(", ")}`);
// Ensure all locales that should be kept are actually present. // Ensure all locales that should be kept are actually present.
for (const locale of localesToKeep) { for (const locale of localesToKeep) {
@ -229,7 +229,7 @@ module.exports = {
if (TRILIUM_ARTIFACT_NAME_HINT) { if (TRILIUM_ARTIFACT_NAME_HINT) {
fileName = TRILIUM_ARTIFACT_NAME_HINT.replaceAll("/", "-") + extension; fileName = TRILIUM_ARTIFACT_NAME_HINT.replaceAll("/", "-") + extension;
} }
const outputPath = path.join(outputDir, fileName); const outputPath = path.join(outputDir, fileName);
console.log(`[Artifact] ${artifactPath} -> ${outputPath}`); console.log(`[Artifact] ${artifactPath} -> ${outputPath}`);
fs.copyFileSync(artifactPath, outputPath); fs.copyFileSync(artifactPath, outputPath);
@ -240,7 +240,7 @@ module.exports = {
}; };
function getExtraResourcesForPlatform() { function getExtraResourcesForPlatform() {
const resources = []; const resources: string[] = [];
const getScriptResources = () => { const getScriptResources = () => {
const scripts = ["trilium-portable", "trilium-safe-mode", "trilium-no-cert-check"]; const scripts = ["trilium-portable", "trilium-safe-mode", "trilium-no-cert-check"];

View File

@ -29,7 +29,7 @@
"prebuild-install": "^7.1.1" "prebuild-install": "^7.1.1"
}, },
"config": { "config": {
"forge": "./electron-forge/forge.config.cjs" "forge": "./electron-forge/forge.config.ts"
}, },
"scripts": { "scripts": {
"start-prod": "nx build desktop && cross-env TRILIUM_DATA_DIR=data TRILIUM_RESOURCE_DIR=dist TRILIUM_PORT=37841 electron dist/main.js" "start-prod": "nx build desktop && cross-env TRILIUM_DATA_DIR=data TRILIUM_RESOURCE_DIR=dist TRILIUM_PORT=37841 electron dist/main.js"

View File

@ -0,0 +1,23 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"module": "ESNext",
"moduleResolution": "bundler",
"target": "ES2020",
"outDir": "dist",
"types": [
"node",
"express"
],
"rootDir": "electron-forge",
"tsBuildInfoFile": "dist/tsconfig.app.tsbuildinfo"
},
"include": [
"**/*.ts"
],
"exclude": [
"eslint.config.js",
"eslint.config.cjs",
"eslint.config.mjs"
]
}

View File

@ -11,6 +11,9 @@
}, },
{ {
"path": "./tsconfig.app.json" "path": "./tsconfig.app.json"
},
{
"path": "./tsconfig.forge.json"
} }
] ]
} }

View File

@ -169,8 +169,6 @@
comment = meta.description; comment = meta.description;
desktopName = "TriliumNext Notes"; desktopName = "TriliumNext Notes";
categories = [ "Office" ]; categories = [ "Office" ];
# TODO: electron-forge build has this set to PRODUCT_NAME (forge.config.cjs)
# But the plain build doesn't set this (or the app icon).
startupWMClass = "TriliumNext Notes"; startupWMClass = "TriliumNext Notes";
}) })
]; ];