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.

refactor: dedupe linux target handling and tighten cli number validation

Tw93fdbc54fd3bb0
diff --git a/bin/builders/LinuxBuilder.ts b/bin/builders/LinuxBuilder.ts
index 3d1e3fc8..09532150 100644
--- a/bin/builders/LinuxBuilder.ts
+++ b/bin/builders/LinuxBuilder.ts
@@ -5,6 +5,7 @@ import { PakeAppOptions } from '@/types';
import tauriConfig from '@/helpers/tauriConfig';
import { shellExec } from '@/utils/shell';
import { generateLinuxPackageName } from '@/utils/name';
+import { LINUX_TARGET_TYPES, filterLinuxTargets } from '@/utils/targets';
import logger from '@/options/logger';
export default class LinuxBuilder extends BaseBuilder {
@@ -55,28 +56,24 @@ export default class LinuxBuilder extends BaseBuilder {
return `${name}-${version}-1.${arch}`;
}
- if (this.currentBuildType === 'zst') {
- return `${name}-${version}-1-${arch}.pkg.tar`;
- }
-
return `${name}_${version}_${arch}`;
}
async build(url: string) {
- const targetTypes = ['deb', 'appimage', 'rpm', 'zst'];
- const requestedTargets = this.options.targets
- .split(',')
- .map((t: string) => t.trim());
-
- for (const target of targetTypes) {
- if (requestedTargets.includes(target)) {
- this.currentBuildType = target;
- if (target === 'zst') {
- await this.buildAndCopy(url, 'deb', false);
- await this.createArchPackageFromDeb();
- } else {
- await this.buildAndCopy(url, target);
- }
+ const targets = filterLinuxTargets(this.options.targets);
+ if (targets.length === 0) {
+ throw new Error(
+ `No valid Linux target in "${this.options.targets}". Valid targets: ${LINUX_TARGET_TYPES.join(', ')}.`,
+ );
+ }
+
+ for (const target of targets) {
+ this.currentBuildType = target;
+ if (target === 'zst') {
+ await this.buildAndCopy(url, 'deb', false);
+ await this.createArchPackageFromDeb();
+ } else {
+ await this.buildAndCopy(url, target);
}
}
}
@@ -127,6 +124,8 @@ export default class LinuxBuilder extends BaseBuilder {
await shellExec(
`tar -xf "${path.join(controlDir, dataArchive)}" -C "${dataDir}"`,
);
+ // Drop the desktop entry auto-generated by the Tauri deb bundler;
+ // the payload already ships Pake's own com.pake.<name>.desktop.
await fsExtra.remove(
path.join(
dataDir,
@@ -256,9 +255,6 @@ post_remove() {
if (target === 'appimage') {
return 'AppImage';
}
- if (target === 'zst') {
- return 'zst';
- }
return super.getFileType(target);
}
diff --git a/bin/helpers/merge.ts b/bin/helpers/merge.ts
index e250a9c0..70c2c6b8 100644
--- a/bin/helpers/merge.ts
+++ b/bin/helpers/merge.ts
@@ -17,6 +17,7 @@ import {
WindowConfig,
} from '@/types';
import { tauriConfigDirectory, npmDirectory } from '@/utils/dir';
+import { LINUX_TARGET_TYPES } from '@/utils/targets';
/**
* Pure transform from CLI options to the window-config slice that gets
@@ -188,20 +189,15 @@ Terminal=false
};
const validTargets = [
- 'deb',
- 'appimage',
- 'rpm',
- 'zst',
- 'deb-arm64',
- 'appimage-arm64',
- 'rpm-arm64',
- 'zst-arm64',
+ ...LINUX_TARGET_TYPES,
+ ...LINUX_TARGET_TYPES.map((target) => `${target}-arm64`),
];
const baseTarget = options.targets.includes('-arm64')
? options.targets.replace('-arm64', '')
: options.targets;
if (validTargets.includes(options.targets)) {
+ // zst is repacked from the deb payload, so Tauri itself bundles a deb.
tauriConf.bundle.targets = [baseTarget === 'zst' ? 'deb' : baseTarget];
} else {
logger.warn(
diff --git a/bin/utils/targets.ts b/bin/utils/targets.ts
new file mode 100644
index 00000000..b3a9b61a
--- /dev/null
+++ b/bin/utils/targets.ts
@@ -0,0 +1,8 @@
+export const LINUX_TARGET_TYPES = ['deb', 'appimage', 'rpm', 'zst'];
+
+// Returns the valid Linux build targets from a comma-separated targets
+// string, preserving LINUX_TARGET_TYPES order. Unknown entries are dropped.
+export function filterLinuxTargets(targets: string): string[] {
+ const requested = targets.split(',').map((target) => target.trim());
+ return LINUX_TARGET_TYPES.filter((target) => requested.includes(target));
+}
diff --git a/bin/utils/validate.ts b/bin/utils/validate.ts
index a5d073fb..b85153c1 100644
--- a/bin/utils/validate.ts
+++ b/bin/utils/validate.ts
@@ -7,6 +7,9 @@ export function validateNumberInput(value: string) {
if (!Number.isFinite(parsedValue)) {
throw new InvalidArgumentError('Not a number.');
}
+ if (parsedValue < 0) {
+ throw new InvalidArgumentError('Must not be negative.');
+ }
return parsedValue;
}
diff --git a/dist/cli.js b/dist/cli.js
index b7110f57..04b2a0aa 100644
--- a/dist/cli.js
+++ b/dist/cli.js
@@ -58,6 +58,7 @@ var scripts = {
analyze: "cd src-tauri && cargo bloat --release --crates",
tauri: "tauri",
cli: "cross-env NODE_ENV=development rollup -c -w",
+ "cli:dev": "cross-env NODE_ENV=development rollup -c -w",
"cli:build": "cross-env NODE_ENV=production rollup -c",
test: "pnpm run cli:build && cross-env PAKE_CREATE_APP=1 node tests/index.js",
format: "prettier --write . --ignore-unknown && find tests -name '*.js' -exec sed -i '' 's/[[:space:]]*$//' {} \\; && cd src-tauri && cargo fmt --verbose",
@@ -409,6 +410,14 @@ function generateIdentifierSafeName(name) {
return cleaned;
}
+const LINUX_TARGET_TYPES = ['deb', 'appimage', 'rpm', 'zst'];
+// Returns the valid Linux build targets from a comma-separated targets
+// string, preserving LINUX_TARGET_TYPES order. Unknown entries are dropped.
+function filterLinuxTargets(targets) {
+ const requested = targets.split(',').map((target) => target.trim());
+ return LINUX_TARGET_TYPES.filter((target) => requested.includes(target));
+}
+
/**
* Pure transform from CLI options to the window-config slice that gets
* merged into pake.json. Exposed for snapshot testing so option drift
@@ -534,19 +543,14 @@ Terminal=false
[desktopInstallPath]: `assets/${desktopFileName}`,
};
const validTargets = [
- 'deb',
- 'appimage',
- 'rpm',
- 'zst',
- 'deb-arm64',
- 'appimage-arm64',
- 'rpm-arm64',
- 'zst-arm64',
+ ...LINUX_TARGET_TYPES,
+ ...LINUX_TARGET_TYPES.map((target) => `${target}-arm64`),
];
const baseTarget = options.targets.includes('-arm64')
? options.targets.replace('-arm64', '')
: options.targets;
if (validTargets.includes(options.targets)) {
+ // zst is repacked from the deb payload, so Tauri itself bundles a deb.
tauriConf.bundle.targets = [baseTarget === 'zst' ? 'deb' : baseTarget];
}
else {
@@ -1395,26 +1399,21 @@ class LinuxBuilder extends BaseBuilder {
if (this.currentBuildType === 'rpm') {
return `${name}-${version}-1.${arch}`;
}
- if (this.currentBuildType === 'zst') {
- return `${name}-${version}-1-${arch}.pkg.tar`;
- }
return `${name}_${version}_${arch}`;
}
async build(url) {
- const targetTypes = ['deb', 'appimage', 'rpm', 'zst'];
- const requestedTargets = this.options.targets
- .split(',')
- .map((t) => t.trim());
- for (const target of targetTypes) {
- if (requestedTargets.includes(target)) {
- this.currentBuildType = target;
- if (target === 'zst') {
- await this.buildAndCopy(url, 'deb', false);
- await this.createArchPackageFromDeb();
- }
- else {
- await this.buildAndCopy(url, target);
- }
+ const targets = filterLinuxTargets(this.options.targets);
+ if (targets.length === 0) {
+ throw new Error(`No valid Linux target in "${this.options.targets}". Valid targets: ${LINUX_TARGET_TYPES.join(', ')}.`);
+ }
+ for (const target of targets) {
+ this.currentBuildType = target;
+ if (target === 'zst') {
+ await this.buildAndCopy(url, 'deb', false);
+ await this.createArchPackageFromDeb();
+ }
+ else {
+ await this.buildAndCopy(url, target);
}
}
}
@@ -1453,6 +1452,8 @@ class LinuxBuilder extends BaseBuilder {
throw new Error(`Could not find data.tar payload in ${debPath}`);
}
await shellExec(`tar -xf "${path.join(controlDir, dataArchive)}" -C "${dataDir}"`);
+ // Drop the desktop entry auto-generated by the Tauri deb bundler;
+ // the payload already ships Pake's own com.pake.<name>.desktop.
await fsExtra.remove(path.join(dataDir, 'usr', 'share', 'applications', `${packageName}.desktop`));
const installedSize = await this.getDirectorySize(dataDir);
const pkgInfo = `pkgname = ${packageName}
@@ -1551,9 +1552,6 @@ post_remove() {
if (target === 'appimage') {
return 'AppImage';
}
- if (target === 'zst') {
- return 'zst';
- }
return super.getFileType(target);
}
hasArchSpecificTarget() {
@@ -2545,6 +2543,9 @@ function validateNumberInput(value) {
if (!Number.isFinite(parsedValue)) {
throw new InvalidArgumentError('Not a number.');
}
+ if (parsedValue < 0) {
+ throw new InvalidArgumentError('Must not be negative.');
+ }
return parsedValue;
}
function validateUrlInput(url) {
diff --git a/tests/unit/builders.test.ts b/tests/unit/builders.test.ts
index c532e9ed..7747f8a6 100644
--- a/tests/unit/builders.test.ts
+++ b/tests/unit/builders.test.ts
@@ -1,141 +1,60 @@
import { describe, it, expect } from 'vitest';
-
-/**
- * Tests for multi-target build parsing logic
- * These tests verify the core logic used in LinuxBuilder without needing to instantiate the class
- */
-describe('Multi-target build parsing', () => {
- /**
- * Simulates the logic from LinuxBuilder.build()
- */
- function parseAndFilterTargets(targetsString: string): string[] {
- const validTargets = ['deb', 'appimage', 'rpm', 'zst'];
- const requestedTargets = targetsString
- .split(',')
- .map((t: string) => t.trim());
-
- return validTargets.filter((target) => requestedTargets.includes(target));
- }
-
- describe('Target parsing', () => {
- it('should parse single target', () => {
- const result = parseAndFilterTargets('deb');
-
- expect(result).toEqual(['deb']);
- expect(result).toHaveLength(1);
- });
-
- it('should parse comma-separated targets', () => {
- const result = parseAndFilterTargets('deb,appimage');
-
- expect(result).toEqual(['deb', 'appimage']);
- expect(result).toHaveLength(2);
- });
-
- it('should handle targets with spaces', () => {
- const result = parseAndFilterTargets('deb, appimage, rpm, zst');
-
- expect(result).toEqual(['deb', 'appimage', 'rpm', 'zst']);
- expect(result).toHaveLength(4);
- });
-
- it('should filter out invalid targets', () => {
- const result = parseAndFilterTargets('deb,invalid,appimage');
-
- expect(result).toEqual(['deb', 'appimage']);
- expect(result).not.toContain('invalid');
- expect(result).toHaveLength(2);
- });
-
- it('should handle all valid targets', () => {
- const result = parseAndFilterTargets('deb,appimage,rpm,zst');
-
- expect(result).toEqual(['deb', 'appimage', 'rpm', 'zst']);
- expect(result).toHaveLength(4);
- });
-
- it('should return empty array for all invalid targets', () => {
- const result = parseAndFilterTargets('invalid1,invalid2');
-
- expect(result).toEqual([]);
- expect(result).toHaveLength(0);
- });
-
- it('should handle excessive whitespace', () => {
- const result = parseAndFilterTargets(
- ' deb , appimage , rpm , zst ',
- );
-
- expect(result).toEqual(['deb', 'appimage', 'rpm', 'zst']);
- expect(result).toHaveLength(4);
- });
-
- it('should be case-sensitive', () => {
- const result = parseAndFilterTargets('DEB,APPIMAGE');
-
- // Should not match uppercase
- expect(result).toEqual([]);
- });
-
- it('should handle single target with comma', () => {
- const result = parseAndFilterTargets('deb,');
-
- expect(result).toEqual(['deb']);
- expect(result).toHaveLength(1);
- });
+import {
+ LINUX_TARGET_TYPES,
+ filterLinuxTargets,
+} from '../../bin/utils/targets.js';
+
+describe('Linux target filtering', () => {
+ it('parses a single target', () => {
+ expect(filterLinuxTargets('deb')).toEqual(['deb']);
});
- describe('Target validation', () => {
- it('should validate against Linux target types', () => {
- const validTargets = ['deb', 'appimage', 'rpm', 'zst'];
-
- expect(validTargets).toContain('deb');
- expect(validTargets).toContain('appimage');
- expect(validTargets).toContain('rpm');
- expect(validTargets).toContain('zst');
- expect(validTargets).not.toContain('msi');
- expect(validTargets).not.toContain('dmg');
- });
-
- it('should check if target is valid', () => {
- const validTargets = ['deb', 'appimage', 'rpm', 'zst'];
- const testTargets = ['deb', 'invalid', 'appimage', 'zst', 'msi'];
-
- const valid = testTargets.filter((t) => validTargets.includes(t));
- const invalid = testTargets.filter((t) => !validTargets.includes(t));
-
- expect(valid).toEqual(['deb', 'appimage', 'zst']);
- expect(invalid).toEqual(['invalid', 'msi']);
- });
+ it('parses comma-separated targets', () => {
+ expect(filterLinuxTargets('deb,appimage')).toEqual(['deb', 'appimage']);
});
- describe('Architecture suffix handling', () => {
- it('should extract format from arm64 target', () => {
- const target = 'deb-arm64';
- const format = target.replace('-arm64', '');
+ it('handles targets with spaces', () => {
+ expect(filterLinuxTargets('deb, appimage, rpm, zst')).toEqual([
+ 'deb',
+ 'appimage',
+ 'rpm',
+ 'zst',
+ ]);
+ });
- expect(format).toBe('deb');
- });
+ it('filters out invalid targets', () => {
+ expect(filterLinuxTargets('deb,invalid,appimage')).toEqual([
+ 'deb',
+ 'appimage',
+ ]);
+ });
- it('should keep format without suffix', () => {
- const target = 'deb';
- const format = target.replace('-arm64', '');
+ it('returns empty array when no target is valid', () => {
+ expect(filterLinuxTargets('invalid1,invalid2')).toEqual([]);
+ });
- expect(format).toBe('deb');
- });
+ it('handles excessive whitespace', () => {
+ expect(filterLinuxTargets(' deb , appimage , rpm , zst ')).toEqual([
+ 'deb',
+ 'appimage',
+ 'rpm',
+ 'zst',
+ ]);
+ });
- it('should handle appimage-arm64', () => {
- const target = 'appimage-arm64';
- const format = target.replace('-arm64', '');
+ it('is case-sensitive', () => {
+ expect(filterLinuxTargets('DEB,APPIMAGE')).toEqual([]);
+ });
- expect(format).toBe('appimage');
- });
+ it('ignores trailing commas', () => {
+ expect(filterLinuxTargets('deb,')).toEqual(['deb']);
+ });
- it('should handle zst-arm64', () => {
- const target = 'zst-arm64';
- const format = target.replace('-arm64', '');
+ it('preserves canonical order regardless of input order', () => {
+ expect(filterLinuxTargets('zst,deb')).toEqual(['deb', 'zst']);
+ });
- expect(format).toBe('zst');
- });
+ it('covers exactly the supported Linux formats', () => {
+ expect(LINUX_TARGET_TYPES).toEqual(['deb', 'appimage', 'rpm', 'zst']);
});
});
diff --git a/tests/unit/cli-options.test.ts b/tests/unit/cli-options.test.ts
index b5fab702..dc79b4b3 100644
--- a/tests/unit/cli-options.test.ts
+++ b/tests/unit/cli-options.test.ts
@@ -63,4 +63,9 @@ describe('CLI options', () => {
expect(() => validateNumberInput('-Infinity')).toThrow('Not a number.');
expect(validateNumberInput('1200')).toBe(1200);
});
+
+ it('rejects negative numeric option values', () => {
+ expect(() => validateNumberInput('-100')).toThrow('Must not be negative.');
+ expect(validateNumberInput('0')).toBe(0);
+ });
});