From 87b781acfe47e1dbb75ff1910eb2b866fc5f90fa Mon Sep 17 00:00:00 2001 From: Rongjian Zhang Date: Thu, 25 Jul 2024 19:21:39 +0800 Subject: [PATCH] style: run format --- index.html | 2 +- src/main/actions.ts | 39 ++++++++++++-------------- src/main/main.ts | 21 ++++---------- src/main/platforms/linux.ts | 22 +++++++-------- src/main/platforms/linux/find-exec.ts | 5 +--- src/main/platforms/linux/find-icon.ts | 19 +++++-------- src/main/platforms/macos.ts | 2 +- src/main/platforms/utils.ts | 2 +- src/main/platforms/win.ts | 40 +++++++++++---------------- src/main/store.ts | 4 +-- src/main/utils.ts | 2 +- src/renderer/app.tsx | 8 ++---- src/renderer/header.tsx | 18 ++++-------- src/renderer/session.tsx | 29 ++++++------------- src/renderer/store.ts | 4 +-- src/renderer/xterm.tsx | 4 +-- vite.base.config.mjs | 12 ++++---- vite.main.config.mjs | 7 +---- vite.preload.config.mjs | 6 +--- vite.renderer.config.mjs | 2 +- 20 files changed, 94 insertions(+), 154 deletions(-) diff --git a/index.html b/index.html index f7286c2..ed42657 100644 --- a/index.html +++ b/index.html @@ -1,4 +1,4 @@ - + diff --git a/src/main/actions.ts b/src/main/actions.ts index 736816f..21c7d45 100644 --- a/src/main/actions.ts +++ b/src/main/actions.ts @@ -1,7 +1,3 @@ -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"; @@ -9,6 +5,10 @@ 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: P1, @@ -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( - 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 : [])), @@ -81,17 +79,15 @@ export const debug: ThunkActionCreator = (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()); @@ -101,7 +97,6 @@ export const debug: ThunkActionCreator = (app) => async (dispatch) => { } }; -export const debugPath: ThunkActionCreator = - (path) => async (dispatch) => { - // TODO: - }; +export const debugPath: ThunkActionCreator = (path) => async (dispatch) => { + // TODO: +}; diff --git a/src/main/main.ts b/src/main/main.ts index 74fede9..c4acd33 100644 --- a/src/main/main.ts +++ b/src/main/main.ts @@ -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")) { @@ -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` diff --git a/src/main/platforms/linux.ts b/src/main/platforms/linux.ts index 7ee2e3f..851d91b 100644 --- a/src/main/platforms/linux.ts +++ b/src/main/platforms/linux.ts @@ -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"; @@ -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 @@ -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"); diff --git a/src/main/platforms/linux/find-exec.ts b/src/main/platforms/linux/find-exec.ts index 7eea92d..db77428 100644 --- a/src/main/platforms/linux/find-exec.ts +++ b/src/main/platforms/linux/find-exec.ts @@ -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) { diff --git a/src/main/platforms/linux/find-icon.ts b/src/main/platforms/linux/find-icon.ts index 914c15c..c7cc390 100644 --- a/src/main/platforms/linux/find-icon.ts +++ b/src/main/platforms/linux/find-icon.ts @@ -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", @@ -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(); @@ -65,9 +62,7 @@ const themeSortedDirs = new Map( themeLeveledNames.map((theme) => [ theme, themeIconBases - .map((base) => - iconDirSortBySize(findAllIconDirs(base, path.join(base, theme))), - ) + .map((base) => iconDirSortBySize(findAllIconDirs(base, path.join(base, theme)))) .flat(), ]), ); diff --git a/src/main/platforms/macos.ts b/src/main/platforms/macos.ts index 6f8aa37..6329922 100644 --- a/src/main/platforms/macos.ts +++ b/src/main/platforms/macos.ts @@ -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; diff --git a/src/main/platforms/utils.ts b/src/main/platforms/utils.ts index 2a482fe..0ff5ac9 100644 --- a/src/main/platforms/utils.ts +++ b/src/main/platforms/utils.ts @@ -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>; diff --git a/src/main/platforms/win.ts b/src/main/platforms/win.ts index c5779ee..725b5c5 100644 --- a/src/main/platforms/win.ts +++ b/src/main/platforms/win.ts @@ -1,16 +1,16 @@ -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, @@ -18,8 +18,7 @@ async function getAppInfoByExePath( values: readonly RegistryValue[], ): Promise { 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) { @@ -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) { @@ -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) { @@ -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", @@ -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); @@ -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 = [ @@ -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()] : [])); @@ -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, diff --git a/src/main/store.ts b/src/main/store.ts index f338d11..1b7c277 100644 --- a/src/main/store.ts +++ b/src/main/store.ts @@ -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: { diff --git a/src/main/utils.ts b/src/main/utils.ts index 8cb9c1f..203a48b 100644 --- a/src/main/utils.ts +++ b/src/main/utils.ts @@ -18,6 +18,6 @@ export async function setUpdater() { // break // TODO: macOS: Make code sign work then use update-electron-app default: - // setUpdateNotification(); + // setUpdateNotification(); } } diff --git a/src/renderer/app.tsx b/src/renderer/app.tsx index 375792a..2636534 100644 --- a/src/renderer/app.tsx +++ b/src/renderer/app.tsx @@ -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)"); @@ -23,9 +23,7 @@ export const App: React.FC = () => { >
- {Object.entries(sessionStore).length ? ( - - ) : ( + {Object.entries(sessionStore).length ? : (
{ const appState = useSelector(appSlice.selectSlice); @@ -36,8 +30,8 @@ export const Header: FC = () => { itemPredicate={(query, item) => { const lq = query.toLowerCase(); return ( - item.name.toLowerCase().includes(lq) || - item.id.toLowerCase().includes(lq) + item.name.toLowerCase().includes(lq) + || item.id.toLowerCase().includes(lq) ); }} itemRenderer={(item, { modifiers, handleClick, handleFocus }) => { diff --git a/src/renderer/session.tsx b/src/renderer/session.tsx index 08a6f21..b315dbb 100644 --- a/src/renderer/session.tsx +++ b/src/renderer/session.tsx @@ -1,17 +1,9 @@ +import { Button, Divider, HTMLTable, Pre, Tab, Tabs, Tag } from "@blueprintjs/core"; +import { type FC, useEffect, useState } from "react"; +import { useSelector } from "react-redux"; import { appSlice } from "../reducers/app"; import { sessionSlice } from "../reducers/session"; import { Xterm } from "./xterm"; -import { - Tabs, - Tab, - Divider, - Pre, - Tag, - HTMLTable, - Button, -} from "@blueprintjs/core"; -import { useEffect, type FC, useState } from "react"; -import { useSelector } from "react-redux"; export const Session: FC = () => { const [activeId, setActiveId] = useState(""); @@ -66,13 +58,11 @@ export const Session: FC = () => { {page.type} @@ -126,8 +116,7 @@ export const Session: FC = () => { diff --git a/src/renderer/store.ts b/src/renderer/store.ts index 1eb63f2..3531922 100644 --- a/src/renderer/store.ts +++ b/src/renderer/store.ts @@ -1,7 +1,7 @@ -import { appSlice } from "../reducers/app"; -import { sessionSlice } from "../reducers/session"; import { configureStore } from "@reduxjs/toolkit"; import { stateSyncEnhancer } from "electron-redux/renderer"; +import { appSlice } from "../reducers/app"; +import { sessionSlice } from "../reducers/session"; export const store = configureStore({ reducer: { diff --git a/src/renderer/xterm.tsx b/src/renderer/xterm.tsx index 8f0e951..183fea0 100644 --- a/src/renderer/xterm.tsx +++ b/src/renderer/xterm.tsx @@ -1,8 +1,8 @@ import { CanvasAddon } from "@xterm/addon-canvas"; import { FitAddon } from "@xterm/addon-fit"; -import { Terminal, type ITerminalOptions } from "@xterm/xterm"; +import { type ITerminalOptions, Terminal } from "@xterm/xterm"; import "@xterm/xterm/css/xterm.css"; -import { useEffect, useRef, type FC } from "react"; +import { type FC, useEffect, useRef } from "react"; export const Xterm: FC<{ content: string; options?: ITerminalOptions }> = ({ content, diff --git a/vite.base.config.mjs b/vite.base.config.mjs index 8b624e9..1f2d591 100644 --- a/vite.base.config.mjs +++ b/vite.base.config.mjs @@ -1,5 +1,5 @@ -import pkg from "./package.json"; import { builtinModules } from "node:module"; +import pkg from "./package.json"; export const builtins = [ "electron", @@ -54,10 +54,9 @@ export const getBuildDefine = (env) => { const define = Object.entries(defineKeys).reduce((acc, [name, keys]) => { const { VITE_DEV_SERVER_URL, VITE_NAME } = keys; const def = { - [VITE_DEV_SERVER_URL]: - command === "serve" - ? JSON.stringify(process.env[VITE_DEV_SERVER_URL]) - : undefined, + [VITE_DEV_SERVER_URL]: command === "serve" + ? JSON.stringify(process.env[VITE_DEV_SERVER_URL]) + : undefined, [VITE_NAME]: JSON.stringify(name), }; return { ...acc, ...def }; @@ -81,8 +80,7 @@ export const pluginExposeRenderer = (name) => { /** @type {import('node:net').AddressInfo} */ const addressInfo = server.httpServer?.address(); // Expose env constant for main process use. - process.env[VITE_DEV_SERVER_URL] = - `http://localhost:${addressInfo?.port}`; + process.env[VITE_DEV_SERVER_URL] = `http://localhost:${addressInfo?.port}`; }); }, }; diff --git a/vite.main.config.mjs b/vite.main.config.mjs index 14448a1..c7dc422 100644 --- a/vite.main.config.mjs +++ b/vite.main.config.mjs @@ -1,10 +1,5 @@ -import { - getBuildConfig, - getBuildDefine, - external, - pluginHotRestart, -} from "./vite.base.config.mjs"; import { defineConfig, mergeConfig } from "vite"; +import { external, getBuildConfig, getBuildDefine, pluginHotRestart } from "./vite.base.config.mjs"; // https://vitejs.dev/config export default defineConfig((env) => { diff --git a/vite.preload.config.mjs b/vite.preload.config.mjs index 9fcfea8..62a5863 100644 --- a/vite.preload.config.mjs +++ b/vite.preload.config.mjs @@ -1,9 +1,5 @@ -import { - getBuildConfig, - external, - pluginHotRestart, -} from "./vite.base.config.mjs"; import { defineConfig, mergeConfig } from "vite"; +import { external, getBuildConfig, pluginHotRestart } from "./vite.base.config.mjs"; // https://vitejs.dev/config export default defineConfig((env) => { diff --git a/vite.renderer.config.mjs b/vite.renderer.config.mjs index 6465db0..e021ba0 100644 --- a/vite.renderer.config.mjs +++ b/vite.renderer.config.mjs @@ -1,5 +1,5 @@ -import { pluginExposeRenderer } from "./vite.base.config.mjs"; import { defineConfig } from "vite"; +import { pluginExposeRenderer } from "./vite.base.config.mjs"; // https://vitejs.dev/config export default defineConfig((env) => {