Skip to content

armgit / Pake

Public Imported from GitHub
0

Turn any website into a lightweight desktop app with one command, on macOS, Windows and Linux.

fix: keep default tray icon when custom icon copy fails

Luís Miguelcb1e51d5fb94
diff --git a/bin/helpers/merge.ts b/bin/helpers/merge.ts
index 10f58750..d3aa3c20 100644
--- a/bin/helpers/merge.ts
+++ b/bin/helpers/merge.ts
@@ -208,6 +208,45 @@ Terminal=false
}
}
+export async function resolveSystemTrayIconPath(
+ systemTrayIcon: string,
+ defaultTrayIconPath: string,
+ safeAppName: string,
+ iconOutputDir = path.join(npmDirectory, 'src-tauri/png'),
+): Promise<string> {
+ if (systemTrayIcon.length === 0) {
+ return defaultTrayIconPath;
+ }
+
+ try {
+ const iconExt = path.extname(systemTrayIcon).toLowerCase();
+ if (iconExt !== '.png' && iconExt !== '.ico') {
+ logger.warn(
+ `✼ System tray icon must be .ico or .png, but you provided ${iconExt}.`,
+ );
+ logger.warn(`✼ Default system tray icon will be used.`);
+ return defaultTrayIconPath;
+ }
+
+ if (!(await fsExtra.pathExists(systemTrayIcon))) {
+ logger.warn(`✼ System tray icon "${systemTrayIcon}" was not found.`);
+ logger.warn(`✼ Default system tray icon will be used.`);
+ return defaultTrayIconPath;
+ }
+
+ const trayIconPath = `png/${safeAppName}${iconExt}`;
+ const trayIcoPath = path.join(iconOutputDir, `${safeAppName}${iconExt}`);
+ await fsExtra.copy(systemTrayIcon, trayIcoPath);
+ return trayIconPath;
+ } catch (err) {
+ logger.warn(
+ `✼ Failed to apply system tray icon "${systemTrayIcon}": ${err instanceof Error ? err.message : String(err)}`,
+ );
+ logger.warn(`✼ Default system tray icon will remain unchanged.`);
+ return defaultTrayIconPath;
+ }
+}
+
async function mergeIcons(
options: PakeAppOptions,
name: string,
@@ -285,32 +324,13 @@ async function mergeIcons(
}
// Set tray icon path.
- let trayIconPath =
+ const defaultTrayIconPath =
platform === 'darwin' ? 'png/icon_512.png' : tauriConf.bundle.icon![0];
- if (options.systemTrayIcon.length > 0) {
- try {
- await fsExtra.pathExists(options.systemTrayIcon);
- const iconExt = path.extname(options.systemTrayIcon).toLowerCase();
- if (iconExt === '.png' || iconExt === '.ico') {
- const trayIcoPath = path.join(
- npmDirectory,
- `src-tauri/png/${safeAppName}${iconExt}`,
- );
- trayIconPath = `png/${safeAppName}${iconExt}`;
- await fsExtra.copy(options.systemTrayIcon, trayIcoPath);
- } else {
- logger.warn(
- `✼ System tray icon must be .ico or .png, but you provided ${iconExt}.`,
- );
- logger.warn(`✼ Default system tray icon will be used.`);
- }
- } catch (err) {
- logger.warn(
- `✼ Failed to apply system tray icon "${options.systemTrayIcon}": ${err instanceof Error ? err.message : String(err)}`,
- );
- logger.warn(`✼ Default system tray icon will remain unchanged.`);
- }
- }
+ const trayIconPath = await resolveSystemTrayIconPath(
+ options.systemTrayIcon,
+ defaultTrayIconPath,
+ safeAppName,
+ );
tauriConf.pake.system_tray_path = trayIconPath;
delete tauriConf.app.trayIcon;
diff --git a/dist/cli.js b/dist/cli.js
index a90a44ab..791c668c 100644
--- a/dist/cli.js
+++ b/dist/cli.js
@@ -649,6 +649,33 @@ Terminal=false
logger.warn(`✼ The target must be one of ${validTargets.join(', ')}, the default 'deb' will be used.`);
}
}
+async function resolveSystemTrayIconPath(systemTrayIcon, defaultTrayIconPath, safeAppName, iconOutputDir = path.join(npmDirectory, 'src-tauri/png')) {
+ if (systemTrayIcon.length === 0) {
+ return defaultTrayIconPath;
+ }
+ try {
+ const iconExt = path.extname(systemTrayIcon).toLowerCase();
+ if (iconExt !== '.png' && iconExt !== '.ico') {
+ logger.warn(`✼ System tray icon must be .ico or .png, but you provided ${iconExt}.`);
+ logger.warn(`✼ Default system tray icon will be used.`);
+ return defaultTrayIconPath;
+ }
+ if (!(await fsExtra.pathExists(systemTrayIcon))) {
+ logger.warn(`✼ System tray icon "${systemTrayIcon}" was not found.`);
+ logger.warn(`✼ Default system tray icon will be used.`);
+ return defaultTrayIconPath;
+ }
+ const trayIconPath = `png/${safeAppName}${iconExt}`;
+ const trayIcoPath = path.join(iconOutputDir, `${safeAppName}${iconExt}`);
+ await fsExtra.copy(systemTrayIcon, trayIcoPath);
+ return trayIconPath;
+ }
+ catch (err) {
+ logger.warn(`✼ Failed to apply system tray icon "${systemTrayIcon}": ${err instanceof Error ? err.message : String(err)}`);
+ logger.warn(`✼ Default system tray icon will remain unchanged.`);
+ return defaultTrayIconPath;
+ }
+}
async function mergeIcons(options, name, tauriConf, platform, safeAppName) {
const platformIconMap = {
win32: {
@@ -709,26 +736,8 @@ async function mergeIcons(options, name, tauriConf, platform, safeAppName) {
tauriConf.bundle.icon = [iconInfo.defaultIcon];
}
// Set tray icon path.
- let trayIconPath = platform === 'darwin' ? 'png/icon_512.png' : tauriConf.bundle.icon[0];
- if (options.systemTrayIcon.length > 0) {
- try {
- await fsExtra.pathExists(options.systemTrayIcon);
- const iconExt = path.extname(options.systemTrayIcon).toLowerCase();
- if (iconExt === '.png' || iconExt === '.ico') {
- const trayIcoPath = path.join(npmDirectory, `src-tauri/png/${safeAppName}${iconExt}`);
- trayIconPath = `png/${safeAppName}${iconExt}`;
- await fsExtra.copy(options.systemTrayIcon, trayIcoPath);
- }
- else {
- logger.warn(`✼ System tray icon must be .ico or .png, but you provided ${iconExt}.`);
- logger.warn(`✼ Default system tray icon will be used.`);
- }
- }
- catch (err) {
- logger.warn(`✼ Failed to apply system tray icon "${options.systemTrayIcon}": ${err instanceof Error ? err.message : String(err)}`);
- logger.warn(`✼ Default system tray icon will remain unchanged.`);
- }
- }
+ const defaultTrayIconPath = platform === 'darwin' ? 'png/icon_512.png' : tauriConf.bundle.icon[0];
+ const trayIconPath = await resolveSystemTrayIconPath(options.systemTrayIcon, defaultTrayIconPath, safeAppName);
tauriConf.pake.system_tray_path = trayIconPath;
delete tauriConf.app.trayIcon;
}
diff --git a/tests/unit/system-tray-icon.test.ts b/tests/unit/system-tray-icon.test.ts
new file mode 100644
index 00000000..aa8710b7
--- /dev/null
+++ b/tests/unit/system-tray-icon.test.ts
@@ -0,0 +1,114 @@
+import os from 'os';
+import path from 'path';
+import fsExtra from 'fs-extra';
+import { afterEach, describe, expect, it, vi } from 'vitest';
+
+import { resolveSystemTrayIconPath } from '../../bin/helpers/merge';
+import logger from '../../bin/options/logger';
+
+const tempDirs: string[] = [];
+
+async function makeTempDir() {
+ const tempDir = await fsExtra.mkdtemp(
+ path.join(os.tmpdir(), 'pake-system-tray-icon-'),
+ );
+ tempDirs.push(tempDir);
+ return tempDir;
+}
+
+describe('resolveSystemTrayIconPath', () => {
+ afterEach(async () => {
+ vi.restoreAllMocks();
+ await Promise.all(tempDirs.splice(0).map((dir) => fsExtra.remove(dir)));
+ });
+
+ it('keeps the default tray icon when no custom icon is configured', async () => {
+ const warnSpy = vi.spyOn(logger, 'warn').mockImplementation(() => {});
+
+ await expect(
+ resolveSystemTrayIconPath('', 'png/icon_512.png', 'TestApp'),
+ ).resolves.toBe('png/icon_512.png');
+ expect(warnSpy).not.toHaveBeenCalled();
+ });
+
+ it('keeps the default tray icon for unsupported file extensions', async () => {
+ const tempDir = await makeTempDir();
+ const sourceIcon = path.join(tempDir, 'tray.svg');
+ const warnSpy = vi.spyOn(logger, 'warn').mockImplementation(() => {});
+ await fsExtra.outputFile(sourceIcon, '<svg />');
+
+ await expect(
+ resolveSystemTrayIconPath(
+ sourceIcon,
+ 'png/icon_512.png',
+ 'TestApp',
+ tempDir,
+ ),
+ ).resolves.toBe('png/icon_512.png');
+ expect(warnSpy).toHaveBeenCalledWith(
+ expect.stringContaining('must be .ico or .png'),
+ );
+ await expect(
+ fsExtra.pathExists(path.join(tempDir, 'TestApp.svg')),
+ ).resolves.toBe(false);
+ });
+
+ it('keeps the default tray icon when the custom icon is missing', async () => {
+ const tempDir = await makeTempDir();
+ const warnSpy = vi.spyOn(logger, 'warn').mockImplementation(() => {});
+
+ await expect(
+ resolveSystemTrayIconPath(
+ path.join(tempDir, 'missing.png'),
+ 'png/icon_512.png',
+ 'TestApp',
+ tempDir,
+ ),
+ ).resolves.toBe('png/icon_512.png');
+ expect(warnSpy).toHaveBeenCalledWith(
+ expect.stringContaining('was not found'),
+ );
+ });
+
+ it('keeps the default tray icon when copying the custom icon fails', async () => {
+ const tempDir = await makeTempDir();
+ const sourceIcon = path.join(tempDir, 'tray.png');
+ const blockedOutputPath = path.join(tempDir, 'not-a-directory');
+ const warnSpy = vi.spyOn(logger, 'warn').mockImplementation(() => {});
+
+ await fsExtra.outputFile(sourceIcon, 'custom-icon');
+ await fsExtra.outputFile(blockedOutputPath, 'blocks-copy-target');
+
+ await expect(
+ resolveSystemTrayIconPath(
+ sourceIcon,
+ 'png/icon_512.png',
+ 'TestApp',
+ blockedOutputPath,
+ ),
+ ).resolves.toBe('png/icon_512.png');
+ expect(warnSpy).toHaveBeenCalledWith(
+ expect.stringContaining('Failed to apply system tray icon'),
+ );
+ });
+
+ it('returns the custom tray icon path only after copying succeeds', async () => {
+ const tempDir = await makeTempDir();
+ const outputDir = path.join(tempDir, 'png');
+ const sourceIcon = path.join(tempDir, 'tray.png');
+
+ await fsExtra.outputFile(sourceIcon, 'custom-icon');
+
+ await expect(
+ resolveSystemTrayIconPath(
+ sourceIcon,
+ 'png/icon_512.png',
+ 'TestApp',
+ outputDir,
+ ),
+ ).resolves.toBe('png/TestApp.png');
+ await expect(
+ fsExtra.readFile(path.join(outputDir, 'TestApp.png'), 'utf8'),
+ ).resolves.toBe('custom-icon');
+ });
+});