Skip to content

Commit

Permalink
refactor: fix lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Frewacom committed Aug 20, 2020
1 parent 97e2bc6 commit 1415280
Show file tree
Hide file tree
Showing 21 changed files with 326 additions and 246 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/artifacts
/daemon/
/dist
.eslintcache
14 changes: 10 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
"watch": "npx rollup -cw",
"browser": "web-ext run",
"web-ext-lint": "web-ext lint",
"eslint": "npx esw -w --color ./src/**/*.ts",
"eslint": "npx esw -w --cache --color --ext .ts",
"eslint-fix": "npx eslint --fix ./src/**/*.ts",
"debug": "run-p watch browser eslint",
"build": "run-s build:* web-ext-lint",
"debug": "run-p watch browser",
"build": "run-s build:* eslint web-ext-lint",
"build:compile": "npx rollup -c",
"build:dist": "web-ext build",
"build:source": "bestzip artifacts/source.zip extension src tsconfig.json package.json yarn.lock rollup.config.js LICENSE README.md"
Expand Down Expand Up @@ -71,7 +71,13 @@
},
"rules": {
"no-console": "off",
"@typescript-eslint/lines-between-class-members": "off"
"no-param-reassign": "off",
"no-prototype-builtins": "off",
"class-methods-use-this": "off",
"no-plusplus": [ "warn", { "allowForLoopAfterthoughts": true } ],
"@typescript-eslint/no-use-before-define": "off",
"@typescript-eslint/lines-between-class-members": "off",
"@typescript-eslint/no-unused-expressions": [ "warn", { "allowShortCircuit": true } ]
}
}
}
92 changes: 46 additions & 46 deletions src/background/auto-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,50 @@ import {
import { sendDebuggingOutput } from '@communication/ui';
import { AUTO_MODE_INTERVAL_MS } from '@config/general';

export class AutoMode {
export function minuteNumberToMs(minute: number, currentSecond: number) {
return Math.abs(minute * 60 * 1000 - (currentSecond * 1000));
}

export function checkIfDayTime(startTime: ITimeIntervalEndpoint, endTime: ITimeIntervalEndpoint) {
const currentDate = new Date();
const currentHour = currentDate.getHours();
const currentMinute = currentDate.getMinutes();
const currentSecond = currentDate.getSeconds();
let timeoutDelay = AUTO_MODE_INTERVAL_MS;
let result = false;

if (currentHour > startTime.hour && currentHour < endTime.hour) {
if (currentHour === (endTime.hour - 1) && endTime.minute <= AUTO_MODE_INTERVAL_MS) {
const difference = (60 + endTime.minute) - currentMinute;
if (difference < AUTO_MODE_INTERVAL_MS && difference !== 0) {
timeoutDelay = minuteNumberToMs(difference, currentSecond);
}
}
result = true;
} else if (currentHour === startTime.hour) {
if (currentMinute >= startTime.minute) {
result = true;
} else {
const difference = startTime.minute - currentMinute;
if (difference < AUTO_MODE_INTERVAL_MS) {
timeoutDelay = minuteNumberToMs(difference, currentSecond);
}
}
} else if (currentHour === endTime.hour && currentMinute < endTime.minute) {
const difference = endTime.minute - currentMinute;
if (difference < AUTO_MODE_INTERVAL_MS && difference !== 0) {
timeoutDelay = minuteNumberToMs(difference, currentSecond);
}
result = true;
}

return {
result,
timeoutDelay,
};
}

export default class AutoMode {
private startTime: ITimeIntervalEndpoint;
private endTime: ITimeIntervalEndpoint;
private callback: IAutoModeTriggerCallback;
Expand All @@ -22,49 +65,6 @@ export class AutoMode {
this.isDay = null;
}

private minuteNumberToMs(minute: number, currentSecond: number) {
return Math.abs(minute * 60 * 1000 - (currentSecond * 1000));
}

private checkIfDayTime(startTime: ITimeIntervalEndpoint, endTime: ITimeIntervalEndpoint) {
const currentDate = new Date();
const currentHour = currentDate.getHours();
const currentMinute = currentDate.getMinutes();
const currentSecond = currentDate.getSeconds();
let timeoutDelay = AUTO_MODE_INTERVAL_MS;
let result = false;

if (currentHour > startTime.hour && currentHour < endTime.hour) {
if (currentHour === (endTime.hour - 1) && endTime.minute <= AUTO_MODE_INTERVAL_MS) {
const difference = (60 + endTime.minute) - currentMinute;
if (difference < AUTO_MODE_INTERVAL_MS && difference !== 0) {
timeoutDelay = this.minuteNumberToMs(difference, currentSecond);
}
}
result = true;
} else if (currentHour === startTime.hour) {
if (currentMinute >= startTime.minute) {
result = true;
} else {
const difference = startTime.minute - currentMinute;
if (difference < AUTO_MODE_INTERVAL_MS) {
timeoutDelay = this.minuteNumberToMs(difference, currentSecond);
}
}
} else if (currentHour === endTime.hour && currentMinute < endTime.minute) {
const difference = endTime.minute - currentMinute;
if (difference < AUTO_MODE_INTERVAL_MS && difference !== 0) {
timeoutDelay = this.minuteNumberToMs(difference, currentSecond);
}
result = true;
}

return {
result,
timeoutDelay,
};
}

private deleteCurrentTimeout(printOutput = false) {
if (this.checkTimeout !== null) {
clearTimeout(this.checkTimeout);
Expand All @@ -75,13 +75,13 @@ export class AutoMode {
}

private update() {
const { result, timeoutDelay } = this.checkIfDayTime(this.startTime, this.endTime);
const { result, timeoutDelay } = checkIfDayTime(this.startTime, this.endTime);

if (result !== this.isDay) {
this.callback(result);
}

this.checkTimeout = window.setTimeout(this.update.bind(this), timeoutDelay);
this.checkTimeout = window.setTimeout(() => this.update.bind(this), timeoutDelay);
this.isDay = result;
}

Expand Down
31 changes: 18 additions & 13 deletions src/background/colorscheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import {
IDuckDuckGoThemeTemplateItem,
} from '@definitions';

import { EXTENSION_THEME_SELCTOR } from '@config/general';
import { EXTENDED_PYWAL_COLORS } from '@config/default-themes';
import { DUCKDUCKGO_THEME_ID, EXTENSION_THEME_SELCTOR } from '@config/general';
import { THEME_TEMPLATE_DATA, PALETTE_TEMPLATE_DATA } from '@config/template-data';

import { changeLuminance } from '@utils/colors';
Expand All @@ -22,7 +22,7 @@ import { changeLuminance } from '@utils/colors';
export function extendPywalColors(pywalColors: IPywalColors) {
const colors = pywalColors;

for (const color of EXTENDED_PYWAL_COLORS) {
EXTENDED_PYWAL_COLORS.forEach((color) => {
const {
targetIndex, colorString, colorIndex, modifier, min, max,
} = color;
Expand All @@ -37,7 +37,7 @@ export function extendPywalColors(pywalColors: IPywalColors) {
} else {
console.warn(`Invalid extended pywal color. Missing required properties for targetIndex: ${targetIndex}`);
}
}
});

return colors;
}
Expand All @@ -47,10 +47,14 @@ export function generateColorscheme(
customColors: Partial<IPalette>,
template: IColorschemeTemplate,
) {
const originalPalette = createObjectFromTemplateData<IPalette>(PALETTE_TEMPLATE_DATA, pywalPalette, template.palette);
const originalPalette = createObjectFromTemplateData<IPalette>(
PALETTE_TEMPLATE_DATA,
pywalPalette,
template.palette,
);

// Override the templated palette with any custom colors set by the user
const palette = Object.assign(originalPalette, customColors);
const palette = { ...originalPalette, ...customColors };

return {
hash: createPaletteHash(palette),
Expand All @@ -68,7 +72,7 @@ export function generateBrowserTheme(palette: IPalette, template: IThemeTemplate
export function generateDDGTheme(palette: IPalette, template: IDuckDuckGoThemeTemplate) {
const theme = <IDuckDuckGoTheme>{};

for (const key of Object.keys(template)) {
Object.keys(template).forEach((key) => {
const item: IDuckDuckGoThemeTemplateItem = template[key];
let color: string = palette[item.colorKey];

Expand All @@ -77,17 +81,17 @@ export function generateDDGTheme(palette: IPalette, template: IDuckDuckGoThemeTe
}

theme[key] = stripHashSymbol(color);
}
});

return theme;
}

export function generateExtensionTheme(palette: IPalette) {
let variables: string = '';

for (const { target, cssVariable } of PALETTE_TEMPLATE_DATA) {
PALETTE_TEMPLATE_DATA.forEach(({ target, cssVariable }) => {
variables += `${cssVariable}:${palette[target]};`;
}
});

return `${EXTENSION_THEME_SELCTOR}{${variables}}`;
}
Expand All @@ -103,7 +107,7 @@ function createObjectFromTemplateData<T>(
template: (IPaletteTemplate | IThemeTemplate),
) {
return data.reduce((obj: T, item: ITemplateItem) => {
obj[<keyof T>item.target] = values[template[item.target]];
obj[<keyof T>item.target] = values[template[item.target]]; // eslint-disable-line
return obj;
}, <T>{});
}
Expand All @@ -118,12 +122,13 @@ function stripHashSymbol(color: string) {
*/
function createPaletteHash(palette: IPalette) {
const colors = Object.keys(palette);
let hash: string = '';

colors.sort((a: string, b: string) => ((a > b) ? 1 : -1));

let hash: string = '';
for (const key of colors) {
colors.forEach((key: string) => {
hash += stripHashSymbol(palette[<PaletteColors>key]);
}
});

return hash;
}
10 changes: 5 additions & 5 deletions src/background/extension-page.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IExtensionTheme } from '@definitions';

export class ExtensionPage {
export default class ExtensionPage {
protected tab: browser.tabs.Tab;
protected currentTheme: IExtensionTheme;
protected url: string;
Expand All @@ -13,9 +13,9 @@ export class ExtensionPage {
}

private async deleteDetached(tabs: browser.tabs.Tab[]) {
for (const tab of tabs) {
tabs.forEach(async (tab) => {
await browser.tabs.remove(tab.id);
}
});
}

private attach(tab: browser.tabs.Tab) {
Expand All @@ -41,7 +41,7 @@ export class ExtensionPage {
}
}

private onClosed(tabId: number, removeInfo?: any) {
private onClosed(tabId: number) {
if (this.tab !== null) {
if (tabId === this.tab.id) {
browser.tabs.onRemoved.removeListener(this.onClosed);
Expand All @@ -51,7 +51,7 @@ export class ExtensionPage {
}
}

private onUpdated(tabId: number, changeInfo: any, tab?: any) {
private onUpdated(tabId: number, changeInfo: any) {
if (changeInfo.status === 'loading') {
// The 'url' attribute is only available on loading
if (changeInfo.url === this.url || changeInfo.url === undefined) {
Expand Down
27 changes: 17 additions & 10 deletions src/background/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ import {
generateBrowserTheme,
} from './colorscheme';

import { State } from './state';
import { NativeApp } from './native-app';
import { AutoMode } from './auto-mode';
import { ExtensionPage } from './extension-page';
import State from './state';
import NativeApp from './native-app';
import AutoMode from './auto-mode';
import ExtensionPage from './extension-page';

export class Extension {
export default class Extension {
private state: State;
private nativeApp: NativeApp;
private settingsPage: ExtensionPage;
Expand Down Expand Up @@ -76,7 +76,7 @@ export class Extension {

if (!themeMode) {
console.error('Failed to get default template: theme mode is not set');
return;
return null;
}

return themeMode === ThemeModes.Dark ? DEFAULT_THEME_DARK : DEFAULT_THEME_LIGHT;
Expand Down Expand Up @@ -180,6 +180,8 @@ export class Extension {
this.state.setUpdateMuted(true);
this.updatePage.close();
break;
default:
break;
}
}

Expand Down Expand Up @@ -236,15 +238,20 @@ export class Extension {

private applyUpdatedPaletteTemplate(template: IPaletteTemplate) {
const pywalColors = this.state.getPywalColors();
const customColors = this.state.getCustomColors();

if (!pywalColors) {
return;
}

const customColors = this.state.getCustomColors();
const filteredCustomColors = customColors.filter((color) => {
pywalColors[template[color]] !== customColors[color];
});
// Make sure that a color from the pywal palette is not used as a custom color.
let filteredCustomColors = customColors;
if (customColors !== null) {
filteredCustomColors = <Partial<IPalette>>Object.keys(customColors).filter((key) => {
const pywalColor = pywalColors[template[key]];
return pywalColor !== customColors[key];
});
}

this.setThemes(pywalColors, filteredCustomColors);
UI.sendCustomColors(filteredCustomColors);
Expand Down
2 changes: 1 addition & 1 deletion src/background/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import '../ui/css/base.css';
import { Extension } from './extension';
import Extension from './extension';

const extension = new Extension();
extension.start();
7 changes: 4 additions & 3 deletions src/background/native-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { RESPONSE_TIMEOUT, NATIVE_MESSAGES } from '@config/native';
* user's computer and share resources that are otherwise inaccessible by the browser.
* https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Native_messaging
*/
export class NativeApp {
export default class NativeApp {
private isConnected: boolean;
private port: browser.runtime.Port;
private callbacks: INativeAppMessageCallbacks;
Expand Down Expand Up @@ -118,7 +118,7 @@ export class NativeApp {
return;
}

this.callbacks.cssFontSizeSetSuccess(parseInt(updatedFontSize));
this.callbacks.cssFontSizeSetSuccess(parseInt(updatedFontSize, 10));
} else {
this.callbacks.cssFontSizeSetFailed(message.error);
}
Expand Down Expand Up @@ -165,7 +165,8 @@ export class NativeApp {
}

public requestCssEnabled(target: string, enabled: boolean) {
this.sendMessage({ action: enabled ? NATIVE_MESSAGES.CSS_ENABLE : NATIVE_MESSAGES.CSS_DISABLE, target });
const action = enabled ? NATIVE_MESSAGES.CSS_ENABLE : NATIVE_MESSAGES.CSS_DISABLE;
this.sendMessage({ action, target });
}

public requestFontSizeSet(target: string, size: number) {
Expand Down
Loading

0 comments on commit 1415280

Please sign in to comment.