3 Commits

Author SHA1 Message Date
pascal 6b01ec5e19 implement log lib 2025-09-11 09:57:44 +02:00
pascal 539ebf01d9 update package.json 2025-09-11 09:48:04 +02:00
pascal 30970d257d init repo from library-template 2025-09-11 09:45:24 +02:00
14 changed files with 140 additions and 44 deletions
+3 -3
View File
@@ -1,6 +1,6 @@
bun.lockb
package-lock.json
node_modules/
.idea/
._*
.idea/
.DS_Store
node_modules/
dist/
+7
View File
@@ -0,0 +1,7 @@
.woodpecker/
test/
.gitignore
README.md
.prettierignore
.prettierrc
eslint.config.ts
+4
View File
@@ -0,0 +1,4 @@
.woodpecker
node_modules
test
dist
+11
View File
@@ -0,0 +1,11 @@
{
"arrowParens": "avoid",
"tabWidth": 2,
"useTabs": false,
"bracketSpacing": false,
"printWidth": 120,
"semi": false,
"singleQuote": true,
"jsxSingleQuote": true,
"trailingComma": "none"
}
+19
View File
@@ -0,0 +1,19 @@
when:
- path:
include: ['src/**/*.ts']
steps:
install:
image: node
when:
- event: [pull_request, push, manual]
commands:
- npm install
test:
image: oven/bun:alpine
when:
- event: [pull_request, push, manual]
depends_on: install
commands:
- bun test
+8
View File
@@ -0,0 +1,8 @@
# Library template
Basic template to create a TypeScript library that works properly when included
## How to split in multiple file
Technically, there is nothing to do, but if you want to allow sub-imports, you have to :
1. In `package.json`, under `exports`, add `"./submodule": "./src/submodule.ts"`
2. In `tsconfig.json`, under `paths`, add `"lib-name/*": ["./src/*"]`
+20
View File
@@ -0,0 +1,20 @@
import js from '@eslint/js'
import globals from 'globals'
import tseslint from 'typescript-eslint'
import {defineConfig, globalIgnores} from 'eslint/config'
export default defineConfig([
tseslint.configs.recommended,
{
files: ['**/*.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
plugins: {js},
extends: ['js/recommended'],
languageOptions: {globals: {...globals.browser, ...globals.node}},
rules: {
'no-unused-vars': 'warn',
'@typescript-eslint/no-unused-vars': ['warn']
}
},
globalIgnores(['dist'])
])
+27 -3
View File
@@ -1,11 +1,35 @@
{
"scripts": {
"check": "clear ; npm run typecheck && npm run lint && npx prettier -c **/*.{js,jsx,ts,tsx} && clear && echo 'OK'",
"fmt": "prettier --write **/*.{js,jsx,ts,tsx}",
"lint": "eslint **/*.{js,jsx,ts,tsx}",
"test": "bun test",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"chalk": "^5.6.2"
},
"devDependencies": {
"@eslint/js": "^9.35.0",
"@types/bun": "^1.2.21",
"bun": "^1.2.21",
"eslint": "^9.35.0",
"globals": "^16.4.0",
"jiti": "^2.5.1",
"prettier": "^3.6.2",
"typescript": "^5.9.2",
"typescript-eslint": "^8.43.0"
},
"name": "log",
"description": "log library with logger and writers",
"version": "1.0.0",
"author": "Pascal Perrenoud <pascal@pband.ch>",
"module": "index.ts",
"type": "module",
"files": ["index.ts", "src", "tsconfig.json"]
"main": "./src/index.ts",
"exports": {
".": "./src/index.ts"
}
}
+19
View File
@@ -0,0 +1,19 @@
import {Chalk} from 'chalk'
import {Level} from './types'
const chalk = new Chalk({level: 2}) // 256 colors
export function get_color(level: Level) {
switch (level) {
case Level.DEBUG:
return chalk.blueBright
case Level.TRACE:
return chalk.green
case Level.INFO:
return (str: string) => str
case Level.WARNING:
return chalk.hex('#FFA500')
case Level.ERROR:
return chalk.red
}
}
+2 -2
View File
@@ -1,5 +1,5 @@
import {Level, type WriterOptions, type Writer} from './src/types'
import {Logger, writers, options} from './src/logger'
import {Level, type WriterOptions, type Writer} from './types'
import {Logger, writers, options} from './logger'
export default (namespace: string): Logger => new Logger(namespace)
export {Logger, Level, type WriterOptions, type Writer, writers, options}
+7 -7
View File
@@ -1,7 +1,7 @@
import {Level, type Options, type Writer} from './types'
export const writers = new Map<string, Writer>()
export let options: Options = {
export const options: Options = {
format: '[$time] $level $namespace :',
pad_level: true,
verbose: true
@@ -18,28 +18,28 @@ export class Logger {
return new Logger(`${this.namespace}:${sub_namespace}`)
}
public debug(...data: any[]): void {
public debug(...data: unknown[]): void {
log(data, Level.DEBUG, this._namespace)
}
public trace(...data: any[]): void {
public trace(...data: unknown[]): void {
log(data, Level.TRACE, this._namespace)
}
public info(...data: any[]): void {
public info(...data: unknown[]): void {
log(data, Level.INFO, this._namespace)
}
public warn(...data: any[]): void {
public warn(...data: unknown[]): void {
log(data, Level.WARNING, this._namespace)
}
public error(...data: any[]): void {
public error(...data: unknown[]): void {
log(data, Level.ERROR, this._namespace)
}
}
function log(message: any[], level: Level, namespace: string): void {
function log(message: unknown[], level: Level, namespace: string): void {
if (writers.size === 0) {
if (options.verbose) console.log('No writer subscribed, discard message')
return
+2 -2
View File
@@ -19,11 +19,11 @@ export enum Level {
export type WriterOptions = {
minLevel: Level
[key: string | number | symbol]: any
[key: string | number | symbol]: unknown
}
export interface Writer {
log(level: Level, ...data: any[]): void
log(level: Level, ...data: unknown[]): void
get options(): WriterOptions
readonly _options: WriterOptions
}
+3
View File
@@ -0,0 +1,3 @@
import {test} from 'bun:test'
test('Basic test', () => 'Hello, world!')
+8 -27
View File
@@ -1,33 +1,14 @@
{
"compilerOptions": {
// Enable latest features
"lib": ["ESNext","dom"],
"target": "ESNext",
"target": "ES2020",
"module": "ESNext",
"moduleDetection": "force",
"allowJs": true,
"checkJs": true,
// Bundler mode
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"noEmit": true,
// Best practices
"moduleResolution": "node",
"declaration": true,
"outDir": "dist",
"rootDir": "./src",
"strict": true,
"skipLibCheck": true,
"noFallthroughCasesInSwitch": true,
// Some stricter flags (disabled by default)
"noUnusedLocals": false,
"noUnusedParameters": false,
"forceConsistentCasingInFileNames": true,
"noPropertyAccessFromIndexSignature": false
"esModuleInterop": true,
"skipLibCheck": true
},
"include": [
"index.ts",
"src/**/*.ts",
]
"include": ["src"]
}