mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-11 20:08:37 +00:00
refactor(infra): directory structure (#4615)
This commit is contained in:
1
packages/common/debug/.gitignore
vendored
Normal file
1
packages/common/debug/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
lib
|
||||
3
packages/common/debug/README.md
Normal file
3
packages/common/debug/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
# @affine/debug
|
||||
|
||||
A common debug interface for packages in this repository.
|
||||
13
packages/common/debug/package.json
Normal file
13
packages/common/debug/package.json
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"name": "@affine/debug",
|
||||
"private": true,
|
||||
"main": "./src/index.ts",
|
||||
"dependencies": {
|
||||
"debug": "^4.3.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/debug": "^4.1.9",
|
||||
"vitest": "0.34.6"
|
||||
},
|
||||
"version": "0.10.0-canary.1"
|
||||
}
|
||||
34
packages/common/debug/src/__tests__/index.spec.ts
Normal file
34
packages/common/debug/src/__tests__/index.spec.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* @vitest-environment happy-dom
|
||||
*/
|
||||
import { describe, expect, test, vi } from 'vitest';
|
||||
|
||||
import { DebugLogger } from '..';
|
||||
|
||||
describe('debug', () => {
|
||||
test('disabled', () => {
|
||||
const logger = new DebugLogger('test');
|
||||
logger.enabled = false;
|
||||
for (const level of ['debug', 'info', 'warn', 'error'] as const) {
|
||||
const fn = vi.fn();
|
||||
vi.spyOn(globalThis.console, level).mockImplementation(fn);
|
||||
expect(logger.enabled).toBe(false);
|
||||
expect(fn).not.toBeCalled();
|
||||
logger[level]('test');
|
||||
expect(fn, level).not.toBeCalled();
|
||||
}
|
||||
});
|
||||
|
||||
test('log', () => {
|
||||
const logger = new DebugLogger('test');
|
||||
logger.enabled = true;
|
||||
for (const level of ['debug', 'info', 'warn', 'error'] as const) {
|
||||
const fn = vi.fn();
|
||||
vi.spyOn(globalThis.console, level).mockImplementation(fn);
|
||||
expect(logger.enabled).toBe(true);
|
||||
expect(fn).not.toBeCalled();
|
||||
logger[level]('test');
|
||||
expect(fn, level).toBeCalled();
|
||||
}
|
||||
});
|
||||
});
|
||||
61
packages/common/debug/src/index.ts
Normal file
61
packages/common/debug/src/index.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import debug from 'debug';
|
||||
type LogLevel = 'debug' | 'info' | 'warn' | 'error';
|
||||
|
||||
const SESSION_KEY = 'affine:debug';
|
||||
|
||||
if (typeof window !== 'undefined') {
|
||||
// enable debug logs if the URL search string contains `debug`
|
||||
// e.g. http://localhost:3000/?debug
|
||||
if (window.location.search.includes('debug')) {
|
||||
// enable debug logs for the current session
|
||||
// since the query string may be removed by the browser after navigations,
|
||||
// we need to store the debug flag in sessionStorage
|
||||
sessionStorage.setItem(SESSION_KEY, 'true');
|
||||
}
|
||||
if (sessionStorage.getItem(SESSION_KEY) === 'true') {
|
||||
// enable all debug logs by default
|
||||
debug.enable('*');
|
||||
console.warn('Debug logs enabled');
|
||||
}
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
debug.enable('*');
|
||||
console.warn('Debug logs enabled');
|
||||
}
|
||||
}
|
||||
|
||||
export class DebugLogger {
|
||||
private _debug: debug.Debugger;
|
||||
|
||||
constructor(namespace: string) {
|
||||
this._debug = debug(namespace);
|
||||
}
|
||||
|
||||
set enabled(enabled: boolean) {
|
||||
this._debug.enabled = enabled;
|
||||
}
|
||||
|
||||
get enabled() {
|
||||
return this._debug.enabled;
|
||||
}
|
||||
|
||||
debug(message: string, ...args: any[]) {
|
||||
this.log('debug', message, ...args);
|
||||
}
|
||||
|
||||
info(message: string, ...args: any[]) {
|
||||
this.log('info', message, ...args);
|
||||
}
|
||||
|
||||
warn(message: string, ...args: any[]) {
|
||||
this.log('warn', message, ...args);
|
||||
}
|
||||
|
||||
error(message: string, ...args: any[]) {
|
||||
this.log('error', message, ...args);
|
||||
}
|
||||
|
||||
log(level: LogLevel, message: string, ...args: any[]) {
|
||||
this._debug.log = console[level].bind(console);
|
||||
this._debug(`[${level.toUpperCase()}] ${message}`, ...args);
|
||||
}
|
||||
}
|
||||
13
packages/common/debug/tsconfig.json
Normal file
13
packages/common/debug/tsconfig.json
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"extends": "../../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"module": "ESNext",
|
||||
"target": "ESNext",
|
||||
"sourceMap": true,
|
||||
"composite": true,
|
||||
"noEmit": false,
|
||||
"outDir": "lib"
|
||||
},
|
||||
"include": ["./src"],
|
||||
"exclude": ["node_modules"]
|
||||
}
|
||||
Reference in New Issue
Block a user