From 05cc9310d1faa35b8fed1e21c9e61c389dd4ada1 Mon Sep 17 00:00:00 2001 From: xhayper Date: Thu, 27 Oct 2022 14:02:19 +0700 Subject: [PATCH 1/4] chore: initial code cleanup --- .eslintrc.cjs | 5 +- src/activity.ts | 201 +++++++++++++++++---------------- src/constants.ts | 4 +- src/controller.ts | 2 +- src/extension.ts | 8 +- src/helpers/getFileSize.ts | 7 +- src/helpers/resolveLangName.ts | 4 +- src/logger.ts | 6 +- 8 files changed, 122 insertions(+), 115 deletions(-) diff --git a/.eslintrc.cjs b/.eslintrc.cjs index f28f5d6a..1346e338 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -11,5 +11,8 @@ module.exports = { "plugin:@typescript-eslint/recommended", "plugin:@typescript-eslint/recommended-requiring-type-checking", "prettier" - ] + ], + rules: { + "no-empty": "off" + } }; diff --git a/src/activity.ts b/src/activity.ts index 9d586ddd..b70472d8 100644 --- a/src/activity.ts +++ b/src/activity.ts @@ -9,6 +9,13 @@ import { getConfig } from "./config"; import { dataClass } from "./data"; import { sep } from "node:path"; +export enum CURRENT_STATUS { + IDLE = "idle", + EDITING = "editing", + DEBUGGING = "debugging", + VIEWING = "viewing" +} + // TODO: move this to data class export let totalProblems = 0; @@ -35,11 +42,13 @@ export const activity = async ( const presence = previous; - presence.startTimestamp = config.get(CONFIG_KEYS.Status.ShowElapsedTime) - ? config.get(CONFIG_KEYS.Status.ResetElapsedTimePerFile) + if (config.get(CONFIG_KEYS.Status.ShowElapsedTime)) { + presence.startTimestamp = config.get(CONFIG_KEYS.Status.ResetElapsedTimePerFile) ? Date.now() - : previous.startTimestamp ?? Date.now() - : undefined; + : previous.startTimestamp ?? Date.now(); + } else { + delete presence.startTimestamp; + } const detailsEnabled = config.get(CONFIG_KEYS.Status.Details.Enabled); const detailsIdleEnabled = config.get(CONFIG_KEYS.Status.Details.Idle.Enabled); @@ -65,6 +74,12 @@ export const activity = async ( const isDebugging = !!debug.activeDebugSession; isViewing = !isDebugging && isViewing; + let status: CURRENT_STATUS; + if (isIdling) status = CURRENT_STATUS.IDLE; + else if (isDebugging) status = CURRENT_STATUS.DEBUGGING; + else if (isViewing) status = CURRENT_STATUS.VIEWING; + else status = CURRENT_STATUS.EDITING; + const PROBLEMS = config.get(CONFIG_KEYS.Status.Problems.Enabled) ? await replaceFileInfo( replaceGitInfo(replaceAppInfo(config.get(CONFIG_KEYS.Status.Problems.Text)), isGitExcluded), @@ -97,83 +112,77 @@ export const activity = async ( workspaceExcludedText = text !== "" ? text : undefined ?? workspaceExcludedText; } - const detailsText = detailsEnabled - ? isWorkspaceExcluded - ? workspaceExcludedText - : isIdling || !dataClass.editor - ? detailsIdleEnabled - ? await replaceAllText(config.get(CONFIG_KEYS.Status.Details.Text.Idle)) - : undefined - : await replaceAllText( - isDebugging - ? config.get(CONFIG_KEYS.Status.Details.Text.Debugging) - : isViewing - ? config.get(CONFIG_KEYS.Status.Details.Text.Viewing) - : config.get(CONFIG_KEYS.Status.Details.Text.Editing) - ) - : undefined; - - const stateText = - stateEnabled && !isWorkspaceExcluded - ? isIdling || !dataClass.editor - ? stateIdleEnabled - ? await replaceAllText(config.get(CONFIG_KEYS.Status.State.Text.Idle)) - : undefined - : await replaceAllText( - isDebugging - ? config.get(CONFIG_KEYS.Status.State.Text.Debugging) - : isViewing - ? config.get(CONFIG_KEYS.Status.State.Text.Viewing) - : config.get(CONFIG_KEYS.Status.State.Text.Editing) - ) - : undefined; - - const largeImageKey = await replaceAllText( - isIdling || !dataClass.editor - ? config.get(CONFIG_KEYS.Status.Image.Large.Idle.Key) - : isDebugging - ? config.get(CONFIG_KEYS.Status.Image.Large.Debugging.Key) - : isViewing - ? config.get(CONFIG_KEYS.Status.Image.Large.Viewing.Key) - : config.get(CONFIG_KEYS.Status.Image.Large.Editing.Key) - ); - - const largeImageText = await replaceAllText( - isIdling || !dataClass.editor - ? config.get(CONFIG_KEYS.Status.Image.Large.Idle.Text) - : isDebugging - ? config.get(CONFIG_KEYS.Status.Image.Large.Debugging.Text) - : isViewing - ? config.get(CONFIG_KEYS.Status.Image.Large.Viewing.Text) - : config.get(CONFIG_KEYS.Status.Image.Large.Editing.Text) - ); - - const smallImageKey = await replaceAllText( - isIdling || !dataClass.editor - ? config.get(CONFIG_KEYS.Status.Image.Small.Idle.Key) - : isDebugging - ? config.get(CONFIG_KEYS.Status.Image.Small.Debugging.Key) - : isViewing - ? config.get(CONFIG_KEYS.Status.Image.Small.Viewing.Key) - : config.get(CONFIG_KEYS.Status.Image.Small.Editing.Key) - ); - - const smallImageText = await replaceAllText( - isIdling || !dataClass.editor - ? config.get(CONFIG_KEYS.Status.Image.Small.Idle.Text) - : isDebugging - ? config.get(CONFIG_KEYS.Status.Image.Small.Debugging.Text) - : isViewing - ? config.get(CONFIG_KEYS.Status.Image.Small.Viewing.Text) - : config.get(CONFIG_KEYS.Status.Image.Small.Editing.Text) - ); - - presence.details = detailsEnabled ? detailsText : undefined; - presence.state = stateEnabled ? stateText : undefined; - presence.largeImageKey = largeImageKey; - presence.largeImageText = largeImageText; - presence.smallImageKey = smallImageKey; - presence.smallImageText = smallImageText; + let deatilsTemplate = isWorkspaceExcluded ? workspaceExcludedText : FAKE_EMPTY; + let stateTemplate = FAKE_EMPTY; + + let largeImageKeyTemplate = FAKE_EMPTY; + let largeImageTextTemplate = FAKE_EMPTY; + + let smallImageKeyTemplate = FAKE_EMPTY; + let smallImageTextTemplate = FAKE_EMPTY; + + switch (status) { + case CURRENT_STATUS.IDLE: { + if (!isWorkspaceExcluded) { + if (detailsIdleEnabled && detailsEnabled) + deatilsTemplate = config.get(CONFIG_KEYS.Status.Details.Text.Idle); + if (stateIdleEnabled && stateEnabled) stateTemplate = config.get(CONFIG_KEYS.Status.State.Text.Idle); + } + + largeImageKeyTemplate = config.get(CONFIG_KEYS.Status.Image.Large.Idle.Key); + largeImageTextTemplate = config.get(CONFIG_KEYS.Status.Image.Large.Idle.Text); + + smallImageKeyTemplate = config.get(CONFIG_KEYS.Status.Image.Small.Idle.Key); + smallImageTextTemplate = config.get(CONFIG_KEYS.Status.Image.Small.Idle.Text); + break; + } + case CURRENT_STATUS.EDITING: { + if (!isWorkspaceExcluded) { + if (detailsEnabled) deatilsTemplate = config.get(CONFIG_KEYS.Status.Details.Text.Editing); + if (stateEnabled) stateTemplate = config.get(CONFIG_KEYS.Status.State.Text.Editing); + } + + largeImageKeyTemplate = config.get(CONFIG_KEYS.Status.Image.Large.Editing.Key); + largeImageTextTemplate = config.get(CONFIG_KEYS.Status.Image.Large.Editing.Text); + + smallImageKeyTemplate = config.get(CONFIG_KEYS.Status.Image.Small.Editing.Key); + smallImageTextTemplate = config.get(CONFIG_KEYS.Status.Image.Small.Editing.Text); + break; + } + case CURRENT_STATUS.DEBUGGING: { + if (!isWorkspaceExcluded) { + if (detailsEnabled) deatilsTemplate = config.get(CONFIG_KEYS.Status.Details.Text.Debugging); + if (stateEnabled) stateTemplate = config.get(CONFIG_KEYS.Status.State.Text.Debugging); + } + + largeImageKeyTemplate = config.get(CONFIG_KEYS.Status.Image.Large.Debugging.Key); + largeImageTextTemplate = config.get(CONFIG_KEYS.Status.Image.Large.Debugging.Text); + + smallImageKeyTemplate = config.get(CONFIG_KEYS.Status.Image.Small.Debugging.Key); + smallImageTextTemplate = config.get(CONFIG_KEYS.Status.Image.Small.Debugging.Text); + break; + } + case CURRENT_STATUS.VIEWING: { + if (!isWorkspaceExcluded) { + if (detailsEnabled) deatilsTemplate = config.get(CONFIG_KEYS.Status.Details.Text.Viewing); + if (stateEnabled) stateTemplate = config.get(CONFIG_KEYS.Status.State.Text.Viewing); + } + + largeImageKeyTemplate = config.get(CONFIG_KEYS.Status.Image.Large.Viewing.Key); + largeImageTextTemplate = config.get(CONFIG_KEYS.Status.Image.Large.Viewing.Text); + + smallImageKeyTemplate = config.get(CONFIG_KEYS.Status.Image.Small.Viewing.Key); + smallImageTextTemplate = config.get(CONFIG_KEYS.Status.Image.Small.Viewing.Text); + break; + } + } + + presence.details = detailsEnabled ? await replaceAllText(deatilsTemplate) : undefined; + presence.state = stateEnabled ? await replaceAllText(stateTemplate) : undefined; + presence.largeImageKey = await replaceAllText(largeImageKeyTemplate); + presence.largeImageText = await replaceAllText(largeImageTextTemplate); + presence.smallImageKey = await replaceAllText(smallImageKeyTemplate); + presence.smallImageText = await replaceAllText(smallImageTextTemplate); if (isIdling || !dataClass.editor) { if (config.get(CONFIG_KEYS.Status.Button.Idle.Enabled)) @@ -220,12 +229,12 @@ export const replaceAppInfo = (text: string): string => { const isInsider = appName.includes("Insiders"); const isCodium = appName.startsWith("VSCodium") || appName.startsWith("codium"); + const insiderAppName = isCodium ? "vscodium-insiders" : "vscode-insiders"; + const normalAppName = isCodium ? "vscodium" : "vscode"; + const replaceMap = new Map([ ["{app_name}", appName], - [ - "{app_id}", - isInsider ? (isCodium ? "vscodium-insiders" : "vscode-insiders") : isCodium ? "vscodium" : "vscode" - ] + ["{app_id}", isInsider ? insiderAppName : normalAppName] ]); for (const [key, value] of replaceMap) text = text.replaceAll(key, value); @@ -239,11 +248,7 @@ export const replaceGitInfo = (text: string, excluded = false): string => { const replaceMap = new Map([ ["{git_owner}", (!excluded ? dataClass.gitRemoteUrl?.owner : undefined) ?? FAKE_EMPTY], ["{git_provider}", (!excluded ? dataClass.gitRemoteUrl?.source : undefined) ?? FAKE_EMPTY], - [ - "{git_repo}", - (!excluded ? (dataClass.gitRemoteUrl ? dataClass.gitRemoteUrl.name : dataClass.gitRepoName) : undefined) ?? - FAKE_EMPTY - ], + ["{git_repo}", (!excluded ? dataClass.gitRemoteUrl?.name ?? dataClass.gitRepoName : undefined) ?? FAKE_EMPTY], ["{git_branch}", (!excluded ? dataClass.gitBranchName : undefined) ?? FAKE_EMPTY], ["{git_url}", (!excluded ? dataClass.gitRemoteUrl?.toString("https") : undefined) ?? FAKE_EMPTY] ]); @@ -262,11 +267,10 @@ export const replaceFileInfo = async ( const config = getConfig(); text = text.slice(); - const workspaceFolderName = (!excluded ? dataClass.workspaceFolder?.name : undefined) ?? FAKE_EMPTY; - const workspaceName = (!excluded ? dataClass.workspaceName : undefined) ?? workspaceFolderName; - const workspaceAndFolder = !excluded - ? `${workspaceName}${workspaceFolderName === FAKE_EMPTY ? "" : ` - ${workspaceFolderName}`}` - : FAKE_EMPTY; + let workspaceFolderName = dataClass.workspaceFolder?.name ?? FAKE_EMPTY; + let workspaceName = dataClass.workspaceName ?? FAKE_EMPTY; + let workspaceAndFolder = + workspaceName + (workspaceFolderName != FAKE_EMPTY ? ` - ${workspaceFolderName}` : FAKE_EMPTY); let fullDirectoryName: string = FAKE_EMPTY; const fileIcon = dataClass.editor ? resolveLangName(dataClass.editor.document) : "text"; @@ -280,6 +284,13 @@ export const replaceFileInfo = async ( fullDirectoryName = `${name}${sep}${relativePath.join(sep)}`; } + if (excluded) { + workspaceFolderName = FAKE_EMPTY; + workspaceName = FAKE_EMPTY; + workspaceAndFolder = FAKE_EMPTY; + fullDirectoryName = FAKE_EMPTY; + } + const replaceMap = new Map([ ["{file_name}", dataClass.fileName ?? FAKE_EMPTY], ["{file_extenstion}", dataClass.fileExtension ?? FAKE_EMPTY], diff --git a/src/constants.ts b/src/constants.ts index b1005e2c..036fd965 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -5,8 +5,8 @@ export const { KNOWN_EXTENSIONS, KNOWN_LANGUAGES } = lang as { KNOWN_LANGUAGES: { language: string; image: string }[]; }; -export const EMPTY = "" as const; -export const FAKE_EMPTY = "\u200b\u200b" as const; +export const EMPTY = ""; +export const FAKE_EMPTY = "\u200b\u200b"; export const CONFIG_KEYS = { Enabled: "enabled" as const, diff --git a/src/controller.ts b/src/controller.ts index eea0ad9c..5d05b6a9 100644 --- a/src/controller.ts +++ b/src/controller.ts @@ -49,7 +49,7 @@ export class RPCController { this.statusBarIcon.command = "vscord.reconnect"; this.statusBarIcon.tooltip = "Reconnect to Discord Gateway"; }) - .then(() => void logInfo(`Successfully logged in to Discord with client ID ${clientId}`)); + .then(() => logInfo(`Successfully logged in to Discord with client ID ${clientId}`)); this.client.on("debug", (...data) => { if (!this.debug) return; diff --git a/src/extension.ts b/src/extension.ts index 4dc8b860..66d35e62 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -37,9 +37,7 @@ export const registerCommands = (ctx: ExtensionContext) => { if (update) try { await config.update(CONFIG_KEYS.Enabled, true); - } catch { - null; - } + } catch {} await controller.enable(); }; @@ -48,9 +46,7 @@ export const registerCommands = (ctx: ExtensionContext) => { if (update) try { await config.update(CONFIG_KEYS.Enabled, false); - } catch { - null; - } + } catch {} await controller.disable(); diff --git a/src/helpers/getFileSize.ts b/src/helpers/getFileSize.ts index b8a3f867..8ac1748c 100644 --- a/src/helpers/getFileSize.ts +++ b/src/helpers/getFileSize.ts @@ -14,7 +14,6 @@ export const getFileSize = async (config: ExtenstionConfiguration, dataClass: Da if (config.get(CONFIG_KEYS.File.Size.Spacer) === "" || config.get(CONFIG_KEYS.File.Size.Spacer)) spacer = config.get(CONFIG_KEYS.File.Size.Spacer); - let fileSize: string | undefined; const fileSizeStandard: FileSizeStandard = config.get(CONFIG_KEYS.File.Size.Standard) ?? "iec"; const fileSizeConfig: FileSizeConfig = { round, @@ -22,9 +21,9 @@ export const getFileSize = async (config: ExtenstionConfiguration, dataClass: Da standard: fileSizeStandard }; - fileSize = config.get(CONFIG_KEYS.File.Size.HumanReadable) - ? (fileSize = filesize((await dataClass.fileSize) ?? 0, fileSizeConfig).toLocaleString()) - : (fileSize = `${dataClass.fileSize.toLocaleString()}${fileSizeConfig.spacer ?? " "}B`); + const fileSize = config.get(CONFIG_KEYS.File.Size.HumanReadable) + ? filesize((await dataClass.fileSize) ?? 0, fileSizeConfig).toLocaleString() + : `${dataClass.fileSize.toLocaleString()}${fileSizeConfig.spacer ?? " "}B`; return fileSize; }; diff --git a/src/helpers/resolveLangName.ts b/src/helpers/resolveLangName.ts index 63585780..dbf117d5 100644 --- a/src/helpers/resolveLangName.ts +++ b/src/helpers/resolveLangName.ts @@ -30,9 +30,7 @@ export const resolveLangName = (document: TextDocument) => { const knownExtension = findKnownExtension ? ALL_KNOWN_KNOWN_EXTENSIONS[findKnownExtension] - : findKnownLanguage - ? findKnownLanguage.image - : null; + : findKnownLanguage?.image; const knownLanguage = findKnownLanguage ? findKnownLanguage.image : knownExtension; const fileIcon = areLanguagesPrioritized ? knownLanguage : knownExtension; diff --git a/src/logger.ts b/src/logger.ts index 38040f7d..34150c1a 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -30,8 +30,8 @@ const logMessage = (logLevel: LogLevel, ...messageList: unknown[]) => { outputChannel.appendLine(`[${timestamp}] [${logLevel}] ${messageToLog.join(" ")}`); }; -export const logInfo = (...message: unknown[]) => void logMessage(LogLevel.INFO, ...message); +export const logInfo = (...message: unknown[]) => logMessage(LogLevel.INFO, ...message); -export const logWarn = (...message: unknown[]) => void logMessage(LogLevel.WARN, ...message); +export const logWarn = (...message: unknown[]) => logMessage(LogLevel.WARN, ...message); -export const logError = (...message: unknown[]) => void logMessage(LogLevel.ERROR, ...message); +export const logError = (...message: unknown[]) => logMessage(LogLevel.ERROR, ...message); From fd70df118879216911b525b0e81a8bbd60920122 Mon Sep 17 00:00:00 2001 From: xhayper Date: Thu, 27 Oct 2022 14:06:58 +0700 Subject: [PATCH 2/4] chore: more clean up --- src/activity.ts | 81 +++++++++++++++++++++++++++---------------------- 1 file changed, 44 insertions(+), 37 deletions(-) diff --git a/src/activity.ts b/src/activity.ts index b70472d8..6067570f 100644 --- a/src/activity.ts +++ b/src/activity.ts @@ -1,7 +1,7 @@ import { type Selection, type TextDocument, debug, DiagnosticSeverity, env, languages, workspace } from "vscode"; import { resolveLangName, toLower, toTitle, toUpper } from "./helpers/resolveLangName"; import { type SetActivity } from "@xhayper/discord-rpc"; -import { CONFIG_KEYS, FAKE_EMPTY } from "./constants"; +import { CONFIG_KEYS, EMPTY, FAKE_EMPTY } from "./constants"; import { getFileSize } from "./helpers/getFileSize"; import { isExcluded } from "./helpers/isExcluded"; import { isObject } from "./helpers/isObject"; @@ -112,77 +112,84 @@ export const activity = async ( workspaceExcludedText = text !== "" ? text : undefined ?? workspaceExcludedText; } - let deatilsTemplate = isWorkspaceExcluded ? workspaceExcludedText : FAKE_EMPTY; - let stateTemplate = FAKE_EMPTY; + let details = isWorkspaceExcluded ? workspaceExcludedText : undefined; + let state = undefined; - let largeImageKeyTemplate = FAKE_EMPTY; - let largeImageTextTemplate = FAKE_EMPTY; + let largeImageKey = undefined; + let largeImageText = undefined; - let smallImageKeyTemplate = FAKE_EMPTY; - let smallImageTextTemplate = FAKE_EMPTY; + let smallImageKey = undefined; + let smallImageText = undefined; switch (status) { case CURRENT_STATUS.IDLE: { if (!isWorkspaceExcluded) { if (detailsIdleEnabled && detailsEnabled) - deatilsTemplate = config.get(CONFIG_KEYS.Status.Details.Text.Idle); - if (stateIdleEnabled && stateEnabled) stateTemplate = config.get(CONFIG_KEYS.Status.State.Text.Idle); + details = await replaceAllText(config.get(CONFIG_KEYS.Status.Details.Text.Idle)); + if (stateIdleEnabled && stateEnabled) + state = await replaceAllText(config.get(CONFIG_KEYS.Status.State.Text.Idle)); } - largeImageKeyTemplate = config.get(CONFIG_KEYS.Status.Image.Large.Idle.Key); - largeImageTextTemplate = config.get(CONFIG_KEYS.Status.Image.Large.Idle.Text); + largeImageKey = await replaceAllText(config.get(CONFIG_KEYS.Status.Image.Large.Idle.Key)); + largeImageText = await replaceAllText(config.get(CONFIG_KEYS.Status.Image.Large.Idle.Text)); - smallImageKeyTemplate = config.get(CONFIG_KEYS.Status.Image.Small.Idle.Key); - smallImageTextTemplate = config.get(CONFIG_KEYS.Status.Image.Small.Idle.Text); + smallImageKey = await replaceAllText(config.get(CONFIG_KEYS.Status.Image.Small.Idle.Key)); + smallImageText = await replaceAllText(config.get(CONFIG_KEYS.Status.Image.Small.Idle.Text)); break; } case CURRENT_STATUS.EDITING: { if (!isWorkspaceExcluded) { - if (detailsEnabled) deatilsTemplate = config.get(CONFIG_KEYS.Status.Details.Text.Editing); - if (stateEnabled) stateTemplate = config.get(CONFIG_KEYS.Status.State.Text.Editing); + if (detailsEnabled) + details = await replaceAllText(config.get(CONFIG_KEYS.Status.Details.Text.Editing)); + if (stateEnabled) + state = await replaceAllText(config.get(CONFIG_KEYS.Status.State.Text.Editing)); } - largeImageKeyTemplate = config.get(CONFIG_KEYS.Status.Image.Large.Editing.Key); - largeImageTextTemplate = config.get(CONFIG_KEYS.Status.Image.Large.Editing.Text); + largeImageKey = await replaceAllText(config.get(CONFIG_KEYS.Status.Image.Large.Editing.Key)); + largeImageText = await replaceAllText(config.get(CONFIG_KEYS.Status.Image.Large.Editing.Text)); - smallImageKeyTemplate = config.get(CONFIG_KEYS.Status.Image.Small.Editing.Key); - smallImageTextTemplate = config.get(CONFIG_KEYS.Status.Image.Small.Editing.Text); + smallImageKey = await replaceAllText(config.get(CONFIG_KEYS.Status.Image.Small.Editing.Key)); + smallImageText = await replaceAllText(config.get(CONFIG_KEYS.Status.Image.Small.Editing.Text)); break; } case CURRENT_STATUS.DEBUGGING: { if (!isWorkspaceExcluded) { - if (detailsEnabled) deatilsTemplate = config.get(CONFIG_KEYS.Status.Details.Text.Debugging); - if (stateEnabled) stateTemplate = config.get(CONFIG_KEYS.Status.State.Text.Debugging); + if (detailsEnabled) + details = await replaceAllText(config.get(CONFIG_KEYS.Status.Details.Text.Debugging)); + if (stateEnabled) + state = await replaceAllText(config.get(CONFIG_KEYS.Status.State.Text.Debugging)); } - largeImageKeyTemplate = config.get(CONFIG_KEYS.Status.Image.Large.Debugging.Key); - largeImageTextTemplate = config.get(CONFIG_KEYS.Status.Image.Large.Debugging.Text); + largeImageKey = await replaceAllText(config.get(CONFIG_KEYS.Status.Image.Large.Debugging.Key)); + largeImageText = await replaceAllText(config.get(CONFIG_KEYS.Status.Image.Large.Debugging.Text)); - smallImageKeyTemplate = config.get(CONFIG_KEYS.Status.Image.Small.Debugging.Key); - smallImageTextTemplate = config.get(CONFIG_KEYS.Status.Image.Small.Debugging.Text); + smallImageKey = await replaceAllText(config.get(CONFIG_KEYS.Status.Image.Small.Debugging.Key)); + smallImageText = await replaceAllText(config.get(CONFIG_KEYS.Status.Image.Small.Debugging.Text)); break; } case CURRENT_STATUS.VIEWING: { if (!isWorkspaceExcluded) { - if (detailsEnabled) deatilsTemplate = config.get(CONFIG_KEYS.Status.Details.Text.Viewing); - if (stateEnabled) stateTemplate = config.get(CONFIG_KEYS.Status.State.Text.Viewing); + if (detailsEnabled) + details = await replaceAllText(config.get(CONFIG_KEYS.Status.Details.Text.Viewing)); + if (stateEnabled) + state = await replaceAllText(config.get(CONFIG_KEYS.Status.State.Text.Viewing)); } - largeImageKeyTemplate = config.get(CONFIG_KEYS.Status.Image.Large.Viewing.Key); - largeImageTextTemplate = config.get(CONFIG_KEYS.Status.Image.Large.Viewing.Text); + largeImageKey = await replaceAllText(config.get(CONFIG_KEYS.Status.Image.Large.Viewing.Key)); + largeImageText = await replaceAllText(config.get(CONFIG_KEYS.Status.Image.Large.Viewing.Text)); - smallImageKeyTemplate = config.get(CONFIG_KEYS.Status.Image.Small.Viewing.Key); - smallImageTextTemplate = config.get(CONFIG_KEYS.Status.Image.Small.Viewing.Text); + smallImageKey = await replaceAllText(config.get(CONFIG_KEYS.Status.Image.Small.Viewing.Key)); + smallImageText = await replaceAllText(config.get(CONFIG_KEYS.Status.Image.Small.Viewing.Text)); break; } } - presence.details = detailsEnabled ? await replaceAllText(deatilsTemplate) : undefined; - presence.state = stateEnabled ? await replaceAllText(stateTemplate) : undefined; - presence.largeImageKey = await replaceAllText(largeImageKeyTemplate); - presence.largeImageText = await replaceAllText(largeImageTextTemplate); - presence.smallImageKey = await replaceAllText(smallImageKeyTemplate); - presence.smallImageText = await replaceAllText(smallImageTextTemplate); + presence.details = details; + presence.state = state; + presence.largeImageKey = largeImageKey; + presence.largeImageText = largeImageText; + presence.smallImageKey = smallImageKey; + presence.smallImageText = smallImageText; if (isIdling || !dataClass.editor) { if (config.get(CONFIG_KEYS.Status.Button.Idle.Enabled)) From 20573e382953c261b019f7f1efce34f8abecbaf0 Mon Sep 17 00:00:00 2001 From: xhayper Date: Thu, 27 Oct 2022 14:09:50 +0700 Subject: [PATCH 3/4] chore: remove unused import --- src/activity.ts | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/activity.ts b/src/activity.ts index 6067570f..1e999d32 100644 --- a/src/activity.ts +++ b/src/activity.ts @@ -1,7 +1,7 @@ import { type Selection, type TextDocument, debug, DiagnosticSeverity, env, languages, workspace } from "vscode"; import { resolveLangName, toLower, toTitle, toUpper } from "./helpers/resolveLangName"; import { type SetActivity } from "@xhayper/discord-rpc"; -import { CONFIG_KEYS, EMPTY, FAKE_EMPTY } from "./constants"; +import { CONFIG_KEYS, FAKE_EMPTY } from "./constants"; import { getFileSize } from "./helpers/getFileSize"; import { isExcluded } from "./helpers/isExcluded"; import { isObject } from "./helpers/isObject"; @@ -139,10 +139,8 @@ export const activity = async ( } case CURRENT_STATUS.EDITING: { if (!isWorkspaceExcluded) { - if (detailsEnabled) - details = await replaceAllText(config.get(CONFIG_KEYS.Status.Details.Text.Editing)); - if (stateEnabled) - state = await replaceAllText(config.get(CONFIG_KEYS.Status.State.Text.Editing)); + if (detailsEnabled) details = await replaceAllText(config.get(CONFIG_KEYS.Status.Details.Text.Editing)); + if (stateEnabled) state = await replaceAllText(config.get(CONFIG_KEYS.Status.State.Text.Editing)); } largeImageKey = await replaceAllText(config.get(CONFIG_KEYS.Status.Image.Large.Editing.Key)); @@ -156,8 +154,7 @@ export const activity = async ( if (!isWorkspaceExcluded) { if (detailsEnabled) details = await replaceAllText(config.get(CONFIG_KEYS.Status.Details.Text.Debugging)); - if (stateEnabled) - state = await replaceAllText(config.get(CONFIG_KEYS.Status.State.Text.Debugging)); + if (stateEnabled) state = await replaceAllText(config.get(CONFIG_KEYS.Status.State.Text.Debugging)); } largeImageKey = await replaceAllText(config.get(CONFIG_KEYS.Status.Image.Large.Debugging.Key)); @@ -169,10 +166,8 @@ export const activity = async ( } case CURRENT_STATUS.VIEWING: { if (!isWorkspaceExcluded) { - if (detailsEnabled) - details = await replaceAllText(config.get(CONFIG_KEYS.Status.Details.Text.Viewing)); - if (stateEnabled) - state = await replaceAllText(config.get(CONFIG_KEYS.Status.State.Text.Viewing)); + if (detailsEnabled) details = await replaceAllText(config.get(CONFIG_KEYS.Status.Details.Text.Viewing)); + if (stateEnabled) state = await replaceAllText(config.get(CONFIG_KEYS.Status.State.Text.Viewing)); } largeImageKey = await replaceAllText(config.get(CONFIG_KEYS.Status.Image.Large.Viewing.Key)); From 2d2152262334fa8b19d5b496f408ef641d4ed63d Mon Sep 17 00:00:00 2001 From: xhayper Date: Sat, 29 Oct 2022 11:13:17 +0700 Subject: [PATCH 4/4] chore: more cleanup --- .vscode/launch.json | 28 +++++++++++++++------------- .vscode/settings.json | 12 ++++++++++-- .vscode/tasks.json | 17 +++-------------- package.json | 12 +++++------- src/controller.ts | 10 +++++++--- yarn.lock | 26 +++++++++++++------------- 6 files changed, 53 insertions(+), 52 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 0beb4d79..c42edc04 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -3,17 +3,19 @@ // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 { - "version": "0.2.0", - "configurations": [ - { - "name": "Run Extension", - "type": "extensionHost", - "request": "launch", - "runtimeExecutable": "${execPath}", - "args": ["--extensionDevelopmentPath=${workspaceFolder}"], - "sourceMaps": true, - "outFiles": ["${workspaceFolder}/dist/**/*.js"], - "preLaunchTask": "npm: watch" - } - ] + "version": "0.2.0", + "configurations": [ + { + "name": "Run Extension", + "type": "extensionHost", + "request": "launch", + "args": [ + "--extensionDevelopmentPath=${workspaceFolder}" + ], + "outFiles": [ + "${workspaceFolder}/dist/**/*.js" + ], + "preLaunchTask": "${defaultBuildTask}" + } + ] } diff --git a/.vscode/settings.json b/.vscode/settings.json index 392aaabe..d647cbee 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,7 +1,15 @@ // Place your settings in this file to overwrite default and user settings. { "typescript.tsdk": "./node_modules/typescript/lib", - "task.allowAutomaticTasks": "on", + "typescript.tsc.autoDetect": "off", "eslint.enable": true, - "prettier.enable": true + "prettier.enable": true, + "files.exclude": { + "out": false, + "dist": false + }, + "search.exclude": { + "out": true, + "dist": true + } } diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 8e4e115c..e1d88a0c 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -5,28 +5,17 @@ "tasks": [ { "type": "npm", - "label": "npm: watch", "script": "watch", - "group": { - "kind": "build", - "isDefault": true - }, "problemMatcher": ["$tsup-watch", "$tsup-tsc-watch"], "isBackground": true, "presentation": { "reveal": "never", "group": "watchers" }, - "runOptions": { - "runOn": "folderOpen" + "group": { + "kind": "build", + "isDefault": true } - }, - { - "type": "npm", - "label": "npm: build", - "script": "build", - "group": "build", - "problemMatcher": ["$tsup", "$tsup-tsc"] } ] } diff --git a/package.json b/package.json index 6ea6ba9b..225bed5a 100644 --- a/package.json +++ b/package.json @@ -39,15 +39,15 @@ "onStartupFinished" ], "scripts": { - "vscode:prepublish": "yarn build", - "build": "tsup", + "vscode:prepublish": "yarn run package", + "package": "tsup", "watch": "tsup --watch", "lint": "eslint src" }, "devDependencies": { "@types/git-url-parse": "^9.0.1", "@types/node": "16.x", - "@types/vscode": "^1.57.0", + "@types/vscode": "1.57.0", "@typescript-eslint/eslint-plugin": "^5.41.0", "@typescript-eslint/parser": "^5.41.0", "@xhayper/discord-rpc": "^1.0.13", @@ -55,6 +55,7 @@ "eslint-config-prettier": "^8.5.0", "filesize": "^10.0.5", "git-url-parse": "^13.1.0", + "prettier": "^2.7.1", "source-map-support": "^0.5.21", "tsup": "^6.3.0", "typescript": "^4.8.4" @@ -728,8 +729,5 @@ } ] }, - "packageManager": "yarn@3.2.4", - "dependencies": { - "prettier": "^2.7.1" - } + "packageManager": "yarn@3.2.4" } diff --git a/src/controller.ts b/src/controller.ts index 5d05b6a9..1c716382 100644 --- a/src/controller.ts +++ b/src/controller.ts @@ -18,7 +18,11 @@ export class RPCController { private idleTimeout: NodeJS.Timer | undefined; private iconTimeout: NodeJS.Timer | undefined; - private activityThrottle = throttle(() => void this.sendActivity(), 2000, true); + private activityThrottle = throttle( + (isViewing?: boolean, isIdling?: boolean) => this.sendActivity(isViewing, isIdling), + 2000, + true + ); constructor(clientId: string, debug = false) { this.client = new Client({ clientId }); @@ -91,11 +95,11 @@ export class RPCController { const fileSwitch = window.onDidChangeActiveTextEditor(() => sendActivity(true)); const fileEdit = workspace.onDidChangeTextDocument((e) => { if (e.document !== dataClass.editor?.document) return; - this.activityThrottle.callable(); + void this.activityThrottle.callable(); }); const fileSelectionChanged = window.onDidChangeTextEditorSelection((e) => { if (e.textEditor !== dataClass.editor) return; - this.activityThrottle.callable(); + void this.activityThrottle.callable(); }); const debugStart = debug.onDidStartDebugSession(() => sendActivity()); const debugEnd = debug.onDidTerminateDebugSession(() => sendActivity()); diff --git a/yarn.lock b/yarn.lock index 496704df..47ddf9c9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -44,13 +44,13 @@ __metadata: linkType: hard "@humanwhocodes/config-array@npm:^0.11.6": - version: 0.11.6 - resolution: "@humanwhocodes/config-array@npm:0.11.6" + version: 0.11.7 + resolution: "@humanwhocodes/config-array@npm:0.11.7" dependencies: "@humanwhocodes/object-schema": ^1.2.1 debug: ^4.1.1 - minimatch: ^3.0.4 - checksum: 2fb7288638968dfeec27f06aef52f043726edd126ac47f24b54256902fdb35b3bf9863d4a4caf0423dccca5dd1354ca5899f3ac047b56774641ca0c4cbedb104 + minimatch: ^3.0.5 + checksum: cf506dc45d9488af7fbf108ea6ac2151ba1a25e6d2b94b9b4fc36d2c1e4099b89ff560296dbfa13947e44604d4ca4a90d97a4fb167370bf8dd01a6ca2b6d83ac languageName: node linkType: hard @@ -150,10 +150,10 @@ __metadata: languageName: node linkType: hard -"@types/vscode@npm:^1.57.0": - version: 1.72.0 - resolution: "@types/vscode@npm:1.72.0" - checksum: 590ec21d01d737176f231fde1cafae31e9774891cc3c89cc7167167c0d88a91547c5a9f2103f4e778804f4f55724e9e1d2e3499448e8cea0d33786a1c728e4b6 +"@types/vscode@npm:1.57.0": + version: 1.57.0 + resolution: "@types/vscode@npm:1.57.0" + checksum: 1393f0c7302562638ddae31a305680935e50b17cfb922eff4f83aeb06e3cc6e196d92224e32781c9c78e5dc342cc9878da18d01066a76739b99fed4658bb2daf languageName: node linkType: hard @@ -695,9 +695,9 @@ __metadata: linkType: hard "discord-api-types@npm:^0.37.14": - version: 0.37.14 - resolution: "discord-api-types@npm:0.37.14" - checksum: 8f45f202e66acfd7b25624c8f4d225b363d9d8991d766959bcf246761548b99e21c12d9f7eafe00903913af66058595e5e56329dfb219eab8bb75a84f6413983 + version: 0.37.15 + resolution: "discord-api-types@npm:0.37.15" + checksum: c54d2feeb8074509bdda430fb8ec0f6ff315512e7327d47399e0e7a78bbd0a6f0f0dcfc4b5e39825eb6141a13f33efa942711af89c9a5936a721cfc1e1d69d19 languageName: node linkType: hard @@ -1810,7 +1810,7 @@ __metadata: languageName: node linkType: hard -"minimatch@npm:^3.0.4, minimatch@npm:^3.1.1, minimatch@npm:^3.1.2": +"minimatch@npm:^3.0.4, minimatch@npm:^3.0.5, minimatch@npm:^3.1.1, minimatch@npm:^3.1.2": version: 3.1.2 resolution: "minimatch@npm:3.1.2" dependencies: @@ -2705,7 +2705,7 @@ __metadata: dependencies: "@types/git-url-parse": ^9.0.1 "@types/node": 16.x - "@types/vscode": ^1.57.0 + "@types/vscode": 1.57.0 "@typescript-eslint/eslint-plugin": ^5.41.0 "@typescript-eslint/parser": ^5.41.0 "@xhayper/discord-rpc": ^1.0.13