refactor: move logger into commons to use it in next.config.js

Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
Tilman Vatteroth 2023-09-24 14:19:13 +02:00
parent 49ec70f2aa
commit d675cc9ed9
No known key found for this signature in database
GPG key ID: FE1CD209E3EA5E85
45 changed files with 98 additions and 71 deletions

View file

@ -1,26 +1,29 @@
{
"testRegex" : "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
"testPathIgnorePatterns" : [
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
"testPathIgnorePatterns": [
"/dist/"
],
"moduleFileExtensions" : [
"moduleFileExtensions": [
"ts",
"tsx",
"js"
],
"extensionsToTreatAsEsm" : [
"extensionsToTreatAsEsm": [
".ts"
],
"moduleNameMapper" : {
"^(\\.{1,2}/.*)\\.js$" : "$1"
"moduleNameMapper": {
"^(\\.{1,2}/.*)\\.js$": "$1"
},
"transformIgnorePatterns": ["<rootDir>/node_modules/"],
"transform" : {
"^.+\\.tsx?$" : [
"testEnvironment": "jsdom",
"transformIgnorePatterns": [
"<rootDir>/node_modules/"
],
"transform": {
"^.+\\.tsx?$": [
"ts-jest",
{
"tsconfig" : "tsconfig.test.json",
"useESM" : true
"tsconfig": "tsconfig.test.json",
"useESM": true
}
]
}

View file

@ -7,3 +7,4 @@
export * from './wait-for-other-promises-to-finish.js'
export type { DeepPartial } from './deep-partial.js'
export * from './test-modes.js'
export * from './logger.js'

View file

@ -3,13 +3,22 @@
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { Logger } from './logger'
import { Logger } from './logger.js'
import { Settings } from 'luxon'
import { Mock } from 'ts-mockery'
import {
jest,
describe,
beforeEach,
expect,
it,
afterEach
} from '@jest/globals'
import type { SpyInstance } from 'jest-mock'
let testMode = false
let devMode = false
jest.mock('@hedgedoc/commons', () => ({
jest.mock('../utils/test-modes.js', () => ({
get isTestMode() {
return testMode
},
@ -22,11 +31,11 @@ describe('Logger', () => {
let originalNow: () => number
let dateShift = 0
let infoLogMock: jest.SpyInstance
let warnLogMock: jest.SpyInstance
let errorLogMock: jest.SpyInstance
let debugLogMock: jest.SpyInstance
let defaultLogMock: jest.SpyInstance
let infoLogMock: SpyInstance
let warnLogMock: SpyInstance
let errorLogMock: SpyInstance
let debugLogMock: SpyInstance
let defaultLogMock: SpyInstance
let isLocalStorageAccessDenied = false
const mockLocalStorage = () => {
@ -63,7 +72,16 @@ describe('Logger', () => {
defaultLogMock = jest.spyOn(console, 'log').mockReturnValue()
originalNow = Settings.now
Settings.now = () => new Date(2021, 9, 25, dateShift, 1 + dateShift, 2 + dateShift, 3 + dateShift).valueOf()
Settings.now = () =>
new Date(
2021,
9,
25,
dateShift,
1 + dateShift,
2 + dateShift,
3 + dateShift
).valueOf()
})
afterEach(() => {

View file

@ -1,9 +1,9 @@
/*
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
* SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { isDevMode, isTestMode } from '@hedgedoc/commons'
import { isDevMode, isTestMode } from './test-modes.js'
import { DateTime } from 'luxon'
import pico from 'picocolors'
@ -16,7 +16,8 @@ export class Logger {
constructor(scope: string) {
this.scope = scope
this.debugLoggingEnabled = isDevMode || isTestMode || this.isDebugLoggingEnabled()
this.debugLoggingEnabled =
isDevMode || isTestMode || this.isDebugLoggingEnabled()
}
private isDebugLoggingEnabled() {
@ -75,7 +76,11 @@ export class Logger {
if (typeof window === 'undefined') {
return [pico.yellow(`[${timestamp}]`), pico.green(`(${this.scope})`)]
} else {
return [`%c[${timestamp}] %c(${this.scope})`, 'color: yellow', 'color: green']
return [
`%c[${timestamp}] %c(${this.scope})`,
'color: yellow',
'color: green'
]
}
}
}

View file

@ -3,17 +3,19 @@
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
const { isMockMode, isTestMode, isProfilingMode, isBuildTime } = require('@hedgedoc/commons')
const { isMockMode, isTestMode, isProfilingMode, isBuildTime, Logger } = require('@hedgedoc/commons')
const path = require('path')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const withBundleAnalyzer = require('@next/bundle-analyzer')({
enabled: isProfilingMode
})
console.log('Node environment is', process.env.NODE_ENV)
const logger = new Logger('Bootstrap')
logger.info('Node environment is', process.env.NODE_ENV)
if (isTestMode) {
console.warn(`This build runs in test mode. This means:
logger.warn(`This build runs in test mode. This means:
- No sandboxed iframe
- Additional data-attributes for e2e tests added to DOM
- Editor and renderer are running on the same origin
@ -22,7 +24,7 @@ if (isTestMode) {
}
if (isMockMode) {
console.warn(`This build runs in mock mode. This means:
logger.warn(`This build runs in mock mode. This means:
- No real data. All API responses are mocked
- No persistent data
- No realtime editing
@ -30,14 +32,14 @@ if (isMockMode) {
}
if (isBuildTime) {
console.warn(`This process runs in build mode. During build time this means:
logger.warn(`This process runs in build mode. During build time this means:
- Editor and Renderer base urls are https://example.org
- No frontend config will be fetched
`)
}
if (isProfilingMode) {
console.info('This build contains the bundle analyzer and profiling metrics.')
logger.info('This build contains the bundle analyzer and profiling metrics.')
}
/** @type {import('@svgr/webpack').LoaderOptions} */

View file

@ -4,7 +4,7 @@
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { Logger } from '../../utils/logger'
import { Logger } from '@hedgedoc/commons'
import { ApplicationLoaderError } from './application-loader-error'
import { createSetUpTaskList } from './initializers'
import { LoadingScreen } from './loading-screen/loading-screen'

View file

@ -4,7 +4,7 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { refreshHistoryState } from '../../../redux/history/methods'
import { Logger } from '../../../utils/logger'
import { Logger } from '@hedgedoc/commons'
import { isDevMode, isTestMode } from '@hedgedoc/commons'
import { fetchAndSetUser } from '../../login-page/auth/utils'
import { loadDarkMode } from './load-dark-mode'

View file

@ -6,7 +6,7 @@
import { DARK_MODE_LOCAL_STORAGE_KEY } from '../../../hooks/dark-mode/use-save-dark-mode-preference-to-local-storage'
import { setDarkModePreference } from '../../../redux/dark-mode/methods'
import { DarkModePreference } from '../../../redux/dark-mode/types'
import { Logger } from '../../../utils/logger'
import { Logger } from '@hedgedoc/commons'
const logger = new Logger('Dark mode initializer')

View file

@ -3,7 +3,7 @@
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { Logger } from '../../../../utils/logger'
import { Logger } from '@hedgedoc/commons'
import { UiIcon } from '../../icons/ui-icon'
import { ShowIf } from '../../show-if/show-if'
import { CopyToClipboardButton } from '../copy-to-clipboard-button/copy-to-clipboard-button'

View file

@ -3,7 +3,7 @@
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { Logger } from '../../../../utils/logger'
import { Logger } from '@hedgedoc/commons'
import { ShowIf } from '../../show-if/show-if'
import type { ReactElement, RefObject } from 'react'
import React, { useCallback, useEffect, useMemo, useState } from 'react'

View file

@ -3,7 +3,7 @@
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { Logger } from '../../../../utils/logger'
import { Logger } from '@hedgedoc/commons'
import type { HLJSApi } from 'highlight.js'
import { useAsync } from 'react-use'
import type { AsyncState } from 'react-use/lib/useAsyncFn'

View file

@ -6,7 +6,7 @@
*/
import { ApiError } from '../../../api/common/api-error'
import { ErrorToI18nKeyMapper } from '../../../api/common/error-to-i18n-key-mapper'
import { Logger } from '../../../utils/logger'
import { Logger } from '@hedgedoc/commons'
import { LoadingScreen } from '../../application-loader/loading-screen/loading-screen'
import { CommonErrorPage } from '../../error-pages/common-error-page'
import { CustomAsyncLoadingBoundary } from '../async-loading-boundary/custom-async-loading-boundary'

View file

@ -4,7 +4,7 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { ORIGIN, useBaseUrl } from '../../../../hooks/common/use-base-url'
import { Logger } from '../../../../utils/logger'
import { Logger } from '@hedgedoc/commons'
import { useEditorToRendererCommunicator } from '../../../editor-page/render-context/editor-to-renderer-communicator-context-provider'
import type { RefObject } from 'react'
import { useCallback, useEffect, useMemo, useRef } from 'react'

View file

@ -5,7 +5,7 @@
*/
import { concatCssClasses } from '../../../utils/concat-css-classes'
import { cypressAttribute, cypressId } from '../../../utils/cypress-attribute'
import { Logger } from '../../../utils/logger'
import { Logger } from '@hedgedoc/commons'
import { isTestMode } from '@hedgedoc/commons'
import { useEditorToRendererCommunicator } from '../../editor-page/render-context/editor-to-renderer-communicator-context-provider'
import type { ScrollProps } from '../../editor-page/synced-scroll/scroll-props'

View file

@ -5,7 +5,7 @@
*/
import type { PropsWithDataCypressId } from '../../utils/cypress-attribute'
import { cypressId } from '../../utils/cypress-attribute'
import { Logger } from '../../utils/logger'
import { Logger } from '@hedgedoc/commons'
import type { MutableRefObject } from 'react'
import React, { useCallback, useEffect, useRef } from 'react'

View file

@ -4,7 +4,7 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { getGlobalState } from '../../../../../redux'
import { Logger } from '../../../../../utils/logger'
import { Logger } from '@hedgedoc/commons'
import { useEditorReceiveHandler } from '../../../../render-page/window-post-message-communicator/hooks/use-editor-receive-handler'
import type { ImageUploadMessage } from '../../../../render-page/window-post-message-communicator/rendering-message'
import { CommunicationMessageType } from '../../../../render-page/window-post-message-communicator/rendering-message'

View file

@ -3,7 +3,7 @@
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { Logger } from '../../../../utils/logger'
import { Logger } from '@hedgedoc/commons'
import { useCodemirrorReferenceContext } from '../../change-content-context/codemirror-reference-context'
import type { ScrollState } from '../../synced-scroll/scroll-props'
import { EditorView } from '@codemirror/view'

View file

@ -4,7 +4,7 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { setRealtimeSyncedState } from '../../../../../redux/realtime/methods'
import { Logger } from '../../../../../utils/logger'
import { Logger } from '@hedgedoc/commons'
import type { MessageTransporter, RealtimeDoc } from '@hedgedoc/commons'
import { YDocSyncClientAdapter } from '@hedgedoc/commons'
import type { Listener } from 'eventemitter2'

View file

@ -4,7 +4,7 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { cypressId } from '../../../../../utils/cypress-attribute'
import { Logger } from '../../../../../utils/logger'
import { Logger } from '@hedgedoc/commons'
import { ShowIf } from '../../../../common/show-if/show-if'
import { acceptedMimeTypes } from '../../../../common/upload-image-mimetypes'
import { UploadInput } from '../../../../common/upload-input'

View file

@ -4,7 +4,7 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { useApplicationState } from '../../../hooks/common/use-application-state'
import { Logger } from '../../../utils/logger'
import { Logger } from '@hedgedoc/commons'
import type { ScrollSource } from '../editor-page-content'
import type { ScrollState } from '../synced-scroll/scroll-props'
import type { RefObject } from 'react'

View file

@ -3,7 +3,7 @@
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { Logger } from '../../../utils/logger'
import { Logger } from '@hedgedoc/commons'
import type { ScrollSource } from '../editor-page-content'
import type { MutableRefObject } from 'react'
import { useCallback } from 'react'

View file

@ -4,7 +4,7 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { defaultConfig } from '../../../api/common/default-config'
import { Logger } from '../../../utils/logger'
import { Logger } from '@hedgedoc/commons'
export const MOTD_LOCAL_STORAGE_KEY = 'motd.lastModified'
const log = new Logger('Motd')

View file

@ -5,7 +5,7 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { cypressId } from '../../../utils/cypress-attribute'
import { Logger } from '../../../utils/logger'
import { Logger } from '@hedgedoc/commons'
import { testId } from '../../../utils/test-id'
import { CommonModal } from '../../common/modals/common-modal'
import { RendererIframe } from '../../common/renderer-iframe/renderer-iframe'

View file

@ -4,7 +4,7 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { cypressId } from '../../../../utils/cypress-attribute'
import { Logger } from '../../../../utils/logger'
import { Logger } from '@hedgedoc/commons'
import { testId } from '../../../../utils/test-id'
import { availableLanguages } from './available-languages'
import { LanguageOption } from './language-option'

View file

@ -3,7 +3,7 @@
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { Logger } from '../../utils/logger'
import { Logger } from '@hedgedoc/commons'
import { AsyncLoadingBoundary } from '../common/async-loading-boundary/async-loading-boundary'
import { RendererIframe } from '../common/renderer-iframe/renderer-iframe'
import { RendererType } from '../render-page/window-post-message-communicator/rendering-message'

View file

@ -5,7 +5,7 @@
*/
import type { AuthProvider } from '../../../../api/config/types'
import { AuthProviderType } from '../../../../api/config/types'
import { Logger } from '../../../../utils/logger'
import { Logger } from '@hedgedoc/commons'
import { IconGitlab } from '../../../common/icons/additional/icon-gitlab'
import styles from '../via-one-click.module.scss'
import type { Icon } from 'react-bootstrap-icons'

View file

@ -4,7 +4,7 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { getProxiedUrl } from '../../../../api/media'
import { Logger } from '../../../../utils/logger'
import { Logger } from '@hedgedoc/commons'
import { useFrontendConfig } from '../../../common/frontend-config-context/use-frontend-config'
import React, { useEffect, useState } from 'react'

View file

@ -3,7 +3,7 @@
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { Logger } from '../../../../utils/logger'
import { Logger } from '@hedgedoc/commons'
import { TravelerNodeProcessor } from '../../node-preprocessors/traveler-node-processor'
import type { DataNode, Element, Node } from 'domhandler'
import { isComment, isTag } from 'domhandler'

View file

@ -3,7 +3,7 @@
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { Logger } from '../../../utils/logger'
import { Logger } from '@hedgedoc/commons'
import type { SlideOptions } from '@hedgedoc/commons'
import { useEffect, useRef, useState } from 'react'
import type Reveal from 'reveal.js'

View file

@ -5,7 +5,7 @@
*/
import type { PropsWithDataCypressId } from '../../../../utils/cypress-attribute'
import { cypressId } from '../../../../utils/cypress-attribute'
import { Logger } from '../../../../utils/logger'
import { Logger } from '@hedgedoc/commons'
import { ShowIf } from '../../../common/show-if/show-if'
import { ProxyImageFrame } from '../../extensions/image/proxy-image-frame'
import styles from './click-shield.module.scss'

View file

@ -4,7 +4,7 @@
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { Logger } from '../../utils/logger'
import { Logger } from '@hedgedoc/commons'
import type { DispatchOptions, UiNotification } from './types'
import { UiNotifications } from './ui-notifications'
import type { TOptions } from 'i18next'

View file

@ -4,7 +4,7 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { cypressId } from '../../utils/cypress-attribute'
import { Logger } from '../../utils/logger'
import { Logger } from '@hedgedoc/commons'
import { UiIcon } from '../common/icons/ui-icon'
import { ShowIf } from '../common/show-if/show-if'
import styles from './notifications.module.scss'

View file

@ -3,7 +3,7 @@
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { Logger } from '../../../utils/logger'
import { Logger } from '@hedgedoc/commons'
import type {
CommunicationMessages,
EditorToRendererMessageType,

View file

@ -3,7 +3,7 @@
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { Logger } from '../../../utils/logger'
import { Logger } from '@hedgedoc/commons'
import type {
CommunicationMessages,
EditorToRendererMessageType,

View file

@ -3,7 +3,7 @@
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import type { Logger } from '../../../utils/logger'
import type { Logger } from '@hedgedoc/commons'
import { Optional } from '@mrdrogdrog/optional'
import { EventEmitter2 } from 'eventemitter2'

View file

@ -5,7 +5,7 @@
*/
import { useRendererToEditorCommunicator } from '../../../../components/editor-page/render-context/renderer-to-editor-communicator-context-provider'
import { CommunicationMessageType } from '../../../../components/render-page/window-post-message-communicator/rendering-message'
import { Logger } from '../../../../utils/logger'
import { Logger } from '@hedgedoc/commons'
import { FileContentFormat, readFile } from '../../../../utils/read-file'
import { useCallback } from 'react'

View file

@ -9,7 +9,7 @@ import { WaitSpinner } from '../../../components/common/wait-spinner/wait-spinne
import type { CodeProps } from '../../../components/markdown-renderer/replace-components/code-block-component-replacer'
import { useEffectWithCatch } from '../../../hooks/common/use-effect-with-catch'
import { cypressId } from '../../../utils/cypress-attribute'
import { Logger } from '../../../utils/logger'
import { Logger } from '@hedgedoc/commons'
import styles from './abc.module.scss'
import React, { useRef } from 'react'
import { useAsync } from 'react-use'

View file

@ -8,7 +8,7 @@ import { AsyncLoadingBoundary } from '../../../components/common/async-loading-b
import { ShowIf } from '../../../components/common/show-if/show-if'
import type { CodeProps } from '../../../components/markdown-renderer/replace-components/code-block-component-replacer'
import { useDarkModeState } from '../../../hooks/dark-mode/use-dark-mode-state'
import { Logger } from '../../../utils/logger'
import { Logger } from '@hedgedoc/commons'
import { testId } from '../../../utils/test-id'
import React, { useEffect, useRef, useState } from 'react'
import { useTranslation } from 'react-i18next'

View file

@ -7,7 +7,7 @@ import { AsyncLoadingBoundary } from '../../../components/common/async-loading-b
import { ShowIf } from '../../../components/common/show-if/show-if'
import type { CodeProps } from '../../../components/markdown-renderer/replace-components/code-block-component-replacer'
import { cypressId } from '../../../utils/cypress-attribute'
import { Logger } from '../../../utils/logger'
import { Logger } from '@hedgedoc/commons'
import React, { useCallback, useEffect, useRef, useState } from 'react'
import { useAsync } from 'react-use'
import { ApplicationErrorAlert } from '../../../components/common/application-error-alert/application-error-alert'

View file

@ -6,7 +6,7 @@
import { ShowIf } from '../../../components/common/show-if/show-if'
import type { CodeProps } from '../../../components/markdown-renderer/replace-components/code-block-component-replacer'
import { cypressId } from '../../../utils/cypress-attribute'
import { Logger } from '../../../utils/logger'
import { Logger } from '@hedgedoc/commons'
import styles from './mermaid.module.scss'
import React, { Fragment, useRef } from 'react'
import { useTranslation } from 'react-i18next'

View file

@ -6,7 +6,7 @@
import { AsyncLoadingBoundary } from '../../../components/common/async-loading-boundary/async-loading-boundary'
import { ShowIf } from '../../../components/common/show-if/show-if'
import type { CodeProps } from '../../../components/markdown-renderer/replace-components/code-block-component-replacer'
import { Logger } from '../../../utils/logger'
import { Logger } from '@hedgedoc/commons'
import React, { useEffect, useRef } from 'react'
import { useTranslation } from 'react-i18next'
import { useAsync } from 'react-use'

View file

@ -4,7 +4,7 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { DarkModePreference } from '../../redux/dark-mode/types'
import { Logger } from '../../utils/logger'
import { Logger } from '@hedgedoc/commons'
import { useApplicationState } from '../common/use-application-state'
import { useEffect } from 'react'

View file

@ -6,7 +6,7 @@
import type { EditorConfig, EditorConfigActions } from './types'
import { EditorConfigActionType } from './types'
import type { Reducer } from 'redux'
import { Logger } from '../../utils/logger'
import { Logger } from '@hedgedoc/commons'
const logger = new Logger('EditorConfig Local Storage')

View file

@ -14,7 +14,7 @@ import { addRemoteOriginToHistoryEntry, historyEntryToHistoryEntryPutDto } from
import type { HistoryEntry, HistoryEntryWithOrigin } from '../../api/history/types'
import { HistoryEntryOrigin } from '../../api/history/types'
import { download } from '../../components/common/download/download'
import { Logger } from '../../utils/logger'
import { Logger } from '@hedgedoc/commons'
import { getGlobalState, store } from '../index'
import type { HistoryExportJson, RemoveEntryAction, SetEntriesAction, UpdateEntryAction, V1HistoryEntry } from './types'
import { HistoryActionType } from './types'

View file

@ -4,9 +4,7 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
import type { BaseUrls } from '../components/common/base-url/base-url-context-provider'
import { Logger } from './logger'
import { isTestMode, isBuildTime } from '@hedgedoc/commons'
import { NoSubdirectoryAllowedError, parseUrl } from '@hedgedoc/commons'
import { NoSubdirectoryAllowedError, parseUrl, Logger, isTestMode, isBuildTime } from '@hedgedoc/commons'
import { Optional } from '@mrdrogdrog/optional'
/**