Skip to content

Commit

Permalink
style: run format
Browse files Browse the repository at this point in the history
  • Loading branch information
pd4d10 committed Jul 25, 2024
1 parent 444c059 commit 87b781a
Show file tree
Hide file tree
Showing 20 changed files with 94 additions and 154 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!doctype html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
Expand Down
39 changes: 17 additions & 22 deletions src/main/actions.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { appSlice, type AppInfo } from "../reducers/app";
import { sessionSlice, type PageInfo } from "../reducers/session";
import { importByPlatform } from "./platforms";
import type { State } from "./store";
import type { ThunkDispatch } from "@reduxjs/toolkit";
import { spawn } from "child_process";
import { dialog } from "electron";
import getPort from "get-port";
import { chunk } from "lodash-es";
import path from "node:path";
import { v4 } from "uuid";
import { type AppInfo, appSlice } from "../reducers/app";
import { type PageInfo, sessionSlice } from "../reducers/session";
import { importByPlatform } from "./platforms";
import type { State } from "./store";

type ThunkActionCreator<P1 = void, P2 = void> = (
p1: P1,
Expand All @@ -33,9 +33,7 @@ export const init: ThunkActionCreator = () => async (dispatch, getState) => {
const ports = sessions.flatMap((s) => [s.nodePort, s.windowPort]);

const responses = await Promise.allSettled<PageInfo[]>(
ports.map((port) =>
fetch(`http://127.0.0.1:${port}/json`).then((res) => res.json()),
),
ports.map((port) => fetch(`http://127.0.0.1:${port}/json`).then((res) => res.json())),
);
const pagess = chunk(
responses.map((p) => (p.status === "fulfilled" ? p.value : [])),
Expand Down Expand Up @@ -81,17 +79,15 @@ export const debug: ThunkActionCreator<AppInfo> = (app) => async (dispatch) => {
dispatch(sessionSlice.actions.removed(sessionId));
});

const handleStdout =
(isError = false) =>
(chunk: Buffer) => {
// TODO: stderr colors
dispatch(
sessionSlice.actions.logAppended({
sessionId,
content: chunk.toString(),
}),
);
};
const handleStdout = (isError = false) => (chunk: Buffer) => {
// TODO: stderr colors
dispatch(
sessionSlice.actions.logAppended({
sessionId,
content: chunk.toString(),
}),
);
};

if (sp.stdout) {
sp.stdout.on("data", handleStdout());
Expand All @@ -101,7 +97,6 @@ export const debug: ThunkActionCreator<AppInfo> = (app) => async (dispatch) => {
}
};

export const debugPath: ThunkActionCreator<string> =
(path) => async (dispatch) => {
// TODO:
};
export const debugPath: ThunkActionCreator<string> = (path) => async (dispatch) => {
// TODO:
};
21 changes: 6 additions & 15 deletions src/main/main.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
import { app, BrowserWindow, ipcMain, Menu, MenuItem, nativeImage, shell } from "electron";
import path from "path";
import type { AppInfo } from "../reducers/app";
import { debug, debugPath, init } from "./actions";
import { store } from "./store";
import { setUpdater, setReporter } from "./utils";
import {
app,
BrowserWindow,
Menu,
MenuItem,
shell,
nativeImage,
ipcMain,
} from "electron";
import path from "path";
import { setReporter, setUpdater } from "./utils";

// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require("electron-squirrel-startup")) {
Expand All @@ -26,10 +18,9 @@ const createWindow = () => {
height: 768,
titleBarStyle: "hidden",
trafficLightPosition: { x: 14, y: 14 },
icon:
process.platform === "linux"
? nativeImage.createFromDataURL(require("../../assets/icon.png"))
: undefined,
icon: process.platform === "linux"
? nativeImage.createFromDataURL(require("../../assets/icon.png"))
: undefined,
webPreferences: {
nodeIntegration: true,
contextIsolation: false, // for `require`
Expand Down
22 changes: 11 additions & 11 deletions src/main/platforms/linux.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { findExecPath } from "./linux/find-exec";
import { findIconPath } from "./linux/find-icon";
import type { AppReader } from "./utils";
import fs from "fs";
import ini from "ini";
import os from "os";
import path from "path";
import { Result } from "ts-results";
import { findExecPath } from "./linux/find-exec";
import { findIconPath } from "./linux/find-icon";
import type { AppReader } from "./utils";

const userAppsDir = path.join(os.homedir(), ".local/share/applications");
const sysAppsDir = "/usr/share/applications";
Expand All @@ -17,16 +17,16 @@ const readAppInfo = (desktopFile: string) =>
});
const entry = ini.parse(content)["Desktop Entry"] as
| {
Name?: string;
Icon?: string;
Exec?: string;
}
Name?: string;
Icon?: string;
Exec?: string;
}
| undefined;

if (!entry?.Exec) throw new Error("Exec not found");

let exePath = "";
if (entry.Exec.startsWith('"')) {
if (entry.Exec.startsWith("\"")) {
exePath = entry.Exec.replace(/^"(.*)".*/, "$1");
} else {
// Remove arg
Expand All @@ -44,9 +44,9 @@ const readAppInfo = (desktopFile: string) =>

if (
!(
fs.existsSync(path.join(exePath, "../resources/electron.asar")) ||
fs.existsSync(path.join(exePath, "../LICENSE.electron.txt")) ||
fs.existsSync(path.join(exePath, "../chrome-sandbox"))
fs.existsSync(path.join(exePath, "../resources/electron.asar"))
|| fs.existsSync(path.join(exePath, "../LICENSE.electron.txt"))
|| fs.existsSync(path.join(exePath, "../chrome-sandbox"))
)
) {
throw new Error("resources/electron.asar not exists");
Expand Down
5 changes: 1 addition & 4 deletions src/main/platforms/linux/find-exec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import fs from "fs";
import path from "path";

const pathDirs =
process.env.PATH?.split(path.delimiter)?.filter((dir) =>
fs.existsSync(dir),
) || [];
const pathDirs = process.env.PATH?.split(path.delimiter)?.filter((dir) => fs.existsSync(dir)) || [];

export function findExecPath(command: string) {
for (const pathDir of pathDirs) {
Expand Down
19 changes: 7 additions & 12 deletions src/main/platforms/linux/find-icon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,20 @@ const themeIconBases = (
process.env.XDG_DATA_DIRS?.split(path.delimiter) || DEFAULT_XDG_DATA_DIRS
)
// If dir is not named with 'icons', append 'icons'
.map((dir) =>
path.basename(dir) === "icons" ? dir : path.join(dir, "icons"),
);
.map((dir) => path.basename(dir) === "icons" ? dir : path.join(dir, "icons"));

const backwards_userIconBase = path.join(os.homedir(), ".icons");
// for backwards compatibility
if (
!themeIconBases.includes(backwards_userIconBase) &&
!themeIconBases.includes(backwards_userIconBase + "/")
!themeIconBases.includes(backwards_userIconBase)
&& !themeIconBases.includes(backwards_userIconBase + "/")
) {
themeIconBases.unshift(backwards_userIconBase);
}

const themeLeveledBase = [
/* 'xxx', */ // todo: get current theme
/* 'xxx', */
// todo: get current theme
"hicolor",
"Papirus",
"default",
Expand All @@ -44,9 +43,7 @@ function initThemeLeveledNames() {
if (!fs.existsSync(base)) return [];

const files = fs.readdirSync(base);
return files.filter((name) =>
fs.statSync(path.join(base, name)).isDirectory(),
);
return files.filter((name) => fs.statSync(path.join(base, name)).isDirectory());
}

const themeNames = themeIconBases.map(findThemeNames).flat();
Expand All @@ -65,9 +62,7 @@ const themeSortedDirs = new Map<string, SizeDir[]>(
themeLeveledNames.map((theme) => [
theme,
themeIconBases
.map((base) =>
iconDirSortBySize(findAllIconDirs(base, path.join(base, theme))),
)
.map((base) => iconDirSortBySize(findAllIconDirs(base, path.join(base, theme))))
.flat(),
]),
);
Expand Down
2 changes: 1 addition & 1 deletion src/main/platforms/macos.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { AppReader } from "./utils";
import fs from "fs";
import path from "path";
import plist from "simple-plist";
import { Result } from "ts-results";
import type { AppReader } from "./utils";

interface MacosAppInfo {
CFBundleIdentifier: string;
Expand Down
2 changes: 1 addition & 1 deletion src/main/platforms/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { AppInfo } from "../../reducers/app";
import { Result } from "ts-results";
import type { AppInfo } from "../../reducers/app";

export interface AppReader {
readAll(): Promise<Result<AppInfo[], Error>>;
Expand Down
40 changes: 16 additions & 24 deletions src/main/platforms/win.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import type { AppInfo } from "../../reducers/app";
import type { AppReader } from "./utils";
import fs from "fs";
import path from "path";
import {
enumerateKeys,
enumerateValues,
HKEY,
type RegistryValue,
type RegistryStringEntry,
type RegistryValue,
RegistryValueType,
enumerateKeys,
enumerateValues,
} from "registry-js";
import { Result } from "ts-results";
import type { AppInfo } from "../../reducers/app";
import type { AppReader } from "./utils";

async function getAppInfoByExePath(
exePath: string,
iconPath: string,
values: readonly RegistryValue[],
): Promise<AppInfo> {
const displayName = values.find(
(v): v is RegistryStringEntry =>
v && v.type === RegistryValueType.REG_SZ && v.name === "DisplayName",
(v): v is RegistryStringEntry => v && v.type === RegistryValueType.REG_SZ && v.name === "DisplayName",
);
let icon = "";
if (iconPath) {
Expand All @@ -41,8 +40,7 @@ const getAppInfoFromRegeditItemValues = async (

// Try to find executable path of Electron app
const displayIcon = values.find(
(v): v is RegistryStringEntry =>
v && v.type === RegistryValueType.REG_SZ && v.name === "DisplayIcon",
(v): v is RegistryStringEntry => v && v.type === RegistryValueType.REG_SZ && v.name === "DisplayIcon",
);

if (displayIcon) {
Expand All @@ -60,8 +58,7 @@ const getAppInfoFromRegeditItemValues = async (
let installDir = "";

const installLocation = values.find(
(v): v is RegistryStringEntry =>
v && v.type === RegistryValueType.REG_SZ && v.name === "InstallLocation",
(v): v is RegistryStringEntry => v && v.type === RegistryValueType.REG_SZ && v.name === "InstallLocation",
);

if (installLocation && installLocation.data) {
Expand Down Expand Up @@ -91,8 +88,8 @@ const getAppInfoFromRegeditItemValues = async (

function isElectronApp(installDir: string) {
return (
fs.existsSync(path.join(installDir, "resources")) &&
[
fs.existsSync(path.join(installDir, "resources"))
&& [
"electron.asar",
// https://github.com/pd4d10/debugtron/pull/26
"default_app.asar",
Expand All @@ -108,8 +105,8 @@ async function findExeFile(dir: string) {
const [exeFile] = files.filter((file) => {
const lc = file.toLowerCase();
return (
lc.endsWith(".exe") &&
!["uninstall", "update"].some((keyword) => lc.includes(keyword))
lc.endsWith(".exe")
&& !["uninstall", "update"].some((keyword) => lc.includes(keyword))
);
});
if (exeFile) return path.join(dir, exeFile);
Expand All @@ -120,9 +117,7 @@ export const adapter: AppReader = {
readAll: () =>
Result.wrapAsync(async () => {
const enumRegeditItems = (key: HKEY, subkey: string) => {
return enumerateKeys(key, subkey).map((k) =>
enumerateValues(key, subkey + "\\" + k),
);
return enumerateKeys(key, subkey).map((k) => enumerateValues(key, subkey + "\\" + k));
};

const items = [
Expand All @@ -141,11 +136,7 @@ export const adapter: AppReader = {
];

const results = await Promise.all(
items.map((itemValues) =>
Result.wrapAsync(async () =>
getAppInfoFromRegeditItemValues(itemValues),
),
),
items.map((itemValues) => Result.wrapAsync(async () => getAppInfoFromRegeditItemValues(itemValues))),
);

const apps = results.flatMap((app) => (app.ok ? [app.unwrap()] : []));
Expand All @@ -155,8 +146,9 @@ export const adapter: AppReader = {

readByPath: (p: string) =>
Result.wrapAsync(async () => {
if (path.extname(p).toLowerCase() === ".exe")
if (path.extname(p).toLowerCase() === ".exe") {
throw new Error("should be suffixed with exe");
}

return {
id: p,
Expand Down
4 changes: 2 additions & 2 deletions src/main/store.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { appSlice } from "../reducers/app";
import { sessionSlice } from "../reducers/session";
import { configureStore } from "@reduxjs/toolkit";
import { stateSyncEnhancer } from "electron-redux/main";
import { appSlice } from "../reducers/app";
import { sessionSlice } from "../reducers/session";

export const store = configureStore({
reducer: {
Expand Down
2 changes: 1 addition & 1 deletion src/main/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ export async function setUpdater() {
// break
// TODO: macOS: Make code sign work then use update-electron-app
default:
// setUpdateNotification();
// setUpdateNotification();
}
}
8 changes: 3 additions & 5 deletions src/renderer/app.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { sessionSlice } from "../reducers/session";
import "./app.css";
import { Header } from "./header";
import { Session } from "./session";
import { Colors } from "@blueprintjs/core";
import React from "react";
import { useSelector } from "react-redux";
import { useMedia } from "react-use";
import { Header } from "./header";
import { Session } from "./session";

export const App: React.FC = () => {
const darkMode = useMedia("(prefers-color-scheme: dark)");
Expand All @@ -23,9 +23,7 @@ export const App: React.FC = () => {
>
<Header />
<div style={{ flexGrow: 1, paddingLeft: 16, paddingRight: 16 }}>
{Object.entries(sessionStore).length ? (
<Session />
) : (
{Object.entries(sessionStore).length ? <Session /> : (
<div
style={{
fontSize: 24,
Expand Down
Loading

0 comments on commit 87b781a

Please sign in to comment.