Skip to content

Commit

Permalink
Merge pull request #129 from leonardssh/chore/code-cleanup
Browse files Browse the repository at this point in the history
chore: code cleanup
  • Loading branch information
xhayper authored Oct 30, 2022
2 parents 54a5187 + 2d21522 commit 3a1c34b
Show file tree
Hide file tree
Showing 13 changed files with 173 additions and 163 deletions.
5 changes: 4 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@ module.exports = {
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking",
"prettier"
]
],
rules: {
"no-empty": "off"
}
};
28 changes: 15 additions & 13 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
}
]
}
12 changes: 10 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -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
}
}
17 changes: 3 additions & 14 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}
]
}
12 changes: 5 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,23 @@
"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",
"eslint": "^8.26.0",
"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"
Expand Down Expand Up @@ -728,8 +729,5 @@
}
]
},
"packageManager": "[email protected]",
"dependencies": {
"prettier": "^2.7.1"
}
"packageManager": "[email protected]"
}
195 changes: 104 additions & 91 deletions src/activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand All @@ -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),
Expand Down Expand Up @@ -97,79 +112,75 @@ 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;
let details = isWorkspaceExcluded ? workspaceExcludedText : undefined;
let state = undefined;

let largeImageKey = undefined;
let largeImageText = undefined;

let smallImageKey = undefined;
let smallImageText = undefined;

switch (status) {
case CURRENT_STATUS.IDLE: {
if (!isWorkspaceExcluded) {
if (detailsIdleEnabled && detailsEnabled)
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));
}

largeImageKey = await replaceAllText(config.get(CONFIG_KEYS.Status.Image.Large.Idle.Key));
largeImageText = await replaceAllText(config.get(CONFIG_KEYS.Status.Image.Large.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) 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));
largeImageText = await replaceAllText(config.get(CONFIG_KEYS.Status.Image.Large.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)
details = await replaceAllText(config.get(CONFIG_KEYS.Status.Details.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));
largeImageText = await replaceAllText(config.get(CONFIG_KEYS.Status.Image.Large.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) 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));
largeImageText = await replaceAllText(config.get(CONFIG_KEYS.Status.Image.Large.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 = details;
presence.state = state;
presence.largeImageKey = largeImageKey;
presence.largeImageText = largeImageText;
presence.smallImageKey = smallImageKey;
Expand Down Expand Up @@ -220,12 +231,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);
Expand All @@ -239,11 +250,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]
]);
Expand All @@ -262,11 +269,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";
Expand All @@ -280,6 +286,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],
Expand Down
4 changes: 2 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading

0 comments on commit 3a1c34b

Please sign in to comment.