@jpp-toolkit/logger 0.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Julien Papini
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,26 @@
1
+ //#region src/logger.d.ts
2
+ type LogFunc = (message?: string, ...args: unknown[]) => void;
3
+ type LoggerOptions = {
4
+ readonly logStdout?: LogFunc | undefined;
5
+ readonly logStderr?: LogFunc | undefined;
6
+ readonly rootDir?: string | undefined;
7
+ readonly rootIsProjectRoot?: boolean | undefined;
8
+ readonly rootIsPackageRoot?: boolean | undefined;
9
+ };
10
+ declare class Logger {
11
+ private readonly _logStdout;
12
+ private readonly _logStderr;
13
+ private readonly _rootDir;
14
+ constructor(options?: LoggerOptions);
15
+ formatPath(path: string): string;
16
+ log(message?: string, ...args: unknown[]): void;
17
+ success(message?: string, ...args: unknown[]): void;
18
+ info(message?: string, ...args: unknown[]): void;
19
+ warning(message?: string, ...args: unknown[]): void;
20
+ error(message?: string, ...args: unknown[]): void;
21
+ list(items: string[]): void;
22
+ paths(paths: string[]): void;
23
+ }
24
+ //#endregion
25
+ export { LogFunc, Logger, LoggerOptions };
26
+ //# sourceMappingURL=index.d.mts.map
package/dist/index.mjs ADDED
@@ -0,0 +1,44 @@
1
+ import { findPackageRoot, findProjectRoot } from "@jpp-toolkit/utils";
2
+ import { blue, gray, green, red, yellow } from "colorette";
3
+
4
+ //#region src/logger.ts
5
+ var Logger = class {
6
+ _logStdout;
7
+ _logStderr;
8
+ _rootDir;
9
+ constructor(options = {}) {
10
+ this._logStdout = options.logStdout ?? console.log;
11
+ this._logStderr = options.logStderr ?? console.error;
12
+ this._rootDir = options.rootDir ?? (options.rootIsPackageRoot ? findPackageRoot() : options.rootIsProjectRoot ? findProjectRoot() : void 0);
13
+ }
14
+ formatPath(path) {
15
+ const root = this._rootDir ?? process.cwd();
16
+ const replaceWith = this._rootDir ? "<root>/" : "<cwd>/";
17
+ return path.replace(new RegExp(`^${root}/`, "u"), gray(replaceWith));
18
+ }
19
+ log(message, ...args) {
20
+ this._logStdout(message, ...args);
21
+ }
22
+ success(message, ...args) {
23
+ this._logStdout(`${green("✔︎")} ${message}`, ...args);
24
+ }
25
+ info(message, ...args) {
26
+ this._logStdout(`${blue("ℹ︎")} ${message}`, ...args);
27
+ }
28
+ warning(message, ...args) {
29
+ this._logStdout(`${yellow("⚠︎")} ${message}`, ...args);
30
+ }
31
+ error(message, ...args) {
32
+ this._logStderr(`${red("✘")} ${message}`, ...args);
33
+ }
34
+ list(items) {
35
+ for (const item of items) this._logStdout(` - ${item}`);
36
+ }
37
+ paths(paths) {
38
+ this.list(paths.map((p) => this.formatPath(p)));
39
+ }
40
+ };
41
+
42
+ //#endregion
43
+ export { Logger };
44
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../src/logger.ts"],"sourcesContent":["import { findPackageRoot, findProjectRoot } from '@jpp-toolkit/utils';\nimport { green, blue, yellow, red, gray } from 'colorette';\n\nexport type LogFunc = (message?: string, ...args: unknown[]) => void;\n\nexport type LoggerOptions = {\n readonly logStdout?: LogFunc | undefined;\n readonly logStderr?: LogFunc | undefined;\n readonly rootDir?: string | undefined;\n readonly rootIsProjectRoot?: boolean | undefined;\n readonly rootIsPackageRoot?: boolean | undefined;\n};\n\nexport class Logger {\n private readonly _logStdout: LogFunc;\n private readonly _logStderr: LogFunc;\n private readonly _rootDir: string | undefined;\n\n constructor(options: LoggerOptions = {}) {\n this._logStdout = options.logStdout ?? console.log;\n this._logStderr = options.logStderr ?? console.error;\n this._rootDir =\n options.rootDir\n ?? (options.rootIsPackageRoot ? findPackageRoot()\n : options.rootIsProjectRoot ? findProjectRoot()\n : undefined);\n }\n\n public formatPath(path: string): string {\n const root = this._rootDir ?? process.cwd();\n const replaceWith = this._rootDir ? '<root>/' : '<cwd>/';\n return path.replace(new RegExp(`^${root}/`, 'u'), gray(replaceWith));\n }\n\n public log(message?: string, ...args: unknown[]): void {\n this._logStdout(message, ...args);\n }\n\n public success(message?: string, ...args: unknown[]): void {\n this._logStdout(`${green('✔︎')} ${message}`, ...args);\n }\n\n public info(message?: string, ...args: unknown[]): void {\n this._logStdout(`${blue('ℹ︎')} ${message}`, ...args);\n }\n\n public warning(message?: string, ...args: unknown[]): void {\n this._logStdout(`${yellow('⚠︎')} ${message}`, ...args);\n }\n\n public error(message?: string, ...args: unknown[]): void {\n this._logStderr(`${red('✘')} ${message}`, ...args);\n }\n\n public list(items: string[]): void {\n for (const item of items) this._logStdout(` - ${item}`);\n }\n\n public paths(paths: string[]): void {\n this.list(paths.map((p) => this.formatPath(p)));\n }\n}\n"],"mappings":";;;;AAaA,IAAa,SAAb,MAAoB;CAChB,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CAEjB,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],
9
+ "homepage": "https://github.com/jpapini/jpp-toolkit/tree/main/packages/logger#readme",
10
+ "bugs": {
11
+ "url": "https://github.com/jpapini/jpp-toolkit/issues"
12
+ },
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/jpapini/jpp-toolkit.git",
16
+ "directory": "packages/logger"
17
+ },
18
+ "license": "MIT",
19
+ "author": "Julien Papini <julien.papini@gmail.com> (https://github.com/jpapini)",
20
+ "type": "module",
21
+ "exports": {
22
+ ".": {
23
+ "types": "./dist/index.d.mts",
24
+ "default": "./dist/index.mjs"
25
+ },
26
+ "./package.json": "./package.json"
27
+ },
28
+ "files": [
29
+ "dist",
30
+ "src"
31
+ ],
32
+ "dependencies": {
33
+ "colorette": "2.0.20",
34
+ "@jpp-toolkit/utils": "0.0.11"
35
+ },
36
+ "engines": {
37
+ "node": "24",
38
+ "pnpm": "10"
39
+ },
40
+ "volta": {
41
+ "extends": "../../package.json"
42
+ },
43
+ "publishConfig": {
44
+ "access": "public"
45
+ },
46
+ "scripts": {
47
+ "typecheck": "tsc --noEmit --pretty",
48
+ "dev": "build-lib --watch",
49
+ "build": "build-lib"
50
+ }
51
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './logger';
package/src/logger.ts ADDED
@@ -0,0 +1,62 @@
1
+ import { findPackageRoot, findProjectRoot } from '@jpp-toolkit/utils';
2
+ import { green, blue, yellow, red, gray } from 'colorette';
3
+
4
+ export type LogFunc = (message?: string, ...args: unknown[]) => void;
5
+
6
+ export type LoggerOptions = {
7
+ readonly logStdout?: LogFunc | undefined;
8
+ readonly logStderr?: LogFunc | undefined;
9
+ readonly rootDir?: string | undefined;
10
+ readonly rootIsProjectRoot?: boolean | undefined;
11
+ readonly rootIsPackageRoot?: boolean | undefined;
12
+ };
13
+
14
+ export class Logger {
15
+ private readonly _logStdout: LogFunc;
16
+ private readonly _logStderr: LogFunc;
17
+ private readonly _rootDir: string | undefined;
18
+
19
+ constructor(options: LoggerOptions = {}) {
20
+ this._logStdout = options.logStdout ?? console.log;
21
+ this._logStderr = options.logStderr ?? console.error;
22
+ this._rootDir =
23
+ options.rootDir
24
+ ?? (options.rootIsPackageRoot ? findPackageRoot()
25
+ : options.rootIsProjectRoot ? findProjectRoot()
26
+ : undefined);
27
+ }
28
+
29
+ public formatPath(path: string): string {
30
+ const root = this._rootDir ?? process.cwd();
31
+ const replaceWith = this._rootDir ? '<root>/' : '<cwd>/';
32
+ return path.replace(new RegExp(`^${root}/`, 'u'), gray(replaceWith));
33
+ }
34
+
35
+ public log(message?: string, ...args: unknown[]): void {
36
+ this._logStdout(message, ...args);
37
+ }
38
+
39
+ public success(message?: string, ...args: unknown[]): void {
40
+ this._logStdout(`${green('✔︎')} ${message}`, ...args);
41
+ }
42
+
43
+ public info(message?: string, ...args: unknown[]): void {
44
+ this._logStdout(`${blue('ℹ︎')} ${message}`, ...args);
45
+ }
46
+
47
+ public warning(message?: string, ...args: unknown[]): void {
48
+ this._logStdout(`${yellow('⚠︎')} ${message}`, ...args);
49
+ }
50
+
51
+ public error(message?: string, ...args: unknown[]): void {
52
+ this._logStderr(`${red('✘')} ${message}`, ...args);
53
+ }
54
+
55
+ public list(items: string[]): void {
56
+ for (const item of items) this._logStdout(` - ${item}`);
57
+ }
58
+
59
+ public paths(paths: string[]): void {
60
+ this.list(paths.map((p) => this.formatPath(p)));
61
+ }
62
+ }