chore: lint

This commit is contained in:
iCrawl 2019-09-13 13:18:12 +02:00
parent 0bd7e3430d
commit 85f29ad073
No known key found for this signature in database
GPG key ID: E41A6DB922EC2CFE
4 changed files with 39 additions and 57 deletions

View file

@ -1,13 +1,7 @@
const { Client } = require('discord-rpc'); // eslint-disable-line const { Client } = require('discord-rpc'); // eslint-disable-line
import { import { Disposable, StatusBarItem, Uri, window, workspace } from 'vscode';
Disposable,
StatusBarItem,
workspace,
Uri,
window
} from 'vscode';
import * as vsls from 'vsls'; import * as vsls from 'vsls';
import Activity, { State } from '../structures/Activity'; import Activity from '../structures/Activity';
import Logger from '../structures/Logger'; import Logger from '../structures/Logger';
const clipboardy = require('clipboardy'); // eslint-disable-line const clipboardy = require('clipboardy'); // eslint-disable-line
@ -29,44 +23,44 @@ export default class RPCClient implements Disposable {
this.statusBarIcon = statusBarIcon; this.statusBarIcon = statusBarIcon;
} }
public get client(): any { public get client() {
return this._rpc; return this._rpc;
} }
public async setActivity(workspaceElapsedTime: boolean = false): Promise<void> { public async setActivity(workspaceElapsedTime = false) {
if (!this._rpc) return; if (!this._rpc) return;
const activity = await this._activity.generate(workspaceElapsedTime); const activity = await this._activity.generate(workspaceElapsedTime);
Logger.log('Sending activity to Discord.'); Logger.log('Sending activity to Discord.');
this._rpc.setActivity(activity); this._rpc.setActivity(activity);
} }
public async allowSpectate(): Promise<void> { public async allowSpectate() {
if (!this._rpc) return; if (!this._rpc) return;
Logger.log('Allowed spectating.'); Logger.log('Allowed spectating.');
Logger.log('Sending spectate activity to Discord.'); Logger.log('Sending spectate activity to Discord.');
await this._activity.allowSpectate(); await this._activity.allowSpectate();
} }
public async disableSpectate(): Promise<void> { public async disableSpectate() {
if (!this._rpc) return; if (!this._rpc) return;
Logger.log('Disabled spectating.'); Logger.log('Disabled spectating.');
await this._activity.disableSpectate(); await this._activity.disableSpectate();
} }
public async allowJoinRequests(): Promise<void> { public async allowJoinRequests() {
if (!this._rpc) return; if (!this._rpc) return;
Logger.log('Allowed join requests.'); Logger.log('Allowed join requests.');
Logger.log('Sending join activity to Discord.'); Logger.log('Sending join activity to Discord.');
await this._activity.allowJoinRequests(); await this._activity.allowJoinRequests();
} }
public async disableJoinRequests(): Promise<void> { public async disableJoinRequests() {
if (!this._rpc) return; if (!this._rpc) return;
Logger.log('Disabled join requests.'); Logger.log('Disabled join requests.');
await this._activity.disableJoinRequests(); await this._activity.disableJoinRequests();
} }
public async login(): Promise<void> { public async login() {
if (this._rpc) return; if (this._rpc) return;
this._rpc = new Client({ transport: 'ipc' }); this._rpc = new Client({ transport: 'ipc' });
Logger.log('Logging into RPC.'); Logger.log('Logging into RPC.');
@ -134,11 +128,11 @@ export default class RPCClient implements Disposable {
const liveshare = await vsls.getApi(); const liveshare = await vsls.getApi();
if (!liveshare) return; if (!liveshare) return;
liveshare.onDidChangeSession(({ session }: { session: vsls.Session }): State | void => { liveshare.onDidChangeSession(({ session }) => {
if (session.id) return this._activity.changePartyId(session.id); if (session.id) return this._activity.changePartyId(session.id);
return this._activity.changePartyId(); return this._activity.changePartyId();
}); });
liveshare.onDidChangePeers(({ added, removed }: { added: vsls.Peer[]; removed: vsls.Peer[] }): State | void => { liveshare.onDidChangePeers(({ added, removed }) => {
if (added.length) return this._activity.increasePartySize(added.length); if (added.length) return this._activity.increasePartySize(added.length);
else if (removed.length) return this._activity.decreasePartySize(removed.length); else if (removed.length) return this._activity.decreasePartySize(removed.length);
}); });
@ -155,7 +149,7 @@ export default class RPCClient implements Disposable {
await this._rpc.login({ clientId: this._clientId }); await this._rpc.login({ clientId: this._clientId });
} }
public async dispose(): Promise<void> { public async dispose() {
this._activity.dispose(); this._activity.dispose();
try { try {
if (this._rpc) await this._rpc.destroy(); if (this._rpc) await this._rpc.destroy();

View file

@ -1,11 +1,4 @@
import { import { commands, ExtensionContext, StatusBarAlignment, StatusBarItem, window, workspace } from 'vscode';
commands,
ExtensionContext,
StatusBarAlignment,
StatusBarItem,
window,
workspace
} from 'vscode';
import RPCClient from './client/RPCClient'; import RPCClient from './client/RPCClient';
import Logger from './structures/Logger'; import Logger from './structures/Logger';
const { register } = require('discord-rpc'); // eslint-disable-line const { register } = require('discord-rpc'); // eslint-disable-line
@ -19,7 +12,7 @@ const config = workspace.getConfiguration('discord');
register(config.get<string>('clientID')); register(config.get<string>('clientID'));
const rpc = new RPCClient(config.get<string>('clientID')!, statusBarIcon); const rpc = new RPCClient(config.get<string>('clientID')!, statusBarIcon);
export async function activate(context: ExtensionContext): Promise<void> { export async function activate(context: ExtensionContext) {
Logger.log('Discord Presence activated!'); Logger.log('Discord Presence activated!');
let isWorkspaceExcluded = false; let isWorkspaceExcluded = false;
@ -29,7 +22,7 @@ export async function activate(context: ExtensionContext): Promise<void> {
const regex = new RegExp(pattern); const regex = new RegExp(pattern);
const folders = workspace.workspaceFolders; const folders = workspace.workspaceFolders;
if (!folders) break; if (!folders) break;
if (folders.some((folder): boolean => regex.test(folder.uri.fsPath))) { if (folders.some(folder => regex.test(folder.uri.fsPath))) {
isWorkspaceExcluded = true; isWorkspaceExcluded = true;
break; break;
} }
@ -106,8 +99,8 @@ export async function activate(context: ExtensionContext): Promise<void> {
context.subscriptions.push(enabler, disabler, reconnecter, disconnect, allowSpectate, disableSpectate, allowJoinRequests, disableJoinRequests); context.subscriptions.push(enabler, disabler, reconnecter, disconnect, allowSpectate, disableSpectate, allowJoinRequests, disableJoinRequests);
} }
export async function deactivate(): Promise<void> { export async function deactivate() {
await rpc.dispose(); await rpc.dispose();
} }
process.on('unhandledRejection', (err): void => Logger.log(err as string)); process.on('unhandledRejection', err => Logger.log(err as string));

View file

@ -1,13 +1,8 @@
import { basename, parse, sep } from 'path'; import { basename, parse, sep } from 'path';
import { import { debug, Disposable, env, window, workspace } from 'vscode';
debug,
Disposable,
env,
window,
workspace
} from 'vscode';
import * as vsls from 'vsls'; import * as vsls from 'vsls';
const lang = require('../data/languages.json'); // eslint-disable-line const lang = require('../data/languages.json'); // eslint-disable-line
const knownExtentions: { [key: string]: { image: string } } = lang.knownExtentions; const knownExtentions: { [key: string]: { image: string } } = lang.knownExtentions;
const knownLanguages: string[] = lang.knownLanguages; const knownLanguages: string[] = lang.knownLanguages;
@ -43,13 +38,13 @@ export default class Activity implements Disposable {
private readonly _config = workspace.getConfiguration('discord'); private readonly _config = workspace.getConfiguration('discord');
private _lastKnownFile: string = ''; private _lastKnownFile = '';
public get state(): State | null { public get state() {
return this._state; return this._state;
} }
public async generate(workspaceElapsedTime: boolean = false): Promise<State | null> { public async generate(workspaceElapsedTime = false) {
let largeImageKey: any = 'vscode-big'; let largeImageKey: any = 'vscode-big';
if (window.activeTextEditor) { if (window.activeTextEditor) {
if (window.activeTextEditor.document.languageId === 'Log') return this._state; if (window.activeTextEditor.document.languageId === 'Log') return this._state;
@ -63,7 +58,7 @@ export default class Activity implements Disposable {
} }
this._lastKnownFile = window.activeTextEditor.document.fileName; this._lastKnownFile = window.activeTextEditor.document.fileName;
const filename = basename(window.activeTextEditor.document.fileName); const filename = basename(window.activeTextEditor.document.fileName);
largeImageKey = knownExtentions[Object.keys(knownExtentions).find((key): boolean => { largeImageKey = knownExtentions[Object.keys(knownExtentions).find(key => {
if (filename.endsWith(key)) return true; if (filename.endsWith(key)) return true;
const match = key.match(/^\/(.*)\/([mgiy]+)$/); const match = key.match(/^\/(.*)\/([mgiy]+)$/);
if (!match) return false; if (!match) return false;
@ -84,7 +79,7 @@ export default class Activity implements Disposable {
largeImageText: window.activeTextEditor largeImageText: window.activeTextEditor
? this._config.get<string>('largeImage')! ? this._config.get<string>('largeImage')!
.replace('{lang}', largeImageKey ? largeImageKey.image || largeImageKey : 'txt') .replace('{lang}', largeImageKey ? largeImageKey.image || largeImageKey : 'txt')
.replace('{Lang}', largeImageKey ? (largeImageKey.image || largeImageKey).toLowerCase().replace(/^\w/, (c: string): string => c.toUpperCase()) : 'Txt') .replace('{Lang}', largeImageKey ? (largeImageKey.image || largeImageKey).toLowerCase().replace(/^\w/, (c: string) => c.toUpperCase()) : 'Txt')
.replace('{LANG}', largeImageKey ? (largeImageKey.image || largeImageKey).toUpperCase() : 'TXT') || .replace('{LANG}', largeImageKey ? (largeImageKey.image || largeImageKey).toUpperCase() : 'TXT') ||
window.activeTextEditor.document.languageId.padEnd(2, '\u200b') window.activeTextEditor.document.languageId.padEnd(2, '\u200b')
: this._config.get<string>('largeImageIdle'), : this._config.get<string>('largeImageIdle'),
@ -95,7 +90,7 @@ export default class Activity implements Disposable {
return this._state; return this._state;
} }
public async allowSpectate(): Promise<State | void> { public async allowSpectate() {
if (!this._state) return; if (!this._state) return;
const liveshare = await vsls.getApi(); const liveshare = await vsls.getApi();
if (!liveshare) return; if (!liveshare) return;
@ -109,7 +104,7 @@ export default class Activity implements Disposable {
return this._state; return this._state;
} }
public async disableSpectate(): Promise<State | void> { public async disableSpectate() {
if (!this._state) return; if (!this._state) return;
const liveshare = await vsls.getApi(); const liveshare = await vsls.getApi();
if (!liveshare) return; if (!liveshare) return;
@ -121,7 +116,7 @@ export default class Activity implements Disposable {
return this._state; return this._state;
} }
public async allowJoinRequests(): Promise<State | void> { public async allowJoinRequests() {
if (!this._state) return; if (!this._state) return;
const liveshare = await vsls.getApi(); const liveshare = await vsls.getApi();
if (!liveshare) return; if (!liveshare) return;
@ -138,7 +133,7 @@ export default class Activity implements Disposable {
return this._state; return this._state;
} }
public async disableJoinRequests(): Promise<State | void> { public async disableJoinRequests() {
if (!this._state) return; if (!this._state) return;
const liveshare = await vsls.getApi(); const liveshare = await vsls.getApi();
if (!liveshare) return; if (!liveshare) return;
@ -153,7 +148,7 @@ export default class Activity implements Disposable {
return this._state; return this._state;
} }
public changePartyId(id?: string): State | void { public changePartyId(id?: string) {
if (!this._state) return; if (!this._state) return;
if (!id) { if (!id) {
delete this._state.partyId; delete this._state.partyId;
@ -174,7 +169,7 @@ export default class Activity implements Disposable {
return this._state; return this._state;
} }
public increasePartySize(size?: number): State | void { public increasePartySize(size?: number) {
if (!this._state) return; if (!this._state) return;
if (this.state && this._state.partySize === 5) return; if (this.state && this._state.partySize === 5) return;
this._state = { this._state = {
@ -185,7 +180,7 @@ export default class Activity implements Disposable {
return this._state; return this._state;
} }
public decreasePartySize(size?: number): State | void { public decreasePartySize(size?: number) {
if (!this._state) return; if (!this._state) return;
if (this.state && this._state.partySize === 1) return; if (this.state && this._state.partySize === 1) return;
this._state = { this._state = {
@ -196,12 +191,12 @@ export default class Activity implements Disposable {
return this._state; return this._state;
} }
public dispose(): void { public dispose() {
this._state = null; this._state = null;
this._lastKnownFile = ''; this._lastKnownFile = '';
} }
private async _generateDetails(debugging: string, editing: string, idling: string, largeImageKey: any): Promise<string> { private async _generateDetails(debugging: string, editing: string, idling: string, largeImageKey: any) {
let raw: string = this._config.get<string>(idling)!.replace('{null}', empty); let raw: string = this._config.get<string>(idling)!.replace('{null}', empty);
let filename = null; let filename = null;
let dirname = null; let dirname = null;
@ -240,7 +235,7 @@ export default class Activity implements Disposable {
.replace('{fulldirname}', fullDirname!) .replace('{fulldirname}', fullDirname!)
.replace('{workspace}', checkState && workspaceFolder ? workspaceFolder.name : this._config.get<string>('lowerDetailsNotFound')!.replace('{null}', empty)) .replace('{workspace}', checkState && workspaceFolder ? workspaceFolder.name : this._config.get<string>('lowerDetailsNotFound')!.replace('{null}', empty))
.replace('{lang}', largeImageKey ? largeImageKey.image || largeImageKey : 'txt') .replace('{lang}', largeImageKey ? largeImageKey.image || largeImageKey : 'txt')
.replace('{Lang}', largeImageKey ? (largeImageKey.image || largeImageKey).toLowerCase().replace(/^\w/, (c: string): string => c.toUpperCase()) : 'Txt') .replace('{Lang}', largeImageKey ? (largeImageKey.image || largeImageKey).toLowerCase().replace(/^\w/, (c: string) => c.toUpperCase()) : 'Txt')
.replace('{LANG}', largeImageKey ? (largeImageKey.image || largeImageKey).toUpperCase() : 'TXT'); .replace('{LANG}', largeImageKey ? (largeImageKey.image || largeImageKey).toUpperCase() : 'TXT');
if (totalLines) raw = raw!.replace('{totallines}', totalLines); if (totalLines) raw = raw!.replace('{totallines}', totalLines);
if (size) raw = raw!.replace('{filesize}', size); if (size) raw = raw!.replace('{filesize}', size);
@ -251,7 +246,7 @@ export default class Activity implements Disposable {
return raw; return raw;
} }
private async _generateFileDetails(str?: string): Promise<FileDetail> { private async _generateFileDetails(str?: string) {
const fileDetail: FileDetail = {}; const fileDetail: FileDetail = {};
if (!str) return fileDetail; if (!str) return fileDetail;
@ -261,11 +256,11 @@ export default class Activity implements Disposable {
} }
if (str.includes('{currentline}')) { if (str.includes('{currentline}')) {
fileDetail.currentLine = (window.activeTextEditor.selection.active.line as number + 1).toLocaleString(); fileDetail.currentLine = (window.activeTextEditor.selection.active.line + 1).toLocaleString();
} }
if (str.includes('{currentcolumn}')) { if (str.includes('{currentcolumn}')) {
fileDetail.currentColumn = (window.activeTextEditor.selection.active.character as number + 1).toLocaleString(); fileDetail.currentColumn = (window.activeTextEditor.selection.active.character + 1).toLocaleString();
} }
if (str.includes('{filesize}')) { if (str.includes('{filesize}')) {

View file

@ -4,11 +4,11 @@ import { OutputChannel, window } from 'vscode';
export default class Logger { export default class Logger {
private static _output: OutputChannel; private static _output: OutputChannel;
private static _setup(): void { private static _setup() {
this._output = this._output || window.createOutputChannel('Discord Presence'); this._output = this._output || window.createOutputChannel('Discord Presence');
} }
public static log(message: string): void { public static log(message: string) {
if (!this._output) this._setup(); if (!this._output) this._setup();
this._output.appendLine(message); this._output.appendLine(message);
} }