@node-cli/logger 1.0.0 → 1.1.0
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/dist/Logger.d.ts +11 -0
- package/dist/Logger.js +30 -0
- package/dist/Logger.js.map +1 -1
- package/package.json +3 -2
package/dist/Logger.d.ts
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
import { Options as BoxenOptions } from "boxen";
|
|
2
|
+
export type PrintBoxOptions = {
|
|
3
|
+
newLineAfter?: boolean;
|
|
4
|
+
newLineBefore?: boolean;
|
|
5
|
+
} & BoxenOptions;
|
|
1
6
|
export declare class Logger {
|
|
2
7
|
#private;
|
|
3
8
|
constructor({ boring, silent, prefix, timestamp, }?: {
|
|
@@ -21,4 +26,10 @@ export declare class Logger {
|
|
|
21
26
|
* @param {number} [exitStatus] the process will exit with this value if provided
|
|
22
27
|
*/
|
|
23
28
|
printErrorsAndExit(errorMessages: string[], exitStatus?: number): void;
|
|
29
|
+
/**
|
|
30
|
+
* Print sets of logs in a box (wrapper to Boxen)
|
|
31
|
+
* @param messages Messages to print
|
|
32
|
+
* @param options
|
|
33
|
+
*/
|
|
34
|
+
printBox(messages: string | string[], options?: PrintBoxOptions): void;
|
|
24
35
|
}
|
package/dist/Logger.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import boxen from "boxen";
|
|
1
2
|
import kleur from "kleur";
|
|
2
3
|
import util from "node:util";
|
|
3
4
|
export class Logger {
|
|
@@ -92,6 +93,35 @@ export class Logger {
|
|
|
92
93
|
}
|
|
93
94
|
}
|
|
94
95
|
}
|
|
96
|
+
/**
|
|
97
|
+
* Print sets of logs in a box (wrapper to Boxen)
|
|
98
|
+
* @param messages Messages to print
|
|
99
|
+
* @param options
|
|
100
|
+
*/ printBox(messages, options = {}) {
|
|
101
|
+
const { newLineAfter , newLineBefore } = {
|
|
102
|
+
newLineAfter: true,
|
|
103
|
+
newLineBefore: true,
|
|
104
|
+
...options
|
|
105
|
+
};
|
|
106
|
+
/**
|
|
107
|
+
* Setting some sensible Boxen options if
|
|
108
|
+
* not provided by the user.
|
|
109
|
+
*/ const boxenOptions = {
|
|
110
|
+
...options,
|
|
111
|
+
borderColor: options.borderColor || (this.#printOptions.colors ? "yellow" : "white"),
|
|
112
|
+
padding: typeof options.padding === "number" ? options.padding : 1,
|
|
113
|
+
textAlignment: options.textAlignment || "center"
|
|
114
|
+
};
|
|
115
|
+
const oldPrefix = this.#globalPrefix;
|
|
116
|
+
const oldTimestamp = this.#showTimestamp;
|
|
117
|
+
this.#globalPrefix = "";
|
|
118
|
+
this.#showTimestamp = false;
|
|
119
|
+
newLineBefore && this.log();
|
|
120
|
+
this.log(boxen(typeof messages === "string" ? messages : messages.join("\n"), boxenOptions));
|
|
121
|
+
newLineAfter && this.log();
|
|
122
|
+
this.#showTimestamp = oldTimestamp;
|
|
123
|
+
this.#globalPrefix = oldPrefix;
|
|
124
|
+
}
|
|
95
125
|
}
|
|
96
126
|
|
|
97
127
|
//# sourceMappingURL=Logger.js.map
|
package/dist/Logger.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/Logger.ts"],"sourcesContent":["import kleur from \"kleur\";\nimport util from \"node:util\";\n\nexport class Logger {\n\t#shouldLog: boolean;\n\t#globalPrefix: string;\n\t#showTimestamp: boolean;\n\t#printOptions: { colors: boolean; compact: boolean; depth: number };\n\n\tconstructor({\n\t\tboring = false,\n\t\tsilent = false,\n\t\tprefix = \"\",\n\t\ttimestamp = false,\n\t} = {}) {\n\t\tthis.#shouldLog = !silent;\n\t\tthis.#globalPrefix = prefix;\n\t\tthis.#showTimestamp = timestamp;\n\t\tthis.#printOptions = {\n\t\t\tcolors: !boring,\n\t\t\tcompact: false,\n\t\t\tdepth: 5,\n\t\t};\n\t}\n\n\tset silent(flag: boolean) {\n\t\tthis.#shouldLog = !flag;\n\t}\n\n\tset boring(flag: boolean) {\n\t\tthis.#printOptions.colors = !flag;\n\t}\n\n\tset prefix(prefix: string) {\n\t\tthis.#globalPrefix = prefix;\n\t}\n\n\tset timestamp(flag: boolean) {\n\t\tthis.#showTimestamp = flag;\n\t}\n\n\t#_log(\n\t\ttype: { method: string | number; color: (argument0: any) => any },\n\t\t...arguments_: string[]\n\t) {\n\t\tif (this.#shouldLog) {\n\t\t\tlet message: string;\n\t\t\tif (!this.#showTimestamp && !this.#globalPrefix) {\n\t\t\t\tmessage = util.formatWithOptions(this.#printOptions, ...arguments_);\n\t\t\t} else {\n\t\t\t\tconst prefix = this.#globalPrefix ? [this.#globalPrefix] : [];\n\t\t\t\tif (this.#showTimestamp) {\n\t\t\t\t\tconst now = new Date();\n\t\t\t\t\tprefix.push(\n\t\t\t\t\t\tthis.#printOptions.colors\n\t\t\t\t\t\t\t? `${kleur.grey(\n\t\t\t\t\t\t\t\t\t`[ ${now.toDateString()} ${now.toLocaleTimeString()} ]`\n\t\t\t\t\t\t\t )}`\n\t\t\t\t\t\t\t: `[ ${now.toDateString()} ${now.toLocaleTimeString()} ]`\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tmessage = util.formatWithOptions(\n\t\t\t\t\tthis.#printOptions,\n\t\t\t\t\tprefix.join(\" \"),\n\t\t\t\t\t...arguments_\n\t\t\t\t);\n\t\t\t}\n\t\t\tconsole[type.method](\n\t\t\t\tthis.#printOptions.colors ? `${type.color(message)}` : message\n\t\t\t);\n\t\t}\n\t}\n\n\tinfo(...arguments_: any) {\n\t\tthis.#_log({ method: \"info\", color: kleur.blue }, ...arguments_);\n\t}\n\n\tlog(...arguments_: any) {\n\t\tthis.#_log({ method: \"log\", color: kleur.white }, ...arguments_);\n\t}\n\n\tdebug(...arguments_: any) {\n\t\tthis.#_log({ method: \"debug\", color: kleur.grey }, ...arguments_);\n\t}\n\n\twarn(...arguments_: any) {\n\t\tthis.#_log({ method: \"warn\", color: kleur.yellow }, ...arguments_);\n\t}\n\n\terror(...arguments_: any) {\n\t\tthis.#_log({ method: \"error\", color: kleur.red }, ...arguments_);\n\t}\n\n\t/**\n\t * Log multiple error messages at the prompt using `console.error` behind the scenes.\n\t * @param {string[]} errorMessages array of error message to display line by line\n\t * @param {number} [exitStatus] the process will exit with this value if provided\n\t */\n\tprintErrorsAndExit(errorMessages: string[], exitStatus?: number) {\n\t\tif (errorMessages && errorMessages.length > 0) {\n\t\t\tthis.log();\n\t\t\tfor (const message of errorMessages) {\n\t\t\t\tthis.error(message);\n\t\t\t}\n\t\t\tthis.log();\n\n\t\t\tif (typeof exitStatus === \"number\") {\n\t\t\t\t// eslint-disable-next-line unicorn/no-process-exit\n\t\t\t\tprocess.exit(exitStatus);\n\t\t\t}\n\t\t}\n\t}\n}\n"],"names":["kleur","util","Logger","shouldLog","globalPrefix","showTimestamp","printOptions","constructor","boring","silent","prefix","timestamp","colors","compact","depth","flag","_log","type","arguments_","message","formatWithOptions","now","Date","push","grey","toDateString","toLocaleTimeString","join","console","method","color","info","blue","log","white","debug","warn","yellow","error","red","printErrorsAndExit","errorMessages","exitStatus","length","process","exit"],"mappings":"AAAA,OAAOA,WAAW,QAAQ;AAC1B,OAAOC,UAAU,YAAY;
|
|
1
|
+
{"version":3,"sources":["../src/Logger.ts"],"sourcesContent":["import boxen, { Options as BoxenOptions } from \"boxen\";\n\nimport kleur from \"kleur\";\nimport util from \"node:util\";\n\nexport type PrintBoxOptions = {\n\tnewLineAfter?: boolean;\n\tnewLineBefore?: boolean;\n} & BoxenOptions;\n\nexport class Logger {\n\t#shouldLog: boolean;\n\t#globalPrefix: string;\n\t#showTimestamp: boolean;\n\t#printOptions: { colors: boolean; compact: boolean; depth: number };\n\n\tconstructor({\n\t\tboring = false,\n\t\tsilent = false,\n\t\tprefix = \"\",\n\t\ttimestamp = false,\n\t} = {}) {\n\t\tthis.#shouldLog = !silent;\n\t\tthis.#globalPrefix = prefix;\n\t\tthis.#showTimestamp = timestamp;\n\t\tthis.#printOptions = {\n\t\t\tcolors: !boring,\n\t\t\tcompact: false,\n\t\t\tdepth: 5,\n\t\t};\n\t}\n\n\tset silent(flag: boolean) {\n\t\tthis.#shouldLog = !flag;\n\t}\n\n\tset boring(flag: boolean) {\n\t\tthis.#printOptions.colors = !flag;\n\t}\n\n\tset prefix(prefix: string) {\n\t\tthis.#globalPrefix = prefix;\n\t}\n\n\tset timestamp(flag: boolean) {\n\t\tthis.#showTimestamp = flag;\n\t}\n\n\t#_log(\n\t\ttype: { method: string | number; color: (argument0: any) => any },\n\t\t...arguments_: string[]\n\t) {\n\t\tif (this.#shouldLog) {\n\t\t\tlet message: string;\n\t\t\tif (!this.#showTimestamp && !this.#globalPrefix) {\n\t\t\t\tmessage = util.formatWithOptions(this.#printOptions, ...arguments_);\n\t\t\t} else {\n\t\t\t\tconst prefix = this.#globalPrefix ? [this.#globalPrefix] : [];\n\t\t\t\tif (this.#showTimestamp) {\n\t\t\t\t\tconst now = new Date();\n\t\t\t\t\tprefix.push(\n\t\t\t\t\t\tthis.#printOptions.colors\n\t\t\t\t\t\t\t? `${kleur.grey(\n\t\t\t\t\t\t\t\t\t`[ ${now.toDateString()} ${now.toLocaleTimeString()} ]`\n\t\t\t\t\t\t\t )}`\n\t\t\t\t\t\t\t: `[ ${now.toDateString()} ${now.toLocaleTimeString()} ]`\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tmessage = util.formatWithOptions(\n\t\t\t\t\tthis.#printOptions,\n\t\t\t\t\tprefix.join(\" \"),\n\t\t\t\t\t...arguments_\n\t\t\t\t);\n\t\t\t}\n\t\t\tconsole[type.method](\n\t\t\t\tthis.#printOptions.colors ? `${type.color(message)}` : message\n\t\t\t);\n\t\t}\n\t}\n\n\tinfo(...arguments_: any) {\n\t\tthis.#_log({ method: \"info\", color: kleur.blue }, ...arguments_);\n\t}\n\n\tlog(...arguments_: any) {\n\t\tthis.#_log({ method: \"log\", color: kleur.white }, ...arguments_);\n\t}\n\n\tdebug(...arguments_: any) {\n\t\tthis.#_log({ method: \"debug\", color: kleur.grey }, ...arguments_);\n\t}\n\n\twarn(...arguments_: any) {\n\t\tthis.#_log({ method: \"warn\", color: kleur.yellow }, ...arguments_);\n\t}\n\n\terror(...arguments_: any) {\n\t\tthis.#_log({ method: \"error\", color: kleur.red }, ...arguments_);\n\t}\n\n\t/**\n\t * Log multiple error messages at the prompt using `console.error` behind the scenes.\n\t * @param {string[]} errorMessages array of error message to display line by line\n\t * @param {number} [exitStatus] the process will exit with this value if provided\n\t */\n\tprintErrorsAndExit(errorMessages: string[], exitStatus?: number) {\n\t\tif (errorMessages && errorMessages.length > 0) {\n\t\t\tthis.log();\n\t\t\tfor (const message of errorMessages) {\n\t\t\t\tthis.error(message);\n\t\t\t}\n\t\t\tthis.log();\n\n\t\t\tif (typeof exitStatus === \"number\") {\n\t\t\t\t// eslint-disable-next-line unicorn/no-process-exit\n\t\t\t\tprocess.exit(exitStatus);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Print sets of logs in a box (wrapper to Boxen)\n\t * @param messages Messages to print\n\t * @param options\n\t */\n\tprintBox(messages: string | string[], options: PrintBoxOptions = {}) {\n\t\tconst { newLineAfter, newLineBefore } = {\n\t\t\tnewLineAfter: true,\n\t\t\tnewLineBefore: true,\n\t\t\t...options,\n\t\t};\n\n\t\t/**\n\t\t * Setting some sensible Boxen options if\n\t\t * not provided by the user.\n\t\t */\n\t\tconst boxenOptions: BoxenOptions = {\n\t\t\t...options,\n\t\t\tborderColor:\n\t\t\t\toptions.borderColor || (this.#printOptions.colors ? \"yellow\" : \"white\"),\n\t\t\tpadding: typeof options.padding === \"number\" ? options.padding : 1,\n\t\t\ttextAlignment: options.textAlignment || \"center\",\n\t\t};\n\n\t\tconst oldPrefix = this.#globalPrefix;\n\t\tconst oldTimestamp = this.#showTimestamp;\n\n\t\tthis.#globalPrefix = \"\";\n\t\tthis.#showTimestamp = false;\n\n\t\tnewLineBefore && this.log();\n\t\tthis.log(\n\t\t\tboxen(\n\t\t\t\ttypeof messages === \"string\" ? messages : messages.join(\"\\n\"),\n\t\t\t\tboxenOptions\n\t\t\t)\n\t\t);\n\t\tnewLineAfter && this.log();\n\n\t\tthis.#showTimestamp = oldTimestamp;\n\t\tthis.#globalPrefix = oldPrefix;\n\t}\n}\n"],"names":["boxen","kleur","util","Logger","shouldLog","globalPrefix","showTimestamp","printOptions","constructor","boring","silent","prefix","timestamp","colors","compact","depth","flag","_log","type","arguments_","message","formatWithOptions","now","Date","push","grey","toDateString","toLocaleTimeString","join","console","method","color","info","blue","log","white","debug","warn","yellow","error","red","printErrorsAndExit","errorMessages","exitStatus","length","process","exit","printBox","messages","options","newLineAfter","newLineBefore","boxenOptions","borderColor","padding","textAlignment","oldPrefix","oldTimestamp"],"mappings":"AAAA,OAAOA,WAAwC,QAAQ;AAEvD,OAAOC,WAAW,QAAQ;AAC1B,OAAOC,UAAU,YAAY;AAO7B,OAAO,MAAMC;IACZ,CAACC,SAAS,CAAU;IACpB,CAACC,YAAY,CAAS;IACtB,CAACC,aAAa,CAAU;IACxB,CAACC,YAAY,CAAuD;IAEpEC,YAAY,EACXC,QAAS,MAAK,EACdC,QAAS,MAAK,EACdC,QAAS,GAAE,EACXC,WAAY,MAAK,EACjB,GAAG,CAAC,CAAC,CAAE;QACP,IAAI,CAAC,CAACR,SAAS,GAAG,CAACM;QACnB,IAAI,CAAC,CAACL,YAAY,GAAGM;QACrB,IAAI,CAAC,CAACL,aAAa,GAAGM;QACtB,IAAI,CAAC,CAACL,YAAY,GAAG;YACpBM,QAAQ,CAACJ;YACTK,SAAS;YACTC,OAAO;QACR;IACD;IAEA,IAAIL,OAAOM,IAAa,EAAE;QACzB,IAAI,CAAC,CAACZ,SAAS,GAAG,CAACY;IACpB;IAEA,IAAIP,OAAOO,IAAa,EAAE;QACzB,IAAI,CAAC,CAACT,YAAY,CAACM,SAAS,CAACG;IAC9B;IAEA,IAAIL,OAAOA,MAAc,EAAE;QAC1B,IAAI,CAAC,CAACN,YAAY,GAAGM;IACtB;IAEA,IAAIC,UAAUI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AG;QAEtB6C,iBAAiB,IAAI,CAACjB;QACtB,IAAI,CAACA,IACJlC,MACC,OAAOgD,aAAa,WAAWA,WAAWA,SAASpB,KAAK,OACxDwB;QAGFF,gBAAgB,IAAI,CAAChB;QAErB,IAAI,CAAC,CAAC5B,aAAa,GAAGmD;QACtB,IAAI,CAAC,CAACpD,YAAY,GAAGmD;IACtB;AACD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@node-cli/logger",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Arno Versini",
|
|
6
6
|
"description": "A tiny console logger for nodejs CLI apps",
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
],
|
|
12
12
|
"node": ">=16",
|
|
13
13
|
"dependencies": {
|
|
14
|
+
"boxen": "7.1.0",
|
|
14
15
|
"kleur": "4.1.5"
|
|
15
16
|
},
|
|
16
17
|
"scripts": {
|
|
@@ -27,5 +28,5 @@
|
|
|
27
28
|
"publishConfig": {
|
|
28
29
|
"access": "public"
|
|
29
30
|
},
|
|
30
|
-
"gitHead": "
|
|
31
|
+
"gitHead": "d1d89854e385600edfd9c2aead7c9bc568bcc657"
|
|
31
32
|
}
|