Skip to content

Commit

Permalink
[Refactor] Logger (#1667)
Browse files Browse the repository at this point in the history
* Change complete project structure

 - Rename "src" -> "src/frontend"
 - Rename "electron" -> "src/backend"
 - Move types.ts to "common/types.ts"

* Switch from react-scripts + foreman + webpack + ts-loader -> Vite

* Set GH_TOKEN and GITHUB_TOKEN in tests directly

This just cleans up package.json a bit

* Get SVG & JPG importing working in VSCode

* Move type definitions into common/typedefs

* Fixup tests

* Rewrite large chunks of the backend to be compatible with strict mode

* Modify frontend to reflect changes to type definition structure

* Remove unused files

These weren't necessary, at least as far as I can tell

* Update translations

* Mapped logger refactor on vite pr

* More clean up

* Fixed tests and added more lint rules

* lint-fix

* Small adjustments

* Add more tests

* i18n

* review suggestions

* remove test.only

* review suggestions (2)

Co-authored-by: Mathis Dröge <mathis.droege@ewe.net>
  • Loading branch information
Nocccer and CommandMC committed Sep 13, 2022
1 parent a00a50f commit 2667bc3
Show file tree
Hide file tree
Showing 74 changed files with 1,403 additions and 965 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ i18next-parser.config.js
flatpak/build/
flatpak/.flatpak-builder/
vite.config.ts
**/__tests__/**
**/__tests__/**
**/__mocks__/**
5 changes: 3 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"sourceType": "module",
"project": ["./tsconfig.json"]
},
"plugins": ["react", "@typescript-eslint"],
"plugins": ["react", "@typescript-eslint", "import"],
"settings": {
"react": {
"version": "detect"
Expand Down Expand Up @@ -49,6 +49,7 @@
"error",
"single",
{ "avoidEscape": true, "allowTemplateLiterals": true }
]
],
"import/no-duplicates": ["error"]
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@
"electron-devtools-installer": "^3.2.0",
"eslint": "^8.13.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-react": "^7.29.4",
"husky": "^7.0.4",
"i18next-parser": "^6.3.0",
Expand Down
13 changes: 13 additions & 0 deletions src/backend/__mocks__/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const constants = jest.requireActual('../constants')

constants.configStore = {
has: jest.fn(),
set: jest.fn(),
get: jest.fn()
}

constants.currentLogFile = 'current.log'
constants.lastLogFile = 'last.log'

module.exports = constants
export {}
20 changes: 16 additions & 4 deletions src/backend/__mocks__/electron.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
import { tmpdir } from 'os'
import { join } from 'path'

export const dialog = {
const appBasePath = tmpdir()
const dialog = {
// dialog override
showErrorBox: jest.fn(),
showMessageBox: jest.fn()
}

export const app = {
getPath: jest.fn().mockReturnValue(tmpdir())
const app = {
// app override
getPath: jest.fn().mockImplementation((path: string) => {
return join(appBasePath, path)
})
}

export class Notification {
class Notification {
public show() {
return
}

public isSupported() {
return false
}
}

export { dialog, app, Notification }
6 changes: 6 additions & 0 deletions src/backend/__mocks__/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const utils = jest.requireActual('../utils')

utils.showErrorBoxModalAuto = jest.fn()

module.exports = utils
export {}
3 changes: 2 additions & 1 deletion src/backend/__tests__/constants.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { fixAsarPath, getShell } from '../constants'

jest.mock('../config', () => '')
jest.mock('../logger/logfile')

describe('Constants - fixAsarPath', () => {
test('need to fix path and replace correctly', () => {
const fixed_path = fixAsarPath('path/app.asar/bin')
Expand Down
69 changes: 0 additions & 69 deletions src/backend/__tests__/logger.test.ts

This file was deleted.

12 changes: 4 additions & 8 deletions src/backend/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@ jest.mock('../logger/logger', () => {
}
})

jest.mock('../logger/logfile')

describe('electron/utils.ts', () => {
test('quoteIfNeccessary', () => {
const testCases = new Map<string, string>([
['path/without/spaces', 'path/without/spaces'],
['path/with /spaces', '"path/with /spaces"'],
['"path/with /start/quote', '""path/with /start/quote"'],
['path/with /end/quote"', '"path/with /end/quote""'],
['"path/quoted/without/spaces"', '"path/quoted/without/spaces"'],
['"path/quoted/with /spaces"', '"path/quoted/with /spaces"'],
[undefined as any, 'undefined']
['"path/quoted/with /spaces"', '"path/quoted/with /spaces"']
])

testCases.forEach((expectString, inputString) => {
Expand All @@ -28,10 +27,7 @@ describe('electron/utils.ts', () => {
test('removeQuotesIfNeccessary', () => {
const testCases = new Map<string, string>([
['path/without/quotes', 'path/without/quotes'],
['"path/with/quotes"', 'path/with/quotes'],
['"path/with/start/quote', '"path/with/start/quote'],
['path/with/end/quote"', 'path/with/end/quote"'],
[undefined as any, 'undefined']
['"path/with/quotes"', 'path/with/quotes']
])

testCases.forEach((expectString, inputString) => {
Expand Down
9 changes: 4 additions & 5 deletions src/backend/anticheat/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ async function downloadAntiCheatData() {
'https://raw.githubusercontent.com/Starz0r/AreWeAntiCheatYet/HEAD/games.json'
)
writeFileSync(heroicAnticheatDataPath, JSON.stringify(data, null, 2))
logInfo(`AreWeAntiCheatYet data downloaded`, LogPrefix.Backend)
logInfo(`AreWeAntiCheatYet data downloaded`, { prefix: LogPrefix.Backend })
} catch (error) {
logWarning(
`Failed download of AreWeAntiCheatYet data: ${error}`,
LogPrefix.Backend
)
logWarning(['Failed download of AreWeAntiCheatYet data:', error], {
prefix: LogPrefix.Backend
})
}
}

Expand Down
26 changes: 10 additions & 16 deletions src/backend/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,9 @@ abstract class GlobalConfig {
try {
version = JSON.parse(readFileSync(heroicConfigPath, 'utf-8'))['version']
} catch (error) {
logError(
`Config file is corrupted, please check ${heroicConfigPath}`,
LogPrefix.Backend,
false
)
logError(`Config file is corrupted, please check ${heroicConfigPath}`, {
prefix: LogPrefix.Backend
})
version = 'v0'
}
// Legacy config file without a version field, it's a v0 config.
Expand Down Expand Up @@ -99,28 +97,24 @@ abstract class GlobalConfig {
GlobalConfig.globalInstance = new GlobalConfigV0()
break
default:
logError(
`Invalid config version '${version}' requested.`,
LogPrefix.GlobalConfig,
false
)
logError(`Invalid config version '${version}' requested.`, {
prefix: LogPrefix.GlobalConfig
})
break
}
// Try to upgrade outdated config.
if (GlobalConfig.globalInstance.upgrade()) {
// Upgrade done, we need to fully reload config.
logInfo(
`Upgraded outdated ${version} config to ${currentGlobalConfigVersion}.`,
LogPrefix.GlobalConfig
{ prefix: LogPrefix.GlobalConfig }
)
return GlobalConfig.reload(currentGlobalConfigVersion)
} else if (version !== currentGlobalConfigVersion) {
// Upgrade failed.
logError(
`Failed to upgrade outdated ${version} config.`,
LogPrefix.GlobalConfig,
false
)
logError(`Failed to upgrade outdated ${version} config.`, {
prefix: LogPrefix.GlobalConfig
})
}
}

Expand Down
14 changes: 5 additions & 9 deletions src/backend/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ import Store from 'electron-store'
import { parse } from '@node-steam/vdf'

import { GameConfigVersion, GlobalConfigVersion } from 'common/types'
import {
createNewLogFileAndClearOldOnces,
logDebug,
LogPrefix
} from './logger/logger'
import { logDebug, LogPrefix } from './logger/logger'
import { createNewLogFileAndClearOldOnces } from './logger/logfile'
import { env } from 'process'
import { app } from 'electron'
import { existsSync, readFileSync } from 'graceful-fs'
Expand Down Expand Up @@ -163,10 +160,9 @@ export async function getSteamLibraries(): Promise<string[]> {
(path) => existsSync(path)
)
}
logDebug(
'Unable to load Steam Libraries, libraryfolders.vdf not found',
LogPrefix.Backend
)
logDebug('Unable to load Steam Libraries, libraryfolders.vdf not found', {
prefix: LogPrefix.Backend
})
return libraries
}

Expand Down
18 changes: 8 additions & 10 deletions src/backend/game_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,9 @@ abstract class GameConfig {
try {
version = JSON.parse(readFileSync(path, 'utf-8'))['version']
} catch (error) {
logError(
`Config file is corrupted, please check ${path}`,
LogPrefix.Backend
)
logError(`Config file is corrupted, please check ${path}`, {
prefix: LogPrefix.Backend
})
version = 'v0'
}
// Legacy config file without a version field, it's a v0 config.
Expand Down Expand Up @@ -95,7 +94,7 @@ abstract class GameConfig {
default:
logError(
`[${appName}]: Invalid config version '${version}' requested.`,
LogPrefix.GameConfig
{ prefix: LogPrefix.GameConfig }
)
break
}
Expand All @@ -104,15 +103,14 @@ abstract class GameConfig {
// Upgrade done, we need to fully reload config.
logInfo(
`[${appName}]: Upgraded outdated ${version} config to ${currentGameConfigVersion}.`,
LogPrefix.GameConfig
{ prefix: LogPrefix.GameConfig }
)
return GameConfig.reload(appName, currentGameConfigVersion)
} else if (version !== currentGameConfigVersion) {
// Upgrade failed.
logError(
`[${appName}]: Failed to upgrade outdated ${version} config.`,
LogPrefix.GameConfig
)
logError(`[${appName}]: Failed to upgrade outdated ${version} config.`, {
prefix: LogPrefix.GameConfig
})
}
}

Expand Down

0 comments on commit 2667bc3

Please sign in to comment.