diff --git a/biome.json b/biome.json index 1e70609..937f431 100644 --- a/biome.json +++ b/biome.json @@ -7,7 +7,7 @@ }, "files": { "ignoreUnknown": true, - "ignore": ["dist"] + "ignore": ["dist", "tsconfig.json"] }, "formatter": { "enabled": true, @@ -36,7 +36,7 @@ "noVar": "error" } }, - "ignore": ["types"] + "ignore": ["types", "tsconfig.json"] }, "javascript": { "formatter": { diff --git a/package.json b/package.json index 5675772..320cf13 100644 --- a/package.json +++ b/package.json @@ -4,32 +4,24 @@ "description": "A minimal, flexible logger", "private": false, "type": "module", - "main": "./dist/index.js", - "module": "./dist/index.mjs", + "main": "./dist/index.cjs", + "module": "./dist/index.js", "types": "./dist/index.d.ts", - "typesVersions": { - "*": { - "lib/char": ["./dist/lib/char.d.ts"], - "lib/config": ["./dist/lib/config.d.ts"] - } - }, "exports": { ".": { - "import": "./dist/index.js", - "require": "./dist/index.js" - }, - "./lib/char": { - "import": "./dist/lib/char.js", - "require": "./dist/lib/char.js" - }, - "./lib/config": { - "import": "./dist/lib/config.js", - "require": "./dist/lib/config.js" + "import": { + "types": "./dist/index.d.ts", + "default": "./dist/index.js" + }, + "require": { + "types": "./dist/index.d.ts", + "default": "./dist/index.cjs" + } } }, "scripts": { "dev": "bun run build --watch", - "build": "rm -rf dist && tsup src/index.ts --dts --out-dir dist --format esm,cjs", + "build": "rm -rf dist && tsup && rm -f dist/*.d.cts dist/**/*.d.cts", "lint": "bunx biome check", "lint:fix": "bunx biome check --fix", "cleanup": "rm -rf logs node_modules bun.lock" @@ -38,8 +30,7 @@ "devDependencies": { "@biomejs/biome": "^1.9.4", "@types/bun": "^1.2.15", - "tsup": "^8.5.0", - "typescript": "^5.8.3" + "tsup": "latest" }, "files": ["dist", "README.md", "LICENSE"], "repository": { @@ -53,6 +44,6 @@ "url": "https://git.creations.works/atums/echo/issues" }, "dependencies": { - "date-fns-tz": "^3.2.0" + "date-fns-tz": "latest" } } diff --git a/src/index.ts b/src/index.ts index 597025a..e81d1be 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,17 +14,17 @@ import { getTimestamp, parsePattern, processPattern, -} from "@lib/char"; +} from "#lib/char"; import { defaultConfig, loadEnvConfig, loadLoggerConfig, logLevelValues, validateAndSanitizeConfig, -} from "@lib/config"; -import { FileLogger } from "@lib/file"; +} from "#lib/config"; +import { FileLogger } from "#lib/file"; -import type { LogLevel, LoggerConfig, PatternTokens } from "@types"; +import type { LogLevel, LoggerConfig, PatternTokens } from "#types"; class Echo { private readonly directory: string; @@ -160,4 +160,3 @@ function createLogger(config?: string | LoggerConfig): Echo { const echo = new Echo(); export { echo, Echo, createLogger }; -export type { LoggerConfig, LogLevel } from "@types"; diff --git a/src/lib/char.ts b/src/lib/char.ts index cd6f474..428b324 100644 --- a/src/lib/char.ts +++ b/src/lib/char.ts @@ -1,7 +1,7 @@ import { basename } from "node:path"; import { format, inspect } from "node:util"; -import { ansiColors, defaultLevelColor, logLevelValues } from "@lib/config"; import { format as formatDate } from "date-fns-tz"; +import { ansiColors, defaultLevelColor, logLevelValues } from "#lib/config"; import type { LogLevel, @@ -9,7 +9,7 @@ import type { LoggerConfig, PatternContext, PatternTokens, -} from "@types"; +} from "#types"; function getTimestamp(config: Required): { prettyTimestamp: string; diff --git a/src/lib/config.ts b/src/lib/config.ts index cdaff77..d7a6a26 100644 --- a/src/lib/config.ts +++ b/src/lib/config.ts @@ -1,7 +1,7 @@ import { readFileSync } from "node:fs"; import { resolve } from "node:path"; -import type { LogLevel, LoggerConfig } from "@types"; +import type { LogLevel, LoggerConfig } from "#types"; const logLevelValues = { trace: 10, @@ -51,6 +51,7 @@ const defaultConfig: Required = { rotate: true, maxFiles: null, fileNameFormat: "yyyy-MM-dd", + subDirectory: null, console: true, consoleColor: true, @@ -233,6 +234,10 @@ function loadEnvConfig(): LoggerConfig { config.fileNameFormat = process.env.LOG_FILE_NAME_FORMAT; } + if (process.env.LOG_SUB_DIRECTORY) { + config.subDirectory = process.env.LOG_SUB_DIRECTORY; + } + if (process.env.LOG_DATE_FORMAT) { config.dateFormat = process.env.LOG_DATE_FORMAT; } diff --git a/src/lib/file.ts b/src/lib/file.ts index faec16d..3c89bbe 100644 --- a/src/lib/file.ts +++ b/src/lib/file.ts @@ -1,27 +1,29 @@ import { type WriteStream, createWriteStream, - existsSync, mkdirSync, readdirSync, unlinkSync, } from "node:fs"; import { join } from "node:path"; -import { serializeLogData } from "@lib/char"; import { format } from "date-fns-tz"; +import { serializeLogData } from "#lib/char"; -import type { LogLevel, LoggerConfig } from "@types"; +import type { LogLevel, LoggerConfig } from "#types"; class FileLogger { private stream: WriteStream | null = null; private filePath = ""; private date = ""; private fileNameFormat = "yyyy-MM-dd"; + private logDirectory: string; constructor(private readonly config: Required) { - if (!existsSync(this.config.directory)) { - mkdirSync(this.config.directory, { recursive: true }); - } + this.logDirectory = this.config.subDirectory + ? join(this.config.directory, this.config.subDirectory) + : this.config.directory; + + mkdirSync(this.logDirectory, { recursive: true }); if (this.config.fileNameFormat) { try { @@ -39,7 +41,7 @@ class FileLogger { private getLogFilePath(dateStr: string): string { const fileName = `${dateStr}.jsonl`; - return join(this.config.directory, fileName); + return join(this.logDirectory, fileName); } private resetStream(path: string): void { @@ -72,7 +74,7 @@ class FileLogger { } const fileRegex = this.generateFileRegex(); - const files = readdirSync(this.config.directory) + const files = readdirSync(this.logDirectory) .filter((file) => fileRegex.test(file)) .sort(); @@ -83,7 +85,7 @@ class FileLogger { for (const file of excess) { try { - unlinkSync(join(this.config.directory, file)); + unlinkSync(join(this.logDirectory, file)); } catch {} } } @@ -92,7 +94,7 @@ class FileLogger { if (this.config.rotate && dateStr) { return this.getLogFilePath(dateStr); } - return join(this.config.directory, "log.jsonl"); + return join(this.logDirectory, "log.jsonl"); } public write( diff --git a/tsconfig.json b/tsconfig.json index 31ec442..d5a1b0e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,11 +3,10 @@ "baseUrl": "./", "paths": { "@/*": ["src/*"], - "@types": ["types/index.ts"], - "@types/*": ["types/*"], - "@lib/*": ["src/lib/*"] + "#types": ["types/index.ts"], + "#lib/*": ["src/lib/*"] }, - "typeRoots": ["./types", "./node_modules/@types"], + "typeRoots": ["./node_modules/@types"], "lib": ["ESNext", "DOM"], "target": "ESNext", "module": "ESNext", diff --git a/tsup.config.ts b/tsup.config.ts new file mode 100644 index 0000000..5f67b3a --- /dev/null +++ b/tsup.config.ts @@ -0,0 +1,29 @@ +import { defineConfig } from "tsup"; + +export default defineConfig({ + entry: ["src/index.ts"], + format: ["esm", "cjs"], + dts: { + resolve: true, + compilerOptions: { + module: "ESNext", + moduleResolution: "bundler", + }, + }, + outDir: "dist", + minify: true, + splitting: false, + sourcemap: false, + clean: true, + treeshake: true, + target: "es2024", + define: { + "process.env.NODE_ENV": '"production"', + }, + outExtension({ format }) { + return { + js: format === "cjs" ? ".cjs" : ".js", + dts: ".d.ts", + }; + }, +}); diff --git a/types/index.ts b/types/index.ts index 99ac624..891e3e3 100644 --- a/types/index.ts +++ b/types/index.ts @@ -1,4 +1,4 @@ -import type { ansiColors, logLevelValues } from "@lib/config"; +import type { ansiColors, logLevelValues } from "#lib/config"; type LogLevelValue = (typeof logLevelValues)[keyof typeof logLevelValues]; type LogLevel = keyof typeof logLevelValues; @@ -11,6 +11,7 @@ type LoggerConfig = { rotate?: boolean; maxFiles?: number | null; fileNameFormat?: string; + subDirectory?: string | null; console?: boolean; consoleColor?: boolean;