Skip to content

Commit

Permalink
add eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
rebelvg committed Nov 24, 2020
1 parent da96f9a commit 4bb6dbe
Show file tree
Hide file tree
Showing 18 changed files with 905 additions and 167 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist-api/
dist-app/
55 changes: 55 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
module.exports = {
env: {
es6: true,
node: true,
},
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
extends: ['eslint:recommended'],
parserOptions: {
ecmaVersion: 2018,
},
globals: {
Electron: true,
},
rules: {
indent: ['off', 2],
quotes: [
'error',
'single',
{ avoidEscape: true, allowTemplateLiterals: true },
],
semi: ['error', 'always'],
'eol-last': ['error', 'always'],
'no-console': 0,
'no-var': 'error',
eqeqeq: ['error', 'always'],
curly: ['error', 'all'],
'no-multiple-empty-lines': ['error', { max: 1 }],
'no-unused-vars': [
'error',
{
args: 'none',
varsIgnorePattern: '^_',
},
],
'no-empty': ['error', { allowEmptyCatch: true }],
'prefer-arrow-callback': ['error'],
'require-await': 'error',
'no-case-declarations': 0,
'prefer-const': [
'error',
{
destructuring: 'all',
ignoreReadBeforeAssign: false,
},
],
'no-prototype-builtins': 'off',
'newline-after-var': ['error', 'always'],
'padding-line-between-statements': [
'error',
{ blankLine: 'always', prev: '*', next: 'return' },
],
'no-return-await': 'error',
},
};
1 change: 1 addition & 0 deletions api/Globals.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-unused-vars */
import * as fs from 'fs';
import * as path from 'path';

Expand Down
9 changes: 6 additions & 3 deletions api/Import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from './api-clients';
import { sleep } from './channel-check';

ipcMain.on('config_twitchImport', async (event, channelName) => {
ipcMain.on('config_twitchImport', (event, channelName) => {
return twitchImport(channelName);
});

Expand All @@ -34,7 +34,7 @@ async function twitchImportChannels(
}

for (const channel of userData.data) {
let channelObj = config.addChannelLink(
const channelObj = config.addChannelLink(
`https://twitch.tv/${channel.login}`,
false,
);
Expand Down Expand Up @@ -86,6 +86,7 @@ async function twitchImportBase(
userData.data.map(async ({ id }) => {
let cursor: string = '';

// eslint-disable-next-line no-constant-condition
while (true) {
const followedChannelsData = await twitchClient.getFollowedChannels(
id,
Expand All @@ -97,6 +98,7 @@ async function twitchImportBase(
}

const followedChannels = followedChannelsData.data;

cursor = followedChannelsData.pagination.cursor;

if (followedChannels.length === 0) {
Expand All @@ -122,7 +124,7 @@ async function twitchImportBase(
}

async function twitchImport(channelName: string) {
let res = await twitchImportBase(channelName, true);
const res = await twitchImportBase(channelName, true);

if (res !== null) {
dialog.showMessageBox({
Expand Down Expand Up @@ -153,6 +155,7 @@ export async function loop() {
await autoTwitchImport(false);

(async () => {
// eslint-disable-next-line no-constant-condition
while (true) {
await sleep(10 * 60 * 1000);

Expand Down
16 changes: 10 additions & 6 deletions api/Notifications.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { app, shell, Notification, nativeImage, NativeImage } from 'electron';
import { shell, Notification, nativeImage, NativeImage } from 'electron';
import * as _ from 'lodash';

import { config } from './settings-file';
Expand All @@ -7,7 +7,9 @@ import { iconPathBalloon } from './main';
import { Channel } from './channel-class';

export function printNotification(title, content, channelObj = null) {
if (!config.settings.showNotifications) return;
if (!config.settings.showNotifications) {
return;
}

printNewNotification(title, content, channelObj);
}
Expand All @@ -16,7 +18,7 @@ function printNewNotification(title, content, channelObj: Channel) {
let icon: string | NativeImage = iconPathBalloon;

if (channelObj) {
let iconBuffer = channelObj._icon
const iconBuffer = channelObj._icon
? channelObj._icon
: channelObj.serviceObj.icon;

Expand All @@ -25,13 +27,13 @@ function printNewNotification(title, content, channelObj: Channel) {
}
}

let notification = new Notification({
const notification = new Notification({
icon: icon,
title: title,
body: content,
});

notification.on('click', function (event) {
notification.on('click', (event) => {
onBalloonClick(title, content, channelObj);
});

Expand All @@ -41,7 +43,9 @@ function printNewNotification(title, content, channelObj: Channel) {
function onBalloonClick(title, content, channelObj) {
addLogs('balloon was clicked.');

if (!config.settings.launchOnBalloonClick) return;
if (!config.settings.launchOnBalloonClick) {
return;
}

if (title.indexOf('Stream is Live') === 0) {
channelObj.emit('play');
Expand Down
52 changes: 2 additions & 50 deletions api/api-clients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,68 +301,20 @@ class YoutubeClient {
return true;
}

public async getChannels(channelName: string): Promise<IYoutubeChannels> {
public getChannels(channelName: string): Promise<IYoutubeChannels> {
if (!config.settings.youtubeTosConsent) {
return;
}

return klpqServiceClient.getYoutubeChannels(channelName);

await this.refreshAccessToken();

if (!this.accessToken) {
return;
}

const channelsUrl = new URL(`${this.baseUrl}/channels`);

channelsUrl.searchParams.set('forUsername', channelName);
channelsUrl.searchParams.set('part', 'id');

try {
const { data } = await axios.get<IYoutubeChannels>(channelsUrl.href, {
headers: { Authorization: `Bearer ${this.accessToken}` },
});

return data;
} catch (error) {
this.handleError(error);

return;
}
}

public async getStreams(channelId: string): Promise<IYoutubeStreams> {
public getStreams(channelId: string): Promise<IYoutubeStreams> {
if (!config.settings.youtubeTosConsent) {
return;
}

return klpqServiceClient.getYoutubeStreams(channelId);

await this.refreshAccessToken();

if (!this.accessToken) {
return;
}

const searchUrl = new URL(`${this.baseUrl}/search`);

searchUrl.searchParams.set('channelId', channelId);
searchUrl.searchParams.set('part', 'snippet');
searchUrl.searchParams.set('type', 'video');
searchUrl.searchParams.set('eventType', 'live');

try {
const { data } = await axios.get<IYoutubeStreams>(searchUrl.href, {
headers: { Authorization: `Bearer ${this.accessToken}` },
});

return data;
} catch (error) {
this.handleError(error);

return;
}
}

private handleError(error: AxiosError): void {
Expand Down
22 changes: 14 additions & 8 deletions api/channel-check.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ipcMain, dialog } from 'electron';
import { dialog } from 'electron';
import * as _ from 'lodash';
import * as childProcess from 'child_process';

Expand Down Expand Up @@ -75,10 +75,12 @@ config.on('channel_added_channels', async (channels: Channel[]) => {
await checkChannels(channels, false);
});

async function isOnline(channelObj: Channel, printBalloon: boolean) {
function isOnline(channelObj: Channel, printBalloon: boolean) {
channelObj._offlineConfirmations = 0;

if (channelObj.isLive) return;
if (channelObj.isLive) {
return;
}

addLogs(`${channelObj.link} went online.`);

Expand Down Expand Up @@ -117,15 +119,18 @@ async function isOnline(channelObj: Channel, printBalloon: boolean) {
}

function isOffline(channelObj: Channel) {
if (!channelObj.isLive) return;
if (!channelObj.isLive) {
return;
}

channelObj._offlineConfirmations++;

if (
channelObj._offlineConfirmations <
_.get(SERVICES_INTERVALS, [channelObj.service, 'confirmations'], 0)
)
) {
return;
}

addLogs(`${channelObj.link} went offline.`);

Expand Down Expand Up @@ -313,12 +318,12 @@ async function getCustomStats(channels: Channel[], printBalloon: boolean) {

for (const channelObjs of chunkedChannels) {
await Promise.all(
channelObjs.map(async (channelObj) => {
channelObjs.map((channelObj) => {
return new Promise<void>((resolve) => {
childProcess.execFile(
'streamlink',
[channelObj.link, 'best', '--twitch-disable-hosting', '--json'],
function (err, stdout, stderr) {
(err, stdout, stderr) => {
try {
const res = JSON.parse(stdout);

Expand Down Expand Up @@ -368,14 +373,15 @@ async function checkServiceLoop(
service: IServiceInterval,
printBalloon: boolean,
) {
// eslint-disable-next-line no-constant-condition
while (true) {
await sleep(service.check * 1000);

await checkService(service, printBalloon);
}
}

export async function sleep(ms: number) {
export function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

Expand Down
16 changes: 12 additions & 4 deletions api/channel-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,26 +101,34 @@ export class Channel extends EventEmitter {
}

this.on('setting_changed', (settingName, settingValue, send) => {
if (send) app['mainWindow'].webContents.send('channel_changeSettingSync');
if (send) {
app['mainWindow'].webContents.send('channel_changeSettingSync');
}
});

this.on('settings_changed', (send) => {
if (send) app['mainWindow'].webContents.send('channel_changeSettingSync');
if (send) {
app['mainWindow'].webContents.send('channel_changeSettingSync');
}
});
}

public update(channelConfig) {
_.forEach(channelConfig, (settingValue, settingName) => {
if (channelValidate.includes(settingName)) {
if (settingName === 'visibleName' && !settingValue) return;
if (settingName === 'visibleName' && !settingValue) {
return;
}

this[settingName] = settingValue;
}
});
}

public changeSetting(settingName, settingValue, send = true) {
if (!this.hasOwnProperty(settingName)) return false;
if (!this.hasOwnProperty(settingName)) {
return false;
}

this[settingName] = settingValue;

Expand Down
2 changes: 0 additions & 2 deletions api/channel-info.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import * as _ from 'lodash';

import { Channel } from './channel-class';
import { addLogs } from './Logs';
import { twitchClient, commonClient, TWITCH_CHUNK_LIMIT } from './api-clients';
import { config } from './settings-file';
import { ipcMain } from 'electron';
import { ServiceNamesEnum } from './Globals';

interface IServiceInfo {
Expand Down
Loading

0 comments on commit 4bb6dbe

Please sign in to comment.