Skip to content

Commit

Permalink
chore: enable typescript eslint rules
Browse files Browse the repository at this point in the history
- refactor code to satisfy rules (batch 1)
- use ensuredir in build to prevent ENOENT
  • Loading branch information
18alantom committed Jun 21, 2023
1 parent 8a1392e commit 4415c04
Show file tree
Hide file tree
Showing 41 changed files with 239 additions and 182 deletions.
31 changes: 20 additions & 11 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
module.exports = {
root: true,

env: {
node: true,
browser: true,
es2020: true,
},

rules: {
'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off',
'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
'no-console': 'warn',
'no-debugger': 'warn',
'arrow-body-style': 'off',
'prefer-arrow-callback': 'off',
'prefer-arrow-callback': 'warn',
'vue/no-mutating-props': 'off',
'vue/multi-word-component-names': 'off',
'vue/no-useless-template-attributes': 'off',
'vue/one-component-per-file': 'off',
'@typescript-eslint/ban-ts-comment': 'off',
'@typescript-eslint/no-var-requires': 'off',
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-floating-promises': 'off',
'@typescript-eslint/no-misused-promises': 'off',
},

parserOptions: {
parser: '@typescript-eslint/parser',
},

extends: ['plugin:vue/vue3-essential', '@vue/typescript'],
parser: '@typescript-eslint/parser',
parserOptions: { project: true, tsconfigRootDir: __dirname },
plugins: ['@typescript-eslint'],
extends: [
'plugin:vue/vue3-strongly-recommended',
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/recommended-requiring-type-checking',
],
ignorePatterns: ['.eslintrc.js'],
};
4 changes: 2 additions & 2 deletions build/scripts/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ await packageApp();

function updatePaths() {
fs.removeSync(buildDirPath);
fs.mkdirSync(buildDirPath);
fs.ensureDirSync(buildDirPath);
fs.removeSync(packageDirPath);
fs.mkdirSync(packageDirPath);
fs.ensureDirSync(packageDirPath);
fs.ensureDirSync(path.join(buildDirPath, 'node_modules'));
}

Expand Down
7 changes: 4 additions & 3 deletions fyo/core/authHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ export class AuthHandler {
return { ...this.#config };
}

init() {}
init() {
return null;
}

async login(email: string, password: string) {
if (email === 'Administrator') {
this.#session.user = 'Administrator';
Expand Down Expand Up @@ -107,8 +110,6 @@ export class AuthHandler {
// TODO: Implement this with auth flow
}

async purgeCache() {}

#getServerURL() {
return this.#config.serverURL || '';
}
Expand Down
8 changes: 4 additions & 4 deletions fyo/core/docHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class DocHandler {
this.observer = new Observable();
}

async purgeCache() {
purgeCache() {
this.init();
}

Expand Down Expand Up @@ -82,10 +82,10 @@ export class DocHandler {
getNewDoc(
schemaName: string,
data: DocValueMap | RawValueMap = {},
cacheDoc: boolean = true,
cacheDoc = true,
schema?: Schema,
Model?: typeof Doc,
isRawValueMap: boolean = true
isRawValueMap = true
): Doc {
if (!this.models[schemaName] && Model) {
this.models[schemaName] = Model;
Expand Down Expand Up @@ -153,7 +153,7 @@ export class DocHandler {

// propagate change to `docs`
doc.on('change', (params: unknown) => {
this.docs!.trigger('change', params);
this.docs.trigger('change', params);
});

doc.on('afterSync', () => {
Expand Down
22 changes: 11 additions & 11 deletions fyo/core/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Doc } from 'fyo/model/doc';
import { Money } from 'pesa';
import { RawValue } from 'schemas/types';
import { AuthDemuxBase } from 'utils/auth/types';
import { DatabaseDemuxBase } from 'utils/db/types';
import type { Doc } from 'fyo/model/doc';
import type { Money } from 'pesa';
import type { RawValue } from 'schemas/types';
import type { AuthDemuxBase } from 'utils/auth/types';
import type { DatabaseDemuxBase } from 'utils/db/types';

export type Attachment = { name: string; type: string; data: string };
export type DocValue =
Expand Down Expand Up @@ -31,12 +31,12 @@ export type DatabaseDemuxConstructor = new (

export type AuthDemuxConstructor = new (isElectron?: boolean) => AuthDemuxBase;

export enum ConfigKeys {
Files = 'files',
LastSelectedFilePath = 'lastSelectedFilePath',
Language = 'language',
DeviceId = 'deviceId',
}
export type ConfigMap = {
files: ConfigFile[];
lastSelectedFilePath: null | string;
language: string
deviceId: string
};

export interface ConfigFile {
id: string;
Expand Down
15 changes: 10 additions & 5 deletions fyo/demux/config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type Store from 'electron-store';
import { ConfigMap } from 'fyo/core/types';

export class Config {
config: Map<string, unknown> | Store;
constructor(isElectron: boolean) {
this.config = new Map();
if (isElectron) {
const Config: typeof Store = require('electron-store');
const Config = require('electron-store') as typeof Store;
this.config = new Config();
}
}
Expand All @@ -23,15 +24,19 @@ export class Config {
}
}

get(key: string, defaultValue?: unknown): unknown {
return this.config.get(key) ?? defaultValue;
get<K extends keyof ConfigMap>(
key: K,
defaultValue?: ConfigMap[K]
): ConfigMap[K] | undefined {
const value = this.config.get(key) as ConfigMap[K] | undefined;
return value ?? defaultValue;
}

set(key: string, value: unknown) {
set<K extends keyof ConfigMap>(key: K, value: ConfigMap[K]) {
this.config.set(key, value);
}

delete(key: string) {
delete(key: keyof ConfigMap) {
this.config.delete(key);
}

Expand Down
17 changes: 8 additions & 9 deletions fyo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class Fyo {
doc: DocHandler;
db: DatabaseHandler;

_initialized: boolean = false;
_initialized = false;

errorLog: ErrorLog[] = [];
temp?: Record<string, unknown>;
Expand Down Expand Up @@ -94,7 +94,7 @@ export class Fyo {
return format(value, field, doc ?? null, this);
}

async setIsElectron() {
setIsElectron() {
try {
this.isElectron = Boolean(require('electron'));
} catch {
Expand All @@ -105,7 +105,7 @@ export class Fyo {
async initializeAndRegister(
models: ModelMap = {},
regionalModels: ModelMap = {},
force: boolean = false
force = false
) {
if (this._initialized && !force) return;

Expand All @@ -121,8 +121,8 @@ export class Fyo {
// temp params while calling routes
this.temp = {};

await this.doc.init();
await this.auth.init();
this.doc.init();
this.auth.init();
await this.db.init();
}

Expand Down Expand Up @@ -189,14 +189,14 @@ export class Fyo {
let value: DocValue | Doc[];
try {
doc = await this.doc.getDoc(schemaName, name);
value = doc.get(fieldname!);
value = doc.get(fieldname);
} catch (err) {
value = undefined;
}

if (value === undefined && schemaName === name) {
const sv = await this.db.getSingleValues({
fieldname: fieldname!,
fieldname: fieldname,
parent: schemaName,
});

Expand All @@ -221,8 +221,7 @@ export class Fyo {
this.errorLog = [];
this.temp = {};
await this.db.purgeCache();
await this.auth.purgeCache();
await this.doc.purgeCache();
this.doc.purgeCache();
}

store = {
Expand Down
13 changes: 4 additions & 9 deletions fyo/telemetry/telemetry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Fyo } from 'fyo';
import { ConfigKeys } from 'fyo/core/types';
import { Noun, Telemetry, Verb } from './types';

/**
Expand Down Expand Up @@ -28,8 +27,8 @@ import { Noun, Telemetry, Verb } from './types';
*/

export class TelemetryManager {
#url: string = '';
#token: string = '';
#url = '';
#token = '';
#started = false;
fyo: Fyo;

Expand Down Expand Up @@ -108,16 +107,12 @@ export class TelemetryManager {
noun: Noun,
more?: Record<string, unknown>
): Telemetry {
const countryCode = this.fyo.singles.SystemSettings?.countryCode as
| string
| undefined;

const countryCode = this.fyo.singles.SystemSettings?.countryCode;
return {
country: countryCode ?? '',
language: this.fyo.store.language,
deviceId:
this.fyo.store.deviceId ||
(this.fyo.config.get(ConfigKeys.DeviceId) as string),
this.fyo.store.deviceId || (this.fyo.config.get('deviceId') ?? '-'),
instanceId: this.fyo.store.instanceId,
version: this.fyo.store.appVersion,
openCount: this.fyo.store.openCount,
Expand Down
9 changes: 5 additions & 4 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// eslint-disable-next-line
require('source-map-support').install({
handleUncaughtException: false,
environment: 'node',
Expand All @@ -22,10 +23,10 @@ import registerIpcMainMessageListeners from './main/registerIpcMainMessageListen
import registerProcessListeners from './main/registerProcessListeners';

export class Main {
title: string = 'Frappe Books';
title = 'Frappe Books';
icon: string;

winURL: string = '';
winURL = '';
checkedForUpdate = false;
mainWindow: BrowserWindow | null = null;

Expand Down Expand Up @@ -130,9 +131,9 @@ export class Main {
this.registerAppProtocol();
}

this.mainWindow!.loadURL(this.winURL);
this.mainWindow.loadURL(this.winURL);
if (this.isDevelopment && !this.isTest) {
this.mainWindow!.webContents.openDevTools();
this.mainWindow.webContents.openDevTools();
}

this.setMainWindowListeners();
Expand Down
5 changes: 3 additions & 2 deletions main/contactMothership.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import fetch from 'node-fetch';
import path from 'path';
import { Creds } from 'utils/types';
import { rendererLog } from './helpers';
import type { Main } from 'main';

export function getUrlAndTokenString(): Creds {
const inProduction = app.isPackaged;
Expand Down Expand Up @@ -42,7 +43,7 @@ export function getUrlAndTokenString(): Creds {
};
}

export async function sendError(body: string) {
export async function sendError(body: string, main: Main) {
const { errorLogUrl, tokenString } = getUrlAndTokenString();
const headers = {
Authorization: tokenString,
Expand All @@ -51,6 +52,6 @@ export async function sendError(body: string) {
};

await fetch(errorLogUrl, { method: 'POST', headers, body }).catch((err) => {
rendererLog(err);
rendererLog(main, err);
});
}
11 changes: 6 additions & 5 deletions main/getLanguageMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import fs from 'fs/promises';
import path from 'path';
import { parseCSV } from 'utils/csvParser';
import { LanguageMap } from 'utils/types';

const fetch = require('node-fetch').default;
import fetch from 'node-fetch';

const VALENTINES_DAY = 1644796800000;

Expand Down Expand Up @@ -100,7 +99,7 @@ async function fetchContentsFromApi(code: string) {
return null;
}

const resJson = await res.json();
const resJson = (await res.json()) as { content: string };
return Buffer.from(resJson.content, 'base64').toString();
}

Expand Down Expand Up @@ -138,7 +137,9 @@ async function getLastUpdated(code: string): Promise<Date> {
return new Date(VALENTINES_DAY);
}

const resJson = await res.json();
const resJson = (await res.json()) as {
commit: { author: { date: string } };
}[];
try {
return new Date(resJson[0].commit.author.date);
} catch {
Expand Down Expand Up @@ -187,7 +188,7 @@ async function storeFile(code: string, contents: string) {

async function errorHandledFetch(url: string) {
try {
return (await fetch(url)) as Response;
return await fetch(url);
} catch {
return null;
}
Expand Down
Loading

0 comments on commit 4415c04

Please sign in to comment.