Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apps/code/runtime-dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ export const requiredNativeModules = [
"better-sqlite3",
];

// file-icon (and its p-map dependency) is only used on macOS.
export const macOnlyNativeModules = ["file-icon", "p-map"];
// file-icon is only used on macOS.
export const macOnlyNativeModules = ["file-icon"];

// The subset that ships compiled .node binaries and must be unpacked from asar.
const asarUnpackModules = [
Expand Down
16 changes: 16 additions & 0 deletions apps/code/src/main/platform-adapters/electron-file-icon.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { describe, expect, it } from "vitest";
import { resolveMacFileIconBinary } from "./electron-file-icon";

describe("resolveMacFileIconBinary", () => {
it("resolves the packaged extractor outside the ASAR", () => {
expect(
resolveMacFileIconBinary(
"/Applications/PostHog Code.app/Contents/Resources/app.asar",
true,
"/unused/node_modules/file-icon/index.js",
),
).toBe(
"/Applications/PostHog Code.app/Contents/Resources/app.asar.unpacked/node_modules/file-icon/file-icon",
);
});
});
49 changes: 38 additions & 11 deletions apps/code/src/main/platform-adapters/electron-file-icon.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
import { execFile } from "node:child_process";
import path from "node:path";
import type { IFileIcon } from "@posthog/platform/file-icon";
import { app } from "electron";
import { injectable } from "inversify";

type FileIconModule = typeof import("file-icon");
const FILE_ICON_MAX_BUFFER_BYTES = 100 * 1024 * 1024;

export function resolveMacFileIconBinary(
appPath: string,
isPackaged: boolean,
modulePath: string,
): string {
const resolvedModulePath = isPackaged
? path.join(`${appPath}.unpacked`, "node_modules", "file-icon", "index.js")
: modulePath;
return path.join(path.dirname(resolvedModulePath), "file-icon");
}

@injectable()
export class ElectronFileIcon implements IFileIcon {
private fileIconModule: FileIconModule | undefined;

public async getAsDataUrl(filePath: string): Promise<string | null> {
try {
if (process.platform === "darwin") {
const mod = await this.loadFileIconModule();
const uint8Array = await mod.fileIconToBuffer(filePath, { size: 64 });
const base64 = Buffer.from(uint8Array).toString("base64");
const buffer = await this.getMacFileIcon(filePath);
const base64 = buffer.toString("base64");
return `data:image/png;base64,${base64}`;
}

Expand All @@ -25,10 +35,27 @@ export class ElectronFileIcon implements IFileIcon {
}
}

private async loadFileIconModule(): Promise<FileIconModule> {
if (!this.fileIconModule) {
this.fileIconModule = await import("file-icon");
}
return this.fileIconModule;
private getMacFileIcon(filePath: string): Promise<Buffer> {
const binaryPath = resolveMacFileIconBinary(
app.getAppPath(),
app.isPackaged,
require.resolve("file-icon"),
);
const input = JSON.stringify([{ appOrPID: filePath, size: 64 }]);

return new Promise((resolve, reject) => {
execFile(
binaryPath,
[input],
{ encoding: "buffer", maxBuffer: FILE_ICON_MAX_BUFFER_BYTES },
(error, stdout) => {
if (error) {
reject(error);
return;
}
resolve(Buffer.from(stdout));
},
);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,10 @@ export class ExternalAppsService {
type: "terminal",
darwin: { path: "/Applications/Ghostty.app" },
},
cmux: {
type: "terminal",
darwin: { path: "/Applications/cmux.app" },
},
kitty: {
type: "terminal",
darwin: { path: "/Applications/kitty.app" },
Expand Down Expand Up @@ -477,6 +481,7 @@ export class ExternalAppsService {
alacritty: "Alacritty",
kitty: "Kitty",
ghostty: "Ghostty",
cmux: "cmux",
hyper: "Hyper",
tabby: "Tabby",
rio: "Rio",
Expand Down
Loading