Skip to content

Commit

Permalink
Add themes (#2)
Browse files Browse the repository at this point in the history
* Add socio theme

* Add generator package and theme script

* Add support for themes in Logger

* Add getTheme function to retrieve logger options from a theme
  • Loading branch information
CasperSocio committed Dec 13, 2023
1 parent c26bc13 commit 857b49c
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 9 deletions.
2 changes: 2 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ export default {
// An array of glob patterns indicating a set of files for which coverage information should be collected
collectCoverageFrom: [
'src/**/*.ts',
'!src/**/_generated/**',
'!src/**/types.ts',
'!src/themes/**',
'!src/index.ts',
'!src/sandbox.ts',
],
Expand Down
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
"description": "A logging utility for managing and displaying logs in a structured manner.",
"author": "CasperSocio",
"scripts": {
"build": "tsc",
"build": "npm run generate:theme && tsc",
"dev": "ts-node src/sandbox.ts",
"generate:theme": "ts-node scripts/generateThemeTypes.ts",
"prepare": "husky install",
"test": "jest",
"test:ci": "jest --ci --coverage=false",
Expand Down Expand Up @@ -36,6 +37,7 @@
],
"license": "MIT",
"devDependencies": {
"@socio-development/generator": "^0.1.0-alpha.3",
"@types/jest": "^29.5.11",
"@types/node": "^20.10.4",
"husky": "^8.0.3",
Expand Down
30 changes: 30 additions & 0 deletions scripts/generateThemeTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { generate } from '@socio-development/generator'
import { existsSync, readdirSync } from 'fs'
import { resolve } from 'path'

const rootPath = resolve('.')
const themesPath = resolve(rootPath, 'src', 'themes')

if (!existsSync(themesPath)) {
throw new Error(`Themes path does not exist: ${themesPath}`)
}

const themeFiles = readdirSync(themesPath)
const themeNames = themeFiles
.filter((file) => file.endsWith('.ts'))
.map((file) => file.replace('.ts', ''))

const code = `
export const themeNames = ['${themeNames.join("', '")}'] as const
export type ThemeName = (typeof themeNames)[number]
`

console.log(code)

generate({
code,
file: 'theme.ts',
path: 'src',
})
6 changes: 6 additions & 0 deletions src/__tests__/classes/Logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ describe('Logger', () => {

expect(logger).toBeInstanceOf(Logger)
})

it('should create a new logger with a theme', () => {
const logger = new Logger('socio')

expect(logger).toBeInstanceOf(Logger)
})
})

describe('.entries', () => {
Expand Down
3 changes: 3 additions & 0 deletions src/_generated/theme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const themeNames = ['socio'] as const

export type ThemeName = (typeof themeNames)[number]
28 changes: 20 additions & 8 deletions src/classes/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
LoggerData,
LoggerOptions,
} from 'src/types'
import { ThemeName } from '../_generated/theme'
import LogEntry from './LogEntry'

const defaultOptions: LoggerConfig = {
Expand Down Expand Up @@ -48,23 +49,34 @@ export default class Logger {
private _separator: string
private _timestampFormat: string

constructor(options: Partial<LoggerOptions> = {}) {
if (options.indentSize && options.indentSize < 1) {
constructor(theme?: ThemeName)
constructor(options?: LoggerOptions)
constructor(optionsOrTheme: LoggerOptions | ThemeName = {}) {
let config: LoggerOptions

if (typeof optionsOrTheme === 'string') {
const theme = require(`../themes/${optionsOrTheme}`).default
config = theme
} else {
config = optionsOrTheme
}

if (config.indentSize && config.indentSize < 1) {
throw new Error('Indent size must be greater than 0')
}

this._currentIndent = 0
this._indentSize = options.indentSize || defaultOptions.indentSize
this._indentSize = config.indentSize || defaultOptions.indentSize
this._entries = []
this._prefix = options.prefix
this._separator = options.separator || defaultOptions.separator
this._prefix = config.prefix
this._separator = config.separator || defaultOptions.separator
this._timestampFormat =
options.timestampFormat || defaultOptions.timestampFormat
config.timestampFormat || defaultOptions.timestampFormat

if (options.composition) {
if (config.composition) {
this._composition = {
...defaultOptions.composition,
...options.composition,
...config.composition,
}
} else {
this._composition = defaultOptions.composition
Expand Down
18 changes: 18 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
import { ThemeName } from './_generated/theme'
import { LoggerOptions } from './types'

export { default as Logger } from './classes/Logger'

/**
* Gets the logger options from a theme.
* @param theme The theme name.
* @returns The logger options.
*/
export function getTheme(theme: ThemeName): LoggerOptions {
const options = require(`./themes/${theme}`).default

if (!options) {
throw new Error(`Theme "${theme}" does not contain a valid configuration.`)
}

return options
}
42 changes: 42 additions & 0 deletions src/themes/socio.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { LoggerConfig } from 'src/types'

export default {
composition: {
default: [
'timestamp',
'color:socio',
'prefix',
'color:reset',
'indent',
'message',
],
error: [
'timestamp',
'color:socio',
'prefix',
'color:red',
'indent',
'message',
],
info: [
'timestamp',
'color:socio',
'prefix',
'color:blue',
'indent',
'message',
],
warn: [
'timestamp',
'color:socio',
'prefix',
'color:yellow',
'indent',
'message',
],
},
indentSize: 2,
prefix: '[SOCIO]',
separator: ' ',
timestampFormat: 'HH:mm:ss',
} satisfies LoggerConfig

0 comments on commit 857b49c

Please sign in to comment.