Skip to content

Open-source, local-first AI agent for real work.

2.1 KB
Raw Blame
import { readFile } from "node:fs/promises";
import { join } from "node:path";
import { Data, NtExecutable, NtExecutableResource, Resource } from "resedit";

const defaultIconPath = join(import.meta.dirname, "..", "build", "icon.ico");
const expectedProductName = "Vetta";

function iconBytes(item) {
	return Buffer.from(item.isIcon() ? item.generate() : item.bin);
}

export async function verifyWindowsExecutableBranding(executablePath, iconPath = defaultIconPath) {
	const executable = NtExecutable.from(await readFile(executablePath), { ignoreCert: true });
	const { entries } = NtExecutableResource.from(executable);
	// electron-builder leaves a second, version-only string table in the launcher.
	const productNames = Resource.VersionInfo.fromEntries(entries)
		.flatMap((info) =>
			info.getAllLanguagesForStringValues().map((language) => info.getStringValues(language).ProductName),
		)
		.filter(Boolean);
	if (productNames.length === 0 || productNames.some((name) => name !== expectedProductName)) {
		throw new Error(
			`[windows-executable-branding] ${executablePath} has product name ${productNames.join(", ") || "(none)"}, expected ${expectedProductName}`,
		);
	}

	const expectedIcons = Data.IconFile.from(await readFile(iconPath)).icons.map(({ data }) => iconBytes(data));
	// Windows shows the first icon group of an executable.
	const [iconGroup] = Resource.IconGroupEntry.fromEntries(entries);
	const actualIcons = iconGroup ? iconGroup.getIconItemsFromEntries(entries).map(iconBytes) : [];
	if (
		actualIcons.length !== expectedIcons.length ||
		actualIcons.some((icon, index) => !icon.equals(expectedIcons[index]))
	) {
		throw new Error(`[windows-executable-branding] ${executablePath} does not use the Vetta icon`);
	}
}

// The launcher and the versioned application are both visible to users: shortcuts use
// the former, the taskbar and Task Manager use the latter.
export async function verifyWindowsLayoutBranding(layoutRoot, version) {
	await verifyWindowsExecutableBranding(join(layoutRoot, "Vetta.exe"));
	await verifyWindowsExecutableBranding(join(layoutRoot, "versions", version, "Vetta.exe"));
}