-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
174 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,80 @@ | ||
import { readdirSafe, readFileSafe } from "../main/utils"; | ||
import type { AppInfo } from "../reducers/app"; | ||
import type { AppReader } from "./utils"; | ||
import fs from "fs"; | ||
import ini from "ini"; | ||
import path from "path"; | ||
import { Result } from "ts-results"; | ||
|
||
const desktopFilesDir = "/usr/share/applications"; | ||
|
||
async function readAppInfo(desktopFile: string): Promise<AppInfo | undefined> { | ||
const content = await readFileSafe(desktopFile); | ||
const readAppInfo = (desktopFile: string) => | ||
Result.wrapAsync(async () => { | ||
const content = await fs.promises.readFile(desktopFile, { | ||
encoding: "utf-8", | ||
}); | ||
const entry = ini.parse(content)["Desktop Entry"] as | ||
| { | ||
Name?: string; | ||
Icon?: string; | ||
Exec?: string; | ||
} | ||
| undefined; | ||
|
||
const entry = ini.parse(content)["Desktop Entry"] as { | ||
Name?: string; | ||
Icon?: string; | ||
Exec?: string; | ||
}; | ||
if (!entry || !entry.Exec) return; | ||
if (!entry?.Exec) throw new Error("Exec not found"); | ||
|
||
let exePath = ""; | ||
if (entry.Exec.startsWith('"')) { | ||
exePath = entry.Exec.replace(/^"(.*)".*/, "$1"); | ||
} else { | ||
// Remove arg | ||
exePath = entry.Exec.split(/\s+/)[0] ?? ""; | ||
} | ||
|
||
if (!exePath.startsWith("/")) return; | ||
|
||
if (!fs.existsSync(path.join(exePath, "../resources/electron.asar"))) return; | ||
let exePath = ""; | ||
if (entry.Exec.startsWith('"')) { | ||
exePath = entry.Exec.replace(/^"(.*)".*/, "$1"); | ||
} else { | ||
// Remove arg | ||
exePath = entry.Exec.split(/\s+/)[0] ?? ""; | ||
} | ||
|
||
let icon = ""; | ||
if (entry.Icon) { | ||
try { | ||
const iconBuffer = await fs.promises.readFile( | ||
`/usr/share/icons/hicolor/1024x1024/apps/${entry.Icon}.png`, | ||
); | ||
icon = "data:image/png;base64," + iconBuffer.toString("base64"); | ||
} catch (err) { | ||
console.error(err); | ||
if (!exePath.startsWith("/")) { | ||
throw new Error("Exec path invalid"); | ||
} | ||
if (!fs.existsSync(path.join(exePath, "../resources/electron.asar"))) { | ||
throw new Error("resources/electron.asar not exists"); | ||
} | ||
} | ||
|
||
return { | ||
id: exePath, | ||
icon: icon, // TODO: Read icon | ||
name: entry.Name || path.basename(exePath), | ||
exePath: exePath, | ||
}; | ||
} | ||
let icon = ""; | ||
if (entry.Icon) { | ||
try { | ||
const iconBuffer = await fs.promises.readFile( | ||
`/usr/share/icons/hicolor/1024x1024/apps/${entry.Icon}.png`, | ||
); | ||
icon = "data:image/png;base64," + iconBuffer.toString("base64"); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
} | ||
|
||
export const adapter: AppReader = { | ||
async readAll() { | ||
const files = await readdirSafe(desktopFilesDir); | ||
const apps = await Promise.all( | ||
files.map((file) => { | ||
if (!file.endsWith(".desktop")) return; | ||
return readAppInfo(path.join(desktopFilesDir, file)); | ||
}), | ||
); | ||
return apps; | ||
}, | ||
async readByPath(p: string) { | ||
return { | ||
id: p, | ||
name: path.basename(p), | ||
icon: "", | ||
exePath: p, | ||
id: exePath, | ||
icon: icon, // TODO: Read icon | ||
name: entry.Name || path.basename(exePath), | ||
exePath: exePath, | ||
}; | ||
}, | ||
}); | ||
|
||
export const adapter: AppReader = { | ||
readAll: () => | ||
Result.wrapAsync(async () => { | ||
const files = await fs.promises.readdir(desktopFilesDir); | ||
const apps = await Promise.all( | ||
files | ||
.filter((f) => f.endsWith(".desktop")) | ||
.map((file) => readAppInfo(path.join(desktopFilesDir, file))), | ||
); | ||
return apps.flatMap((app) => (app.ok ? [app.unwrap()] : [])); | ||
}), | ||
readByPath: (p: string) => | ||
Result.wrapAsync(async () => { | ||
// TODO: | ||
return { | ||
id: p, | ||
name: path.basename(p), | ||
icon: "", | ||
exePath: p, | ||
}; | ||
}), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import type { AppInfo } from "../reducers/app"; | ||
import { Result } from "ts-results"; | ||
|
||
export interface AppReader { | ||
readAll(): Promise<(AppInfo | undefined)[]>; | ||
readByPath(p: string): Promise<AppInfo | undefined>; | ||
readAll(): Promise<Result<AppInfo[], Error>>; | ||
readByPath(p: string): Promise<Result<AppInfo, Error>>; | ||
} |
Oops, something went wrong.