Turn any website into a lightweight desktop app with one command, on macOS, Windows and Linux.
Rust chatgptclaudeclidesktopdesktop-appelectron-alternativegeminilinuxmacosnativefierno-electronrusttauriweb-to-desktopwebsite-to-appwebviewwindowsyoutube
import fs from 'fs';
import path from 'path';
import { PakeError } from './error';
import { InvalidArgumentError } from 'commander';
import { normalizeUrl } from './url';
export function validateNumberInput(value: string) {
if (value.trim() === '') {
throw new InvalidArgumentError('Not a number.');
}
const parsedValue = Number(value);
if (!Number.isFinite(parsedValue)) {
throw new InvalidArgumentError('Not a number.');
}
if (parsedValue < 0) {
throw new InvalidArgumentError('Must not be negative.');
}
return parsedValue;
}
// Path-shaped input (./x, ../x, /x, ~/x, C:\x). A missing path must fail
// loudly: appending https:// to "./typo" would otherwise produce a valid URL
// like https://./typo and a silently broken app (worst case for agents).
const PATH_LIKE_PATTERN = /^(\.{1,2}[\\/]|[\\/]|~[\\/]|[a-zA-Z]:[\\/])/;
export function validateUrlInput(url: string) {
const isFile = fs.existsSync(url);
if (!isFile) {
if (PATH_LIKE_PATTERN.test(url)) {
throw new InvalidArgumentError(
`Local path "${url}" does not exist. Check the path, or pass a web URL instead.`,
);
}
try {
return normalizeUrl(url);
} catch (error) {
if (error instanceof Error) {
throw new InvalidArgumentError(error.message);
}
throw error;
}
}
return url;
}
export function validateDownloadDirInput(value: string): string {
if (value === '') return value;
const homeRelative = value.startsWith('~/') ? value.slice(2) : null;
const invalidHomePath =
homeRelative !== null &&
(path.isAbsolute(homeRelative) ||
(process.platform === 'win32' &&
/^(?:[a-zA-Z]:|[\\/])/.test(homeRelative)));
if (
invalidHomePath ||
value.includes('\0') ||
!(path.isAbsolute(value) || value === '~' || value.startsWith('~/')) ||
(process.platform === 'win32' &&
value !== '~' &&
!value.startsWith('~/') &&
!/^(?:[a-zA-Z]:[\\/]|[\\/]{2}[^\\/]+[\\/][^\\/]+)/.test(value))
) {
throw new PakeError('Invalid download directory.', {
code: 'INVALID_INPUT',
hint: 'Use an absolute path or a quoted ~/path; relative paths are not supported.',
});
}
return value;
}
// The injected link policy compiles this with `new RegExp` and, when that
// throws, only logs it and falls back to same-root-domain matching, so a typo
// would ship an app that silently ignores the option.
export function validateInternalUrlRegexInput(value: string): string {
if (value === '') return value;
try {
new RegExp(value);
} catch (error) {
const detail = error instanceof Error ? error.message : String(error);
throw new PakeError(`Invalid internal URL regex: ${detail}`, {
code: 'INVALID_INPUT',
hint: 'Pass a JavaScript regular expression to --internal-url-regex, or list domains with --safe-domain.',
});
}
return value;
}
const INJECT_EXTENSIONS = new Set(['.css', '.js']);
// Shared by --inject and the config "inject" field, so a bad file fails before
// anything is built. The flag has split its value on commas already; a config
// entry is one path and may hold a comma.
export function validateInjectInput(
inject: string[] | undefined,
cwd: string = process.cwd(),
): string[] {
if (!Array.isArray(inject)) return [];
const files = inject
.map((item) => item.trim())
.filter((item) => item.length > 0);
for (const file of files) {
if (!INJECT_EXTENSIONS.has(path.extname(file).toLowerCase())) {
throw new PakeError(
`Injected file "${file}" is not a .css or .js file.`,
{
code: 'INVALID_INPUT',
hint: 'Pass local CSS or JS files to --inject, separated by commas.',
},
);
}
const resolved = path.isAbsolute(file) ? file : path.join(cwd, file);
if (!fs.statSync(resolved, { throwIfNoEntry: false })?.isFile()) {
throw new PakeError(`Injected file not found: ${file}`, {
code: 'INVALID_INPUT',
hint: 'Relative --inject paths resolve against the current directory.',
});
}
}
return files;
}