Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up code and improve formatting #1878

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ pids
# Miscellaneous
.tmp/
!.vscode/launch.json

.vscode/setting.json
.idea
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"github_info.currentFollowers": 99
}
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"scripts": {
"build": "npm run lint && webpack --mode production",
"lint": "prettier --check . && eslint src --ext mjs,js,ts",
"format": "prettier --write . && eslint src --ext mjs,js,ts --fix"
"format": "prettier --write . && eslint src --ext mjs,js,ts --fix",
"watch": "webpack --watch"
},
"activationEvents": [
"*"
Expand Down Expand Up @@ -191,6 +192,7 @@
"utf-8-validate": "^5.0.9"
},
"devDependencies": {
"@types/discord-rpc": "^4.0.4",
"@types/lodash-es": "^4.17.6",
"@types/node": "^17.0.41",
"@types/vscode": "^1.67.0",
Expand All @@ -211,4 +213,4 @@
"engines": {
"vscode": "^1.63.1"
}
}
}
34 changes: 8 additions & 26 deletions src/activity.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,21 @@
import { basename, parse, sep } from 'path';
import { debug, env, Selection, TextDocument, window, workspace } from 'vscode';

import { log } from './logger';
import { getConfig, getGit, resolveFileIcon, toLower, toTitle, toUpper } from './util';
import type { ActivityPayload } from './interfaces/activityPayload.interface';
import { LogLevel } from './constants/logLevel.constant';
import {
CONFIG_KEYS,
DEBUG_IMAGE_KEY,
EMPTY,
FAKE_EMPTY,
FILE_SIZES,
IDLE_IMAGE_KEY,
REPLACE_KEYS,
UNKNOWN_GIT_BRANCH,
UNKNOWN_GIT_REPO_NAME,
VSCODE_IMAGE_KEY,
VSCODE_INSIDERS_IMAGE_KEY,
} from './constants';
import { log, LogLevel } from './logger';
import { getConfig, getGit, resolveFileIcon, toLower, toTitle, toUpper } from './util';

interface ActivityPayload {
details?: string | undefined;
state?: string | undefined;
startTimestamp?: number | null | undefined;
largeImageKey?: string | undefined;
largeImageText?: string | undefined;
smallImageKey?: string | undefined;
smallImageText?: string | undefined;
partyId?: string | undefined;
partySize?: number | undefined;
partyMax?: number | undefined;
matchSecret?: string | undefined;
joinSecret?: string | undefined;
spectateSecret?: string | undefined;
buttons?: { label: string; url: string }[] | undefined;
instance?: boolean | undefined;
}
} from './constants/keys.constant';
import { FILE_SIZES } from './constants/sizes.constant';
import { UNKNOWN_GIT_BRANCH, UNKNOWN_GIT_REPO_NAME } from './constants/git.constant';
import { EMPTY, FAKE_EMPTY } from './constants/empty.constant';

async function fileDetails(_raw: string, document: TextDocument, selection: Selection) {
let raw = _raw.slice();
Expand Down
61 changes: 0 additions & 61 deletions src/constants.ts

This file was deleted.

1 change: 1 addition & 0 deletions src/constants/client.constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const CLIENT_ID = '383226320970055681' as const;
5 changes: 5 additions & 0 deletions src/constants/commands.constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum CommandsId {
Enable = 'discord.enable',
Disable = 'discord.disable',
Reconnect = 'discord.reconnect',
}
2 changes: 2 additions & 0 deletions src/constants/empty.constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const EMPTY = '' as const;
export const FAKE_EMPTY = '\u200b\u200b' as const;
2 changes: 2 additions & 0 deletions src/constants/git.constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const UNKNOWN_GIT_BRANCH = 'Unknown' as const;
export const UNKNOWN_GIT_REPO_NAME = 'Unknown' as const;
48 changes: 48 additions & 0 deletions src/constants/keys.constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
export const enum REPLACE_KEYS {
Empty = '{empty}',
FileName = '{file_name}',
DirName = '{dir_name}',
FullDirName = '{full_dir_name}',
Workspace = '{workspace}',
VSCodeWorkspace = '(Workspace)',
WorkspaceFolder = '{workspace_folder}',
WorkspaceAndFolder = '{workspace_and_folder}',
LanguageLowerCase = '{lang}',
LanguageTitleCase = '{Lang}',
LanguageUpperCase = '{LANG}',
TotalLines = '{total_lines}',
CurrentLine = '{current_line}',
CurrentColumn = '{current_column}',
FileSize = '{file_size}',
AppName = '{app_name}',
GitRepoName = '{git_repo_name}',
GitBranch = '{git_branch}',
}

export const enum CONFIG_KEYS {
Enabled = 'enabled',
DetailsIdling = 'detailsIdling',
DetailsEditing = 'detailsEditing',
DetailsDebugging = 'detailsDebugging',
LowerDetailsIdling = 'lowerDetailsIdling',
LowerDetailsEditing = 'lowerDetailsEditing',
LowerDetailsDebugging = 'lowerDetailsDebugging',
LowerDetailsNoWorkspaceFound = 'lowerDetailsNoWorkspaceFound',
LargeImageIdling = 'largeImageIdling',
LargeImage = 'largeImage',
SmallImage = 'smallImage',
SuppressNotifications = 'suppressNotifications',
WorkspaceExcludePatterns = 'workspaceExcludePatterns',
SwapBigAndSmallImage = 'swapBigAndSmallImage',
RemoveDetails = 'removeDetails',
RemoveLowerDetails = 'removeLowerDetails',
RemoveTimestamp = 'removeTimestamp',
RemoveRemoteRepository = 'removeRemoteRepository',
IdleTimeout = 'idleTimeout',
}


export const IDLE_IMAGE_KEY = 'vscode-big' as const;
export const DEBUG_IMAGE_KEY = 'debug' as const;
export const VSCODE_IMAGE_KEY = 'vscode' as const;
export const VSCODE_INSIDERS_IMAGE_KEY = 'vscode-insiders' as const;
4 changes: 4 additions & 0 deletions src/constants/languages.constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import LANG from '../data/languages.json';

export const KNOWN_EXTENSIONS: { [key: string]: { image: string } } = LANG.KNOWN_EXTENSIONS;
export const KNOWN_LANGUAGES: { language: string; image: string }[] = LANG.KNOWN_LANGUAGES;
7 changes: 7 additions & 0 deletions src/constants/logLevel.constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const enum LogLevel {
Trace = 'TRACE',
Debug = 'DEBUG',
Info = 'INFO',
Warn = 'WARN',
Error = 'ERROR',
}
1 change: 1 addition & 0 deletions src/constants/sizes.constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const FILE_SIZES = [' bytes', 'KB', 'MB', 'GB', 'TB'] as const;
30 changes: 17 additions & 13 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access */

const { Client } = require('discord-rpc'); // eslint-disable-line
import { Client } from 'discord-rpc';
import { commands, ExtensionContext, StatusBarAlignment, StatusBarItem, window, workspace, debug } from 'vscode';
import throttle from 'lodash-es/throttle';

import { activity } from './activity';
import { CLIENT_ID, CONFIG_KEYS } from './constants';
import { log, LogLevel } from './logger';
import { log } from './logger';
import { getConfig, getGit } from './util';
import { LogLevel } from './constants/logLevel.constant';
import { CONFIG_KEYS } from './constants/keys.constant';
import { CLIENT_ID } from './constants/client.constant';
import type { ActivityPayload } from './interfaces/activityPayload.interface';
import { CommandsId } from './constants/commands.constant';

const statusBarIcon: StatusBarItem = window.createStatusBarItem(StatusBarAlignment.Left);
statusBarIcon.text = '$(pulse) Connecting to Discord...';
Expand All @@ -27,10 +31,11 @@ export function cleanUp() {
}

async function sendActivity() {
const activityPayload: ActivityPayload = await activity(state);
state = {
...(await activity(state)),
...activityPayload,
};
rpc.setActivity(state);
await rpc.setActivity(state);
}

async function login() {
Expand All @@ -57,22 +62,21 @@ async function login() {
cleanUp();
rpc.destroy();
statusBarIcon.text = '$(pulse) Reconnect to Discord';
statusBarIcon.command = 'discord.reconnect';
statusBarIcon.command = CommandsId.Reconnect;
});

try {
await rpc.login({ clientId: CLIENT_ID });
} catch (error) {
log(LogLevel.Error, `Encountered following error while trying to login:\n${error as string}`);
cleanUp();
rpc.destroy();
await rpc.destroy();
if (!config[CONFIG_KEYS.SuppressNotifications]) {
// @ts-expect-error
if (error?.message?.includes('ENOENT')) void window.showErrorMessage('No Discord client detected');
else void window.showErrorMessage(`Couldn't connect to Discord via RPC: ${error as string}`);
}
statusBarIcon.text = '$(pulse) Reconnect to Discord';
statusBarIcon.command = 'discord.reconnect';
statusBarIcon.command = CommandsId.Reconnect;
}
}

Expand Down Expand Up @@ -117,26 +121,26 @@ export async function activate(context: ExtensionContext) {
statusBarIcon.hide();
};

const enabler = commands.registerCommand('discord.enable', async () => {
const enabler = commands.registerCommand(CommandsId.Enable, async () => {
await disable();
await enable();
await window.showInformationMessage('Enabled Discord Presence for this workspace');
});

const disabler = commands.registerCommand('discord.disable', async () => {
const disabler = commands.registerCommand(CommandsId.Disable, async () => {
await disable();
await window.showInformationMessage('Disabled Discord Presence for this workspace');
});

const reconnecter = commands.registerCommand('discord.reconnect', async () => {
const reconnecter = commands.registerCommand(CommandsId.Reconnect, async () => {
await disable(false);
await enable(false);
});

const disconnect = commands.registerCommand('discord.disconnect', async () => {
await disable(false);
statusBarIcon.text = '$(pulse) Reconnect to Discord';
statusBarIcon.command = 'discord.reconnect';
statusBarIcon.command = CommandsId.Reconnect;
statusBarIcon.show();
});

Expand Down
17 changes: 17 additions & 0 deletions src/interfaces/activityPayload.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export interface ActivityPayload {
details?: string | undefined;
state?: string | undefined;
startTimestamp?: number | null | undefined;
largeImageKey?: string | undefined;
largeImageText?: string | undefined;
smallImageKey?: string | undefined;
smallImageText?: string | undefined;
partyId?: string | undefined;
partySize?: number | undefined;
partyMax?: number | undefined;
matchSecret?: string | undefined;
joinSecret?: string | undefined;
spectateSecret?: string | undefined;
buttons?: { label: string; url: string }[] | undefined;
instance?: boolean | undefined;
}
Loading