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: remove duplicate sharp runtime

Tw9371b1cd83fb05
diff --git a/bin/options/icon.ts b/bin/options/icon.ts
index 1a8e9ffa..c313bf3d 100644
--- a/bin/options/icon.ts
+++ b/bin/options/icon.ts
@@ -3,6 +3,7 @@ import fsExtra from 'fs-extra';
import chalk from 'chalk';
import { dir } from 'tmp-promise';
import { fileTypeFromBuffer } from 'file-type';
+import { execa } from 'execa';
import logger from './logger';
import { getSpinner } from '@/utils/info';
@@ -55,9 +56,22 @@ const ICON_CONFIG = {
const PLATFORM_CONFIG: Record<'win' | 'linux' | 'macos', PlatformIconConfig> = {
win: { format: '.ico', sizes: [...WIN_STANDARD_ICO_SIZES] },
linux: { format: '.png', size: 512 },
- macos: { format: '.icns', sizes: [16, 32, 64, 128, 256, 512, 1024] },
+ macos: { format: '.icns' },
};
+const MACOS_ICONSET_FILES = [
+ ['icon_16x16.png', 16],
+ ['icon_32x32.png', 32],
+ ['icon_128x128.png', 128],
+ ['[email protected]', 256],
+ ['icon_256x256.png', 256],
+ ['[email protected]', 512],
+ ['icon_512x512.png', 512],
+ ['[email protected]', 1024],
+] as const;
+
const API_KEYS = {
logoDev: ['pk_JLLMUKGZRpaG5YclhXaTkg', 'pk_Ph745P8mQSeYFfW2Wk039A'],
brandfetch: ['1idqvJC0CeFSeyp3Yf7', '1idej-yhU_ThggIHFyG'],
@@ -209,6 +223,44 @@ async function applyMacOSMask(inputPath: string): Promise<string> {
}
}
+export async function generateMacOSIcns(
+ inputPath: string,
+ outputDir: string,
+ iconName: string,
+): Promise<string> {
+ const sharp = await loadSharp();
+ const iconsetPath = path.join(outputDir, `${iconName}.iconset`);
+ const outputPath = path.join(
+ outputDir,
+ `${iconName}${PLATFORM_CONFIG.macos.format}`,
+ );
+ await fsExtra.ensureDir(iconsetPath);
+
+ const source = sharp(inputPath);
+ await Promise.all(
+ MACOS_ICONSET_FILES.map(async ([fileName, size]) => {
+ await source
+ .clone()
+ .resize(size, size, {
+ fit: 'contain',
+ background: ICON_CONFIG.transparentBackground,
+ })
+ .ensureAlpha()
+ .png()
+ .toFile(path.join(iconsetPath, fileName));
+ }),
+ );
+
+ await execa('/usr/bin/iconutil', [
+ '-c',
+ 'icns',
+ iconsetPath,
+ '-o',
+ outputPath,
+ ]);
+ return outputPath;
+}
+
/**
* Converts icon to platform-specific format
*/
@@ -274,14 +326,10 @@ async function convertIconFormat(
// macOS
const macIconPath = await applyMacOSMask(processedInputPath);
- const { default: icongen } = await import('icon-gen');
- await icongen(macIconPath, platformOutputDir, {
- report: false,
- icns: { name: iconName, sizes: PLATFORM_CONFIG.macos.sizes },
- });
- const outputPath = path.join(
+ const outputPath = await generateMacOSIcns(
+ macIconPath,
platformOutputDir,
- `${iconName}${PLATFORM_CONFIG.macos.format}`,
+ iconName,
);
return (await fsExtra.pathExists(outputPath)) ? outputPath : null;
} catch (error) {
diff --git a/dist/cli.js b/dist/cli.js
index 9fb4b673..73a14b19 100644
--- a/dist/cli.js
+++ b/dist/cli.js
@@ -21,7 +21,7 @@ var name = "pake-cli";
var version = "3.15.6";
var description = "🤱🏻 Turn any webpage into a desktop app with one command. 🤱🏻 一键打包网页生成轻量桌面应用。";
var engines = {
- node: ">=20.0.0"
+ node: ">=20.9.0"
};
var packageManager = "[email protected]";
var bin = {
@@ -78,7 +78,6 @@ var dependencies = {
execa: "^9.6.1",
"file-type": "^21.3.4",
"fs-extra": "^11.3.3",
- "icon-gen": "^5.0.0",
loglevel: "^1.9.2",
ora: "^9.3.0",
prompts: "^2.4.2",
@@ -2360,8 +2359,20 @@ const ICON_CONFIG = {
const PLATFORM_CONFIG = {
win: { format: '.ico', sizes: [...WIN_STANDARD_ICO_SIZES] },
linux: { format: '.png', size: 512 },
- macos: { format: '.icns', sizes: [16, 32, 64, 128, 256, 512, 1024] },
+ macos: { format: '.icns' },
};
+const MACOS_ICONSET_FILES = [
+ ['icon_16x16.png', 16],
+ ['icon_32x32.png', 32],
+ ['icon_128x128.png', 128],
+ ['[email protected]', 256],
+ ['icon_256x256.png', 256],
+ ['[email protected]', 512],
+ ['icon_512x512.png', 512],
+ ['[email protected]', 1024],
+];
const API_KEYS = {
logoDev: ['pk_JLLMUKGZRpaG5YclhXaTkg', 'pk_Ph745P8mQSeYFfW2Wk039A'],
brandfetch: ['1idqvJC0CeFSeyp3Yf7', '1idej-yhU_ThggIHFyG'],
@@ -2482,6 +2493,32 @@ async function applyMacOSMask(inputPath) {
return inputPath;
}
}
+async function generateMacOSIcns(inputPath, outputDir, iconName) {
+ const sharp = await loadSharp();
+ const iconsetPath = path.join(outputDir, `${iconName}.iconset`);
+ const outputPath = path.join(outputDir, `${iconName}${PLATFORM_CONFIG.macos.format}`);
+ await fsExtra.ensureDir(iconsetPath);
+ const source = sharp(inputPath);
+ await Promise.all(MACOS_ICONSET_FILES.map(async ([fileName, size]) => {
+ await source
+ .clone()
+ .resize(size, size, {
+ fit: 'contain',
+ background: ICON_CONFIG.transparentBackground,
+ })
+ .ensureAlpha()
+ .png()
+ .toFile(path.join(iconsetPath, fileName));
+ }));
+ await execa('/usr/bin/iconutil', [
+ '-c',
+ 'icns',
+ iconsetPath,
+ '-o',
+ outputPath,
+ ]);
+ return outputPath;
+}
/**
* Converts icon to platform-specific format
*/
@@ -2530,12 +2567,7 @@ async function convertIconFormat(inputPath, appName) {
}
// macOS
const macIconPath = await applyMacOSMask(processedInputPath);
- const { default: icongen } = await import('icon-gen');
- await icongen(macIconPath, platformOutputDir, {
- report: false,
- icns: { name: iconName, sizes: PLATFORM_CONFIG.macos.sizes },
- });
- const outputPath = path.join(platformOutputDir, `${iconName}${PLATFORM_CONFIG.macos.format}`);
+ const outputPath = await generateMacOSIcns(macIconPath, platformOutputDir, iconName);
return (await fsExtra.pathExists(outputPath)) ? outputPath : null;
}
catch (error) {
diff --git a/package.json b/package.json
index 42a87fc4..78594e43 100644
--- a/package.json
+++ b/package.json
@@ -3,7 +3,7 @@
"version": "3.15.6",
"description": "🤱🏻 Turn any webpage into a desktop app with one command. 🤱🏻 一键打包网页生成轻量桌面应用。",
"engines": {
- "node": ">=20.0.0"
+ "node": ">=20.9.0"
},
"packageManager": "[email protected]",
"bin": {
@@ -60,7 +60,6 @@
"execa": "^9.6.1",
"file-type": "^21.3.4",
"fs-extra": "^11.3.3",
- "icon-gen": "^5.0.0",
"loglevel": "^1.9.2",
"ora": "^9.3.0",
"prompts": "^2.4.2",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 4ee65cd9..424722c3 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -34,9 +34,6 @@ importers:
fs-extra:
specifier: ^11.3.3
version: 11.3.3
- icon-gen:
- specifier: ^5.0.0
- version: 5.0.0(@types/[email protected])
loglevel:
specifier: ^1.9.2
version: 1.9.2
@@ -858,10 +855,6 @@ packages:
resolution: {integrity: sha512-bXfOC4QcT1tKXGorxL3wbJm6XJPDqEnij2gQ2m7ESQuE+/z9YFIWnl/5RpTiKWbMq3EVKR4fRLJGn6DVfu0mpw==}
engines: {node: '>=18.20'}
- resolution: {integrity: sha512-Vw8qHK3bZM9y/P10u3Vib8o/DdkvA2OtPtZvD871QKjy74Wj1WSKFILMPRPSdUSx5RFK1arlJzEtA4PkFgnbuA==}
- engines: {node: '>=18'}
-
resolution: {integrity: sha512-H+y0Jo/T1RZ9qPP4Eh1pkcQcLRglraJaSLoyOtHxu6AapkjWVCy2Sit1QQ4x3Dng8qDlSsZEet7g5Pq06MvTgw==}
engines: {node: '>=20'}
@@ -1007,11 +1000,6 @@ packages:
resolution: {integrity: sha512-eKCa6bwnJhvxj14kZk5NCPc6Hb6BdsU9DZcOnmQKSnO1VKrfV0zCvtttPZUsBvjmNDn8rpcJfpwSYnHBjc95MQ==}
engines: {node: '>=18.18.0'}
- resolution: {integrity: sha512-9C1FWnx7gP+HmvcaQziDtmqv2sHChB1wE1yuI59aCThvOpOAse4REsZr6vRwZ7UMBSKHfR9GHQqUBzCRBurfpQ==}
- engines: {node: '>= 20'}
- hasBin: true
-
resolution: {integrity: sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==}
@@ -1179,10 +1167,6 @@ packages:
resolution: {integrity: sha512-HRDzbaKjC+AOWVXxAU/x54COGeIv9eb+6CkDSQoNTt4XyWoIJvuPsXizxu/Fr23EiekbtZwmh1IcIG/l/a10GQ==}
engines: {node: '>=8'}
- resolution: {integrity: sha512-LKWqWJRhstyYo9pGvgor/ivk2w94eSjE3RGVuzLGlr3NmD8bf7RcYGze1mNdEHRP6TRP6rMuDHk5t44hnTRyow==}
- engines: {node: '>=14.19.0'}
-
resolution: {integrity: sha512-3Ybi1tAuwAP9s0r1UQ2J4n5Y0G05bJkpUIO0/bI9MhwmD70S5aTWbXGBwxHrelT+XM1k6dM0pk+SwNkpTRN7Pg==}
engines: {node: ^10 || ^12 || >=14}
@@ -2086,8 +2070,6 @@ snapshots:
-
@@ -2253,14 +2235,6 @@ snapshots:
- dependencies:
- commander: 12.1.0
- pngjs: 7.0.0
- sharp: 0.35.3(@types/[email protected])
- transitivePeerDependencies:
- - '@types/node'
-
@@ -2394,8 +2368,6 @@ snapshots:
dependencies:
find-up: 4.1.0
-
dependencies:
nanoid: 3.3.11
diff --git a/tests/index.js b/tests/index.js
index cea133d3..09f08099 100644
--- a/tests/index.js
+++ b/tests/index.js
@@ -15,7 +15,7 @@ import config, { TIMEOUTS, TEST_URLS } from "./config.js";
const rejectImageDependenciesLoader = `data:text/javascript,${encodeURIComponent(`
export async function resolve(specifier, context, nextResolve) {
- if (specifier === "sharp" || specifier === "icon-gen") {
+ if (specifier === "sharp") {
throw new Error("Image dependency loaded during CLI startup: " + specifier);
}
return nextResolve(specifier, context);
@@ -349,8 +349,10 @@ class PakeTestRunner {
);
const essentialDeps = ["commander", "chalk", "fs-extra", "execa"];
- return essentialDeps.every(
- (dep) => packageJson.dependencies && packageJson.dependencies[dep],
+ return (
+ essentialDeps.every(
+ (dep) => packageJson.dependencies && packageJson.dependencies[dep],
+ ) && !packageJson.dependencies["icon-gen"]
);
} catch {
return false;
diff --git a/tests/unit/icon.test.ts b/tests/unit/icon.test.ts
index 3ee9df4f..e87dfc33 100644
--- a/tests/unit/icon.test.ts
+++ b/tests/unit/icon.test.ts
@@ -1,11 +1,20 @@
import fs from 'fs';
+import os from 'os';
import path from 'path';
import { afterEach, describe, expect, it, vi } from 'vitest';
+import sharp from 'sharp';
-import { downloadIcon } from '@/options/icon';
+const execaMock = vi.hoisted(() => vi.fn());
+
+vi.mock('execa', () => ({
+ execa: execaMock,
+}));
+
+import { downloadIcon, generateMacOSIcns } from '@/options/icon';
import { getIconSourcePriority } from '@/utils/icon-source';
const downloadedIconPaths: string[] = [];
+const generatedIconDirs: string[] = [];
afterEach(() => {
vi.unstubAllGlobals();
@@ -22,6 +31,66 @@ afterEach(() => {
fs.rmSync(tempDir, { recursive: true, force: true });
}
}
+
+ while (generatedIconDirs.length > 0) {
+ const tempDir = generatedIconDirs.pop();
+ if (tempDir && fs.existsSync(tempDir)) {
+ fs.rmSync(tempDir, { recursive: true, force: true });
+ }
+ }
+});
+
+describe('generateMacOSIcns', () => {
+ it('builds a complete iconset before calling the system converter', async () => {
+ const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'pake-icns-'));
+ generatedIconDirs.push(tempDir);
+ const sourcePath = path.join(tempDir, 'source.svg');
+ fs.writeFileSync(
+ sourcePath,
+ '<svg xmlns="http://www.w3.org/2000/svg" width="1024" height="1024"><rect width="1024" height="1024" fill="#123456"/></svg>',
+ );
+
+ const expectedSizes = new Map([
+ ['icon_16x16.png', 16],
+ ['icon_32x32.png', 32],
+ ['icon_128x128.png', 128],
+ ['[email protected]', 256],
+ ['icon_256x256.png', 256],
+ ['[email protected]', 512],
+ ['icon_512x512.png', 512],
+ ['[email protected]', 1024],
+ ]);
+
+ execaMock.mockImplementation(async (command: string, args: string[]) => {
+ expect(command).toBe('/usr/bin/iconutil');
+ expect(args.slice(0, 2)).toEqual(['-c', 'icns']);
+
+ const iconsetPath = args[2];
+ const outputPath = args[4];
+ expect(fs.readdirSync(iconsetPath).sort()).toEqual(
+ [...expectedSizes.keys()].sort(),
+ );
+
+ for (const [fileName, size] of expectedSizes) {
+ const metadata = await sharp(
+ path.join(iconsetPath, fileName),
+ ).metadata();
+ expect(metadata.width).toBe(size);
+ expect(metadata.height).toBe(size);
+ }
+
+ fs.writeFileSync(outputPath, 'test-icns');
+ return { exitCode: 0 };
+ });
+
+ const outputPath = await generateMacOSIcns(sourcePath, tempDir, 'example');
+
+ expect(outputPath).toBe(path.join(tempDir, 'example.icns'));
+ expect(fs.readFileSync(outputPath, 'utf8')).toBe('test-icns');
+ expect(execaMock).toHaveBeenCalledTimes(1);
+ });
});
describe('icon source priority', () => {