@node-cli/logger 1.2.6 → 1.3.1
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/README.md +295 -160
- package/dist/Logger.d.ts +19 -1
- package/dist/Logger.js +38 -6
- package/dist/Logger.js.map +1 -1
- package/package.json +11 -6
package/README.md
CHANGED
|
@@ -1,249 +1,384 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @node-cli/logger
|
|
2
2
|
|
|
3
3
|

|
|
4
4
|
|
|
5
|
-
>
|
|
5
|
+
> A powerful, flexible console logger for Node.js command-line applications with rich formatting options and in-memory logging capabilities.
|
|
6
6
|
|
|
7
7
|
## Installation
|
|
8
8
|
|
|
9
9
|
```sh
|
|
10
|
-
|
|
11
|
-
> npm install --save-dev @node-cli/logger
|
|
10
|
+
npm install --save-dev @node-cli/logger
|
|
12
11
|
```
|
|
13
12
|
|
|
14
|
-
|
|
13
|
+
## Overview
|
|
15
14
|
|
|
16
|
-
-
|
|
17
|
-
- `Spinner` is an "elegant terminal spinner", relying behind the scenes on the excellent [ora](https://github.com/sindresorhus/ora)
|
|
15
|
+
`@node-cli/logger` provides two main classes:
|
|
18
16
|
|
|
19
|
-
|
|
17
|
+
- **`Logger`**: A versatile console logger with enhanced formatting, colors, and utility methods
|
|
18
|
+
- **`Spinner`**: An elegant terminal spinner for displaying progress, based on [ora](https://github.com/sindresorhus/ora)
|
|
19
|
+
|
|
20
|
+
## Basic Usage
|
|
21
|
+
|
|
22
|
+
### Logger
|
|
20
23
|
|
|
21
24
|
```js
|
|
22
25
|
import { Logger } from "@node-cli/logger";
|
|
26
|
+
|
|
27
|
+
// Create a new logger instance
|
|
23
28
|
const log = new Logger();
|
|
24
29
|
|
|
25
|
-
|
|
26
|
-
log.
|
|
27
|
-
log.
|
|
30
|
+
// Basic logging methods
|
|
31
|
+
log.info("This is an informational message");
|
|
32
|
+
log.warn("This is a warning message");
|
|
33
|
+
log.error("This is an error message");
|
|
34
|
+
log.debug("This is a debug message");
|
|
35
|
+
log.log("This is a standard log message");
|
|
28
36
|
```
|
|
29
37
|
|
|
38
|
+
### Spinner
|
|
39
|
+
|
|
30
40
|
```js
|
|
31
41
|
import { Spinner } from "@node-cli/logger";
|
|
32
|
-
const spinner = new Spinner("Updating package.json...");
|
|
33
|
-
|
|
34
|
-
// assuming a long running process here...
|
|
35
|
-
spinner.text = "Git stage and commit, please wait...";
|
|
36
|
-
// assuming a long running process here...
|
|
37
|
-
spinner.text = "Almost there...";
|
|
38
|
-
// assuming a long running process here... returning some result
|
|
39
|
-
if (result === "success") {
|
|
40
|
-
spinner.stop("Process completed successfully!");
|
|
41
|
-
} else {
|
|
42
|
-
spinner.stop("Process failed miserably...", Spinner.ERROR);
|
|
43
|
-
}
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
## API
|
|
47
42
|
|
|
48
|
-
|
|
43
|
+
// Create a new spinner with initial text
|
|
44
|
+
const spinner = new Spinner("Processing files...");
|
|
49
45
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
| constructor | options | Initialize a Spinner instance with [ora](https://github.com/sindresorhus/ora) options |
|
|
53
|
-
| start | text | Starts the spinner on the terminal and append a string to it |
|
|
54
|
-
| stop | text, status | Stop the spinner, change it to a green, red, yellow or blue marker, and persist the current text, or text if provided. The argument `status` can be one of `Spinner.SUCCESS`, `Spinner.ERROR`, `Spinner.WARNING` or `Spinner.INFO` |
|
|
46
|
+
// Start the spinner
|
|
47
|
+
spinner.start();
|
|
55
48
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
| text | Set the text of the spinner. If the spinner is stopped, the text will be persisted. |
|
|
49
|
+
// Update the spinner text during a long-running process
|
|
50
|
+
spinner.text = "Analyzing data...";
|
|
59
51
|
|
|
60
|
-
|
|
52
|
+
// Complete the spinner with different statuses
|
|
53
|
+
spinner.stop("Process completed successfully!"); // Success (default)
|
|
54
|
+
// or
|
|
55
|
+
spinner.stop("Process failed!", Spinner.ERROR); // Error
|
|
56
|
+
// or
|
|
57
|
+
spinner.stop("Process needs attention", Spinner.WARNING); // Warning
|
|
58
|
+
// or
|
|
59
|
+
spinner.stop("Process information", Spinner.INFO); // Info
|
|
60
|
+
```
|
|
61
61
|
|
|
62
|
-
|
|
62
|
+
## Advanced Logger Examples
|
|
63
63
|
|
|
64
|
-
|
|
65
|
-
| ------------------ | --------------------------------------------------------- | ------------ |
|
|
66
|
-
| debug | Outputs a message to the console with the log level debug | grey |
|
|
67
|
-
| log | For general output of logging information. | white |
|
|
68
|
-
| info | Informative logging of information. | blue |
|
|
69
|
-
| warn | Outputs a message to the console with the log level debug | yellow |
|
|
70
|
-
| error | Outputs an error message. | red |
|
|
71
|
-
| printBox | Output message(s) in a box | custom |
|
|
72
|
-
| printErrorsAndExit | Output error message(s) and exit | red |
|
|
64
|
+
### Customizing Logger Output
|
|
73
65
|
|
|
74
|
-
|
|
66
|
+
```js
|
|
67
|
+
// Initialize with options
|
|
68
|
+
const log = new Logger({
|
|
69
|
+
boring: false, // Enable colors (default)
|
|
70
|
+
silent: false, // Enable logging (default)
|
|
71
|
+
prefix: "MyApp:", // Add a prefix to all logs
|
|
72
|
+
timestamp: true, // Add timestamps to logs
|
|
73
|
+
inMemory: false // Log to console (default)
|
|
74
|
+
});
|
|
75
75
|
|
|
76
|
-
|
|
76
|
+
// Log with different levels
|
|
77
|
+
log.info("Starting application...");
|
|
78
|
+
log.debug("Debug information");
|
|
79
|
+
log.warn("Warning: configuration file not found");
|
|
80
|
+
log.error("Error: failed to connect to database");
|
|
81
|
+
|
|
82
|
+
// Output:
|
|
83
|
+
// MyApp: [ Tue Mar 18 2025 10:30:00 AM ] Starting application...
|
|
84
|
+
// MyApp: [ Tue Mar 18 2025 10:30:01 AM ] Debug information
|
|
85
|
+
// MyApp: [ Tue Mar 18 2025 10:30:02 AM ] Warning: configuration file not found
|
|
86
|
+
// MyApp: [ Tue Mar 18 2025 10:30:03 AM ] Error: failed to connect to database
|
|
87
|
+
```
|
|
77
88
|
|
|
78
|
-
|
|
89
|
+
### Dynamic Configuration
|
|
79
90
|
|
|
80
91
|
```js
|
|
81
|
-
import { Logger } from "@node-cli/logger";
|
|
82
92
|
const log = new Logger();
|
|
83
93
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
log.silent = process.env.NODE_ENV === "production";
|
|
87
|
-
log.info("but this will not");
|
|
88
|
-
log.silent = false;
|
|
89
|
-
log.info("this will be logged again!");
|
|
90
|
-
```
|
|
94
|
+
// Change configuration at runtime
|
|
95
|
+
log.info("Normal colored output");
|
|
91
96
|
|
|
92
|
-
|
|
97
|
+
// Disable colors for test environments
|
|
98
|
+
log.boring = process.env.NODE_ENV === "test";
|
|
99
|
+
log.info("No colors in test environment");
|
|
93
100
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
101
|
+
// Add a prefix for specific sections
|
|
102
|
+
log.prefix = "[CONFIG]";
|
|
103
|
+
log.info("Loading configuration..."); // Output: [CONFIG] Loading configuration...
|
|
97
104
|
|
|
98
|
-
|
|
105
|
+
// Add timestamps for debugging
|
|
106
|
+
log.timestamp = true;
|
|
107
|
+
log.debug("Detailed timing information"); // Output: [CONFIG] [ Tue Mar 18 2025 10:30:00 AM ] Detailed timing information
|
|
108
|
+
|
|
109
|
+
// Silence logs temporarily
|
|
110
|
+
log.silent = true;
|
|
111
|
+
log.info("This won't be displayed");
|
|
112
|
+
|
|
113
|
+
// Re-enable logs
|
|
99
114
|
log.silent = false;
|
|
100
|
-
log.info("
|
|
115
|
+
log.info("Logging resumed");
|
|
101
116
|
```
|
|
102
117
|
|
|
103
|
-
###
|
|
104
|
-
|
|
105
|
-
You can disable colors with `boring`:
|
|
118
|
+
### Printing Messages in a Box
|
|
106
119
|
|
|
107
120
|
```js
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
121
|
+
// Simple box with default options
|
|
122
|
+
log.printBox("Important Message");
|
|
123
|
+
/*
|
|
124
|
+
┌─────────────────────┐
|
|
125
|
+
│ │
|
|
126
|
+
│ Important Message │
|
|
127
|
+
│ │
|
|
128
|
+
└─────────────────────┘
|
|
129
|
+
*/
|
|
130
|
+
|
|
131
|
+
// Multiple messages in a box
|
|
132
|
+
log.printBox(["Welcome to MyApp v1.2.3", "Copyright © 2025"]);
|
|
133
|
+
/*
|
|
134
|
+
┌──────────────────────────┐
|
|
135
|
+
│ │
|
|
136
|
+
│ Welcome to MyApp v1.2.3 │
|
|
137
|
+
│ Copyright © 2025 │
|
|
138
|
+
│ │
|
|
139
|
+
└──────────────────────────┘
|
|
140
|
+
*/
|
|
141
|
+
|
|
142
|
+
// Customized box
|
|
143
|
+
log.printBox("WARNING: Disk space low", {
|
|
144
|
+
borderColor: "red",
|
|
145
|
+
padding: 2,
|
|
146
|
+
textAlignment: "center",
|
|
147
|
+
title: "System Alert",
|
|
148
|
+
newLineAfter: true,
|
|
149
|
+
newLineBefore: true
|
|
150
|
+
});
|
|
151
|
+
/*
|
|
152
|
+
┌─ System Alert ───────────────┐
|
|
153
|
+
│ │
|
|
154
|
+
│ │
|
|
155
|
+
│ WARNING: Disk space low │
|
|
156
|
+
│ │
|
|
157
|
+
│ │
|
|
158
|
+
└──────────────────────────────┘
|
|
159
|
+
*/
|
|
117
160
|
```
|
|
118
161
|
|
|
119
|
-
|
|
162
|
+
### Error Handling and Process Exit
|
|
120
163
|
|
|
121
164
|
```js
|
|
122
|
-
|
|
123
|
-
const
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
165
|
+
// Display multiple errors and exit the process
|
|
166
|
+
const errors = [
|
|
167
|
+
"Failed to connect to database",
|
|
168
|
+
"Configuration file is invalid",
|
|
169
|
+
"Required environment variables missing"
|
|
170
|
+
];
|
|
171
|
+
|
|
172
|
+
// Display errors and exit with code 1
|
|
173
|
+
log.printErrorsAndExit(errors, 1);
|
|
174
|
+
|
|
175
|
+
// Or just display errors without exiting
|
|
176
|
+
log.printErrorsAndExit(errors);
|
|
128
177
|
```
|
|
129
178
|
|
|
130
|
-
###
|
|
131
|
-
|
|
132
|
-
You can add a prefix to the logs with `prefix`:
|
|
179
|
+
### In-Memory Logging
|
|
133
180
|
|
|
134
181
|
```js
|
|
135
|
-
|
|
182
|
+
// Create a logger that stores logs in memory instead of console
|
|
183
|
+
const memoryLogger = new Logger({ inMemory: true });
|
|
184
|
+
|
|
185
|
+
// Log messages that are stored in memory
|
|
186
|
+
memoryLogger.info("Starting process");
|
|
187
|
+
memoryLogger.warn("Resource usage high");
|
|
188
|
+
memoryLogger.error("Process failed");
|
|
189
|
+
|
|
190
|
+
// Retrieve all logs as a string
|
|
191
|
+
const logs = memoryLogger.getMemoryLogs();
|
|
192
|
+
console.log(logs);
|
|
193
|
+
// Output:
|
|
194
|
+
// Starting process
|
|
195
|
+
// Resource usage high
|
|
196
|
+
// Process failed
|
|
197
|
+
|
|
198
|
+
// Clear memory logs when no longer needed
|
|
199
|
+
memoryLogger.clearMemoryLogs();
|
|
200
|
+
|
|
201
|
+
// Toggle between console and memory logging
|
|
136
202
|
const log = new Logger();
|
|
203
|
+
log.info("This goes to console");
|
|
137
204
|
|
|
138
|
-
log.
|
|
139
|
-
log.
|
|
140
|
-
log.info("this will have a prefix!");
|
|
141
|
-
```
|
|
205
|
+
log.inMemory = true;
|
|
206
|
+
log.info("This goes to memory only");
|
|
142
207
|
|
|
143
|
-
|
|
208
|
+
// Get only the in-memory logs
|
|
209
|
+
const memLogs = log.getMemoryLogs(); // Contains only "This goes to memory only"
|
|
144
210
|
|
|
145
|
-
|
|
146
|
-
|
|
211
|
+
log.inMemory = false;
|
|
212
|
+
log.info("Back to console logging");
|
|
147
213
|
```
|
|
148
214
|
|
|
149
|
-
|
|
215
|
+
## Advanced Spinner Examples
|
|
216
|
+
|
|
217
|
+
### Spinner with Custom Options
|
|
150
218
|
|
|
151
219
|
```js
|
|
152
|
-
|
|
153
|
-
const
|
|
220
|
+
// Create a spinner with custom options
|
|
221
|
+
const spinner = new Spinner({
|
|
222
|
+
text: "Processing...",
|
|
223
|
+
color: "blue",
|
|
224
|
+
spinner: "dots" // Use the 'dots' spinner pattern
|
|
225
|
+
});
|
|
154
226
|
|
|
155
|
-
|
|
156
|
-
log.prefix = false;
|
|
157
|
-
log.info("this will be NOT be logged with a prefix");
|
|
227
|
+
spinner.start();
|
|
158
228
|
```
|
|
159
229
|
|
|
160
|
-
###
|
|
161
|
-
|
|
162
|
-
You can add a timestamp to the logs with `timestamp`:
|
|
230
|
+
### Spinner in Async Functions
|
|
163
231
|
|
|
164
232
|
```js
|
|
165
|
-
import {
|
|
166
|
-
const log = new Logger();
|
|
233
|
+
import { Spinner } from "@node-cli/logger";
|
|
167
234
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
235
|
+
async function deployApplication() {
|
|
236
|
+
const spinner = new Spinner("Preparing deployment...");
|
|
237
|
+
spinner.start();
|
|
238
|
+
|
|
239
|
+
try {
|
|
240
|
+
// Building phase
|
|
241
|
+
spinner.text = "Building application...";
|
|
242
|
+
await buildApp();
|
|
243
|
+
|
|
244
|
+
// Testing phase
|
|
245
|
+
spinner.text = "Running tests...";
|
|
246
|
+
await runTests();
|
|
247
|
+
|
|
248
|
+
// Deployment phase
|
|
249
|
+
spinner.text = "Deploying to production...";
|
|
250
|
+
await deploy();
|
|
251
|
+
|
|
252
|
+
// Success
|
|
253
|
+
spinner.stop("Deployment completed successfully!");
|
|
254
|
+
return true;
|
|
255
|
+
} catch (error) {
|
|
256
|
+
// Handle failure
|
|
257
|
+
spinner.stop(`Deployment failed: ${error.message}`, Spinner.ERROR);
|
|
258
|
+
return false;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
171
261
|
```
|
|
172
262
|
|
|
173
|
-
|
|
263
|
+
### Chaining Multiple Spinners
|
|
174
264
|
|
|
175
|
-
```
|
|
176
|
-
|
|
265
|
+
```js
|
|
266
|
+
import { Spinner } from "@node-cli/logger";
|
|
267
|
+
|
|
268
|
+
async function processWorkflow() {
|
|
269
|
+
// Step 1
|
|
270
|
+
const step1 = new Spinner("Step 1: Data validation");
|
|
271
|
+
step1.start();
|
|
272
|
+
await validateData();
|
|
273
|
+
step1.stop("Data validation complete", Spinner.SUCCESS);
|
|
274
|
+
|
|
275
|
+
// Step 2
|
|
276
|
+
const step2 = new Spinner("Step 2: Processing records");
|
|
277
|
+
step2.start();
|
|
278
|
+
await processRecords();
|
|
279
|
+
step2.stop("Records processed", Spinner.SUCCESS);
|
|
280
|
+
|
|
281
|
+
// Step 3
|
|
282
|
+
const step3 = new Spinner("Step 3: Generating report");
|
|
283
|
+
step3.start();
|
|
284
|
+
await generateReport();
|
|
285
|
+
step3.stop("Report generated", Spinner.SUCCESS);
|
|
286
|
+
|
|
287
|
+
console.log("Workflow completed successfully!");
|
|
288
|
+
}
|
|
177
289
|
```
|
|
178
290
|
|
|
179
|
-
|
|
291
|
+
## API Reference
|
|
180
292
|
|
|
181
|
-
|
|
182
|
-
import { Logger } from "@node-cli/logger";
|
|
183
|
-
const log = new Logger({ timestamp: true });
|
|
293
|
+
### Logger Class
|
|
184
294
|
|
|
185
|
-
|
|
186
|
-
log.timestamp = false;
|
|
187
|
-
log.info("this will be NOT be logged with a timestamp");
|
|
188
|
-
```
|
|
295
|
+
#### Constructor Options
|
|
189
296
|
|
|
190
|
-
|
|
297
|
+
| Option | Type | Default | Description |
|
|
298
|
+
| ----------- | ------- | ------- | --------------------------------------- |
|
|
299
|
+
| `boring` | boolean | `false` | Disable colors in output |
|
|
300
|
+
| `silent` | boolean | `false` | Disable all logging |
|
|
301
|
+
| `prefix` | string | `""` | Add a prefix to all log messages |
|
|
302
|
+
| `timestamp` | boolean | `false` | Add timestamps to all log messages |
|
|
303
|
+
| `inMemory` | boolean | `false` | Store logs in memory instead of console |
|
|
191
304
|
|
|
192
|
-
|
|
305
|
+
#### Methods
|
|
193
306
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
307
|
+
| Method | Arguments | Description |
|
|
308
|
+
| -------------------- | ----------------------------------------------------------- | ------------------------------------------------------ |
|
|
309
|
+
| `log` | `...args` | Standard log output (white) |
|
|
310
|
+
| `info` | `...args` | Informational log output (blue) |
|
|
311
|
+
| `debug` | `...args` | Debug log output (grey) |
|
|
312
|
+
| `warn` | `...args` | Warning log output (yellow) |
|
|
313
|
+
| `error` | `...args` | Error log output (red) |
|
|
314
|
+
| `printBox` | `messages: string \| string[]`, `options?: PrintBoxOptions` | Display message(s) in a formatted box |
|
|
315
|
+
| `printErrorsAndExit` | `errorMessages: string[]`, `exitStatus?: number` | Display error messages and optionally exit the process |
|
|
316
|
+
| `getMemoryLogs` | none | Get all logs stored in memory as a string |
|
|
317
|
+
| `clearMemoryLogs` | none | Clear all logs stored in memory |
|
|
197
318
|
|
|
198
|
-
|
|
319
|
+
#### Properties (Setters)
|
|
199
320
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
321
|
+
| Property | Type | Description |
|
|
322
|
+
| ----------- | ------- | -------------------------------- |
|
|
323
|
+
| `silent` | boolean | Enable/disable logging |
|
|
324
|
+
| `boring` | boolean | Enable/disable colors |
|
|
325
|
+
| `prefix` | string | Set prefix for log messages |
|
|
326
|
+
| `timestamp` | boolean | Enable/disable timestamps |
|
|
327
|
+
| `inMemory` | boolean | Enable/disable in-memory logging |
|
|
206
328
|
|
|
207
|
-
|
|
329
|
+
#### PrintBox Options
|
|
208
330
|
|
|
209
|
-
|
|
331
|
+
| Option | Type | Default | Description |
|
|
332
|
+
| --------------- | ------- | ----------- | ------------------------------------------------ |
|
|
333
|
+
| `newLineAfter` | boolean | `true` | Print a new line after the box |
|
|
334
|
+
| `newLineBefore` | boolean | `true` | Print a new line before the box |
|
|
335
|
+
| `borderColor` | string | `"yellow"` | Color of the box border |
|
|
336
|
+
| `padding` | number | `1` | Padding inside the box |
|
|
337
|
+
| `textAlignment` | string | `"center"` | Text alignment (`"left"`, `"center"`, `"right"`) |
|
|
338
|
+
| `title` | string | `undefined` | Optional title for the box |
|
|
210
339
|
|
|
211
|
-
|
|
212
|
-
- `printLineBefore` (default to `true`)
|
|
213
|
-
- As well as all the options available with [Boxen](https://github.com/sindresorhus/boxen)
|
|
340
|
+
Plus all options from [Boxen](https://github.com/sindresorhus/boxen).
|
|
214
341
|
|
|
215
|
-
|
|
342
|
+
### Spinner Class
|
|
216
343
|
|
|
217
|
-
|
|
218
|
-
- no extra line after the box
|
|
219
|
-
- no padding (no space between the border and the text)
|
|
220
|
-
- text is justified to the right
|
|
221
|
-
- there is a title injected at the top of the box
|
|
344
|
+
#### Constructor Options
|
|
222
345
|
|
|
223
|
-
|
|
224
|
-
import { Logger } from "@node-cli/logger";
|
|
225
|
-
const log = new Logger();
|
|
346
|
+
Accepts all options from [ora](https://github.com/sindresorhus/ora).
|
|
226
347
|
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
});
|
|
234
|
-
```
|
|
348
|
+
| Option | Type | Description |
|
|
349
|
+
| --------- | ---------------- | --------------------------------- |
|
|
350
|
+
| `text` | string | Text to display after the spinner |
|
|
351
|
+
| `color` | string | Color of the spinner |
|
|
352
|
+
| `spinner` | string \| object | Spinner pattern to use |
|
|
353
|
+
| ... | ... | All other ora options |
|
|
235
354
|
|
|
236
|
-
|
|
355
|
+
#### Methods
|
|
237
356
|
|
|
238
|
-
|
|
239
|
-
|
|
357
|
+
| Method | Arguments | Description |
|
|
358
|
+
| ------- | ---------------------------------- | ---------------------------------------------- |
|
|
359
|
+
| `start` | `text?: string` | Start the spinner with optional text |
|
|
360
|
+
| `stop` | `text?: string`, `status?: string` | Stop the spinner with optional text and status |
|
|
240
361
|
|
|
241
|
-
|
|
242
|
-
import { Logger } from "@node-cli/logger";
|
|
243
|
-
const log = new Logger();
|
|
362
|
+
#### Properties
|
|
244
363
|
|
|
245
|
-
|
|
246
|
-
|
|
364
|
+
| Property | Type | Description |
|
|
365
|
+
| -------- | ------ | -------------------- |
|
|
366
|
+
| `text` | string | Set the spinner text |
|
|
367
|
+
|
|
368
|
+
#### Status Constants
|
|
369
|
+
|
|
370
|
+
| Constant | Value | Description |
|
|
371
|
+
| ----------------- | ----------- | ----------------------------------- |
|
|
372
|
+
| `Spinner.SUCCESS` | `"success"` | Success status (green checkmark) |
|
|
373
|
+
| `Spinner.ERROR` | `"fail"` | Error status (red X) |
|
|
374
|
+
| `Spinner.WARNING` | `"warn"` | Warning status (yellow exclamation) |
|
|
375
|
+
| `Spinner.INFO` | `"info"` | Info status (blue i) |
|
|
376
|
+
|
|
377
|
+
## Environment Compatibility
|
|
378
|
+
|
|
379
|
+
- Works with Node.js >=16
|
|
380
|
+
- ESM module format
|
|
381
|
+
- TypeScript definitions included
|
|
247
382
|
|
|
248
383
|
## License
|
|
249
384
|
|
package/dist/Logger.d.ts
CHANGED
|
@@ -4,18 +4,36 @@ export type PrintBoxOptions = {
|
|
|
4
4
|
newLineAfter?: boolean;
|
|
5
5
|
newLineBefore?: boolean;
|
|
6
6
|
} & BoxenOptions;
|
|
7
|
+
export type LoggerOptions = {
|
|
8
|
+
boring?: boolean;
|
|
9
|
+
silent?: boolean;
|
|
10
|
+
prefix?: string;
|
|
11
|
+
timestamp?: boolean;
|
|
12
|
+
inMemory?: boolean;
|
|
13
|
+
};
|
|
7
14
|
export declare class Logger {
|
|
8
15
|
#private;
|
|
9
|
-
constructor({ boring, silent, prefix, timestamp, }?: {
|
|
16
|
+
constructor({ boring, silent, prefix, timestamp, inMemory, }?: {
|
|
10
17
|
boring?: boolean;
|
|
11
18
|
silent?: boolean;
|
|
12
19
|
prefix?: string;
|
|
13
20
|
timestamp?: boolean;
|
|
21
|
+
inMemory?: boolean;
|
|
14
22
|
});
|
|
15
23
|
set silent(flag: boolean);
|
|
16
24
|
set boring(flag: boolean);
|
|
17
25
|
set prefix(prefix: string);
|
|
18
26
|
set timestamp(flag: boolean);
|
|
27
|
+
set inMemory(flag: boolean);
|
|
28
|
+
/**
|
|
29
|
+
* Get the accumulated logs as a string
|
|
30
|
+
* @returns {string} All logs joined by the separator
|
|
31
|
+
*/
|
|
32
|
+
getMemoryLogs(): string;
|
|
33
|
+
/**
|
|
34
|
+
* Clear all accumulated logs from memory
|
|
35
|
+
*/
|
|
36
|
+
clearMemoryLogs(): void;
|
|
19
37
|
info(...arguments_: any): void;
|
|
20
38
|
log(...arguments_: any): void;
|
|
21
39
|
debug(...arguments_: any): void;
|
package/dist/Logger.js
CHANGED
|
@@ -7,12 +7,17 @@ export class Logger {
|
|
|
7
7
|
#globalPrefix;
|
|
8
8
|
#showTimestamp;
|
|
9
9
|
#printOptions;
|
|
10
|
-
|
|
10
|
+
#inMemory;
|
|
11
|
+
#memoryLogs;
|
|
12
|
+
constructor({ boring = false, silent = false, prefix = "", timestamp = false, inMemory = false } = {}){
|
|
11
13
|
this.#shouldLog = !silent;
|
|
12
14
|
this.#globalPrefix = prefix;
|
|
13
15
|
this.#showTimestamp = timestamp;
|
|
16
|
+
this.#inMemory = inMemory;
|
|
17
|
+
this.#memoryLogs = [];
|
|
18
|
+
// When in memory mode, we disable colors
|
|
14
19
|
this.#printOptions = {
|
|
15
|
-
colors: !boring,
|
|
20
|
+
colors: !boring && !inMemory,
|
|
16
21
|
compact: false,
|
|
17
22
|
depth: 5
|
|
18
23
|
};
|
|
@@ -21,7 +26,10 @@ export class Logger {
|
|
|
21
26
|
this.#shouldLog = !flag;
|
|
22
27
|
}
|
|
23
28
|
set boring(flag) {
|
|
24
|
-
|
|
29
|
+
// Only set colors if not in memory mode
|
|
30
|
+
if (!this.#inMemory) {
|
|
31
|
+
this.#printOptions.colors = !flag;
|
|
32
|
+
}
|
|
25
33
|
}
|
|
26
34
|
set prefix(prefix) {
|
|
27
35
|
this.#globalPrefix = prefix;
|
|
@@ -29,6 +37,24 @@ export class Logger {
|
|
|
29
37
|
set timestamp(flag) {
|
|
30
38
|
this.#showTimestamp = flag;
|
|
31
39
|
}
|
|
40
|
+
set inMemory(flag) {
|
|
41
|
+
this.#inMemory = flag;
|
|
42
|
+
// When enabling in-memory mode, disable colors
|
|
43
|
+
if (flag) {
|
|
44
|
+
this.#printOptions.colors = false;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Get the accumulated logs as a string
|
|
49
|
+
* @returns {string} All logs joined by the separator
|
|
50
|
+
*/ getMemoryLogs() {
|
|
51
|
+
return this.#memoryLogs.join("\n");
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Clear all accumulated logs from memory
|
|
55
|
+
*/ clearMemoryLogs() {
|
|
56
|
+
this.#memoryLogs = [];
|
|
57
|
+
}
|
|
32
58
|
#_log(type, ...arguments_) {
|
|
33
59
|
if (this.#shouldLog) {
|
|
34
60
|
let message;
|
|
@@ -44,8 +70,14 @@ export class Logger {
|
|
|
44
70
|
}
|
|
45
71
|
message = util.formatWithOptions(this.#printOptions, prefix.join(" "), ...arguments_);
|
|
46
72
|
}
|
|
47
|
-
//
|
|
48
|
-
|
|
73
|
+
// Store in memory if enabled
|
|
74
|
+
if (this.#inMemory) {
|
|
75
|
+
this.#memoryLogs.push(message);
|
|
76
|
+
}
|
|
77
|
+
// Still output to console if not in memory-only mode
|
|
78
|
+
if (!this.#inMemory) {
|
|
79
|
+
console[type.method](this.#printOptions.colors ? `${type.color(message)}` : message);
|
|
80
|
+
}
|
|
49
81
|
}
|
|
50
82
|
}
|
|
51
83
|
info(...arguments_) {
|
|
@@ -126,7 +158,7 @@ export class Logger {
|
|
|
126
158
|
this.#globalPrefix = oldPrefix;
|
|
127
159
|
}
|
|
128
160
|
}
|
|
129
|
-
/*
|
|
161
|
+
/* v8 ignore next 48 */ export class Spinner {
|
|
130
162
|
spinner;
|
|
131
163
|
constructor(options){
|
|
132
164
|
this.spinner = ora({
|
package/dist/Logger.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/Logger.ts"],"sourcesContent":["import boxen, { Options as BoxenOptions } from \"boxen\";\nimport ora, { Ora, Options as OraOptions } from \"ora\";\n\nimport util from \"node:util\";\nimport kleur from \"kleur\";\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\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\t// eslint-disable-next-line no-console\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 borderColor = options.borderColor || \"yellow\";\n\t\tconst boxenOptions: BoxenOptions = {\n\t\t\t...options,\n\t\t\tborderColor: this.#printOptions.colors ? borderColor : \"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\n/* istanbul ignore next */\nexport class Spinner {\n\tspinner: Ora;\n\n\tconstructor(options?: OraOptions) {\n\t\tthis.spinner = ora({\n\t\t\t...options,\n\t\t\tisSilent: process.env.NODE_ENV === \"test\",\n\t\t});\n\t}\n\n\tset text(message: string) {\n\t\tthis.spinner.text = message;\n\t}\n\n\tstart(message?: string) {\n\t\tthis.spinner.start(message);\n\t}\n\n\tstop(message?: string, type?: string) {\n\t\tswitch (type) {\n\t\t\tcase Spinner.ERROR: {\n\t\t\t\tthis.spinner.fail(message);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase Spinner.WARNING: {\n\t\t\t\tthis.spinner.warn(message);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase Spinner.INFO: {\n\t\t\t\tthis.spinner.info(message);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tdefault: {\n\t\t\t\tsetTimeout(() => {\n\t\t\t\t\tthis.spinner.succeed(message);\n\t\t\t\t}, 1000);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\n\tstatic SUCCESS = \"success\";\n\tstatic ERROR = \"fail\";\n\tstatic WARNING = \"warn\";\n\tstatic INFO = \"info\";\n}\n"],"names":["boxen","ora","util","kleur","Logger","constructor","boring","silent","prefix","timestamp","colors","compact","depth","flag","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","borderColor","boxenOptions","padding","textAlignment","oldPrefix","oldTimestamp","Spinner","spinner","isSilent","env","NODE_ENV","text","start","stop","ERROR","fail","WARNING","INFO","setTimeout","succeed","SUCCESS"],"mappings":"AAAA,OAAOA,WAAwC,QAAQ;AACvD,OAAOC,SAAyC,MAAM;AAEtD,OAAOC,UAAU,YAAY;AAC7B,OAAOC,WAAW,QAAQ;AAO1B,OAAO,MAAMC;IACZ,CAAA,SAAU,CAAU;IACpB,CAAA,YAAa,CAAS;IACtB,CAAA,aAAc,CAAU;IACxB,CAAA,YAAa,CAAuD;IAEpEC,YAAY,EACXC,SAAS,KAAK,EACdC,SAAS,KAAK,EACdC,SAAS,EAAE,EACXC,YAAY,KAAK,EACjB,GAAG,CAAC,CAAC,CAAE;QACP,IAAI,CAAC,CAAA,SAAU,GAAG,CAACF;QACnB,IAAI,CAAC,CAAA,YAAa,GAAGC;QACrB,IAAI,CAAC,CAAA,aAAc,GAAGC;QACtB,IAAI,CAAC,CAAA,YAAa,GAAG;YACpBC,QAAQ,CAACJ;YACTK,SAAS;YACTC,OAAO;QACR;IACD;IAEA,IAAIL,OAAOM,IAAa,EAAE;QACzB,IAAI,CAAC,CAAA,SAAU,GAAG,CAACA;IACpB;IAEA,IAAIP,OAAOO,IAA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AC3B,IAAI,CAACZ;oBAClB;gBACD;YACA;gBAAS;oBACRkD,WAAW;wBACV,IAAI,CAACX,OAAO,CAACY,OAAO,CAACnD;oBACtB,GAAG;oBACH;gBACD;QACD;IACD;IAEA,OAAOoD,UAAU,UAAU;IAC3B,OAAON,QAAQ,OAAO;IACtB,OAAOE,UAAU,OAAO;IACxB,OAAOC,OAAO,OAAO;AACtB"}
|
|
1
|
+
{"version":3,"sources":["../src/Logger.ts"],"sourcesContent":["import boxen, { Options as BoxenOptions } from \"boxen\";\nimport ora, { Ora, Options as OraOptions } from \"ora\";\n\nimport util from \"node:util\";\nimport kleur from \"kleur\";\n\nexport type PrintBoxOptions = {\n\tnewLineAfter?: boolean;\n\tnewLineBefore?: boolean;\n} & BoxenOptions;\n\nexport type LoggerOptions = {\n\tboring?: boolean;\n\tsilent?: boolean;\n\tprefix?: string;\n\ttimestamp?: boolean;\n\tinMemory?: boolean;\n};\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\t#inMemory: boolean;\n\t#memoryLogs: string[];\n\n\tconstructor({\n\t\tboring = false,\n\t\tsilent = false,\n\t\tprefix = \"\",\n\t\ttimestamp = false,\n\t\tinMemory = false,\n\t} = {}) {\n\t\tthis.#shouldLog = !silent;\n\t\tthis.#globalPrefix = prefix;\n\t\tthis.#showTimestamp = timestamp;\n\t\tthis.#inMemory = inMemory;\n\t\tthis.#memoryLogs = [];\n\n\t\t// When in memory mode, we disable colors\n\t\tthis.#printOptions = {\n\t\t\tcolors: !boring && !inMemory,\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\t// Only set colors if not in memory mode\n\t\tif (!this.#inMemory) {\n\t\t\tthis.#printOptions.colors = !flag;\n\t\t}\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\tset inMemory(flag: boolean) {\n\t\tthis.#inMemory = flag;\n\t\t// When enabling in-memory mode, disable colors\n\t\tif (flag) {\n\t\t\tthis.#printOptions.colors = false;\n\t\t}\n\t}\n\n\t/**\n\t * Get the accumulated logs as a string\n\t * @returns {string} All logs joined by the separator\n\t */\n\tgetMemoryLogs(): string {\n\t\treturn this.#memoryLogs.join(\"\\n\");\n\t}\n\n\t/**\n\t * Clear all accumulated logs from memory\n\t */\n\tclearMemoryLogs(): void {\n\t\tthis.#memoryLogs = [];\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\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\n\t\t\t// Store in memory if enabled\n\t\t\tif (this.#inMemory) {\n\t\t\t\tthis.#memoryLogs.push(message);\n\t\t\t}\n\n\t\t\t// Still output to console if not in memory-only mode\n\t\t\tif (!this.#inMemory) {\n\t\t\t\tconsole[type.method](\n\t\t\t\t\tthis.#printOptions.colors ? `${type.color(message)}` : message,\n\t\t\t\t);\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 borderColor = options.borderColor || \"yellow\";\n\t\tconst boxenOptions: BoxenOptions = {\n\t\t\t...options,\n\t\t\tborderColor: this.#printOptions.colors ? borderColor : \"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\n/* v8 ignore next 48 */\nexport class Spinner {\n\tspinner: Ora;\n\n\tconstructor(options?: OraOptions) {\n\t\tthis.spinner = ora({\n\t\t\t...options,\n\t\t\tisSilent: process.env.NODE_ENV === \"test\",\n\t\t});\n\t}\n\n\tset text(message: string) {\n\t\tthis.spinner.text = message;\n\t}\n\n\tstart(message?: string) {\n\t\tthis.spinner.start(message);\n\t}\n\n\tstop(message?: string, type?: string) {\n\t\tswitch (type) {\n\t\t\tcase Spinner.ERROR: {\n\t\t\t\tthis.spinner.fail(message);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase Spinner.WARNING: {\n\t\t\t\tthis.spinner.warn(message);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase Spinner.INFO: {\n\t\t\t\tthis.spinner.info(message);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tdefault: {\n\t\t\t\tsetTimeout(() => {\n\t\t\t\t\tthis.spinner.succeed(message);\n\t\t\t\t}, 1000);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\n\tstatic SUCCESS = \"success\";\n\tstatic ERROR = \"fail\";\n\tstatic WARNING = \"warn\";\n\tstatic INFO = \"info\";\n}\n"],"names":["boxen","ora","util","kleur","Logger","constructor","boring","silent","prefix","timestamp","inMemory","colors","compact","depth","flag","getMemoryLogs","join","clearMemoryLogs","type","arguments_","message","formatWithOptions","now","Date","push","grey","toDateString","toLocaleTimeString","console","method","color","info","blue","log","white","debug","warn","yellow","error","red","printErrorsAndExit","errorMessages","exitStatus","length","process","exit","printBox","messages","options","newLineAfter","newLineBefore","borderColor","boxenOptions","padding","textAlignment","oldPrefix","oldTimestamp","Spinner","spinner","isSilent","env","NODE_ENV","text","start","stop","ERROR","fail","WARNING","INFO","setTimeout","succeed","SUCCESS"],"mappings":"AAAA,OAAOA,WAAwC,QAAQ;AACvD,OAAOC,SAAyC,MAAM;AAEtD,OAAOC,UAAU,YAAY;AAC7B,OAAOC,WAAW,QAAQ;AAe1B,OAAO,MAAMC;IACZ,CAAA,SAAU,CAAU;IACpB,CAAA,YAAa,CAAS;IACtB,CAAA,aAAc,CAAU;IACxB,CAAA,YAAa,CAAuD;IACpE,CAAA,QAAS,CAAU;IACnB,CAAA,UAAW,CAAW;IAEtBC,YAAY,EACXC,SAAS,KAAK,EACdC,SAAS,KAAK,EACdC,SAAS,EAAE,EACXC,YAAY,KAAK,EACjBC,WAAW,KAAK,EAChB,GAAG,CAAC,CAAC,CAAE;QACP,IAAI,CAAC,CAAA,SAAU,GAAG,CAACH;QACnB,IAAI,CAAC,CAAA,YAAa,GAAGC;QACrB,IAAI,CAAC,CAAA,aAAc,GAAGC;QACtB,IAAI,CAAC,CAAA,QAAS,GAAGC;QACjB,IAAI,CAAC,CAAA,UAAW,GAAG,EAAE;QAErB,yCAAyC;QACzC,IAAI,CAAC,CAAA,YAAa,GAAG;YACpBC,QAAQ,CAACL,UAAU,CAACI;YACpBE,SAAS;YACTC,OAAO;QACR;IACD;IAEA,IAAIN,OAAOO,IAAa,EAAE;QACzB,IAAI,CAAC,CAAA,SAAU,GAAG,CAACA;IACpB;IAEA,IAAIR,OAAOQ,IAAa,EAAE;QACzB,wC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zD,IAAI;YAClB,GAAG+C,OAAO;YACVW,UAAUf,QAAQgB,GAAG,CAACC,QAAQ,KAAK;QACpC;IACD;IAEA,IAAIC,KAAK1C,OAAe,EAAE;QACzB,IAAI,CAACsC,OAAO,CAACI,IAAI,GAAG1C;IACrB;IAEA2C,MAAM3C,OAAgB,EAAE;QACvB,IAAI,CAACsC,OAAO,CAACK,KAAK,CAAC3C;IACpB;IAEA4C,KAAK5C,OAAgB,EAAEF,IAAa,EAAE;QACrC,OAAQA;YACP,KAAKuC,QAAQQ,KAAK;gBAAE;oBACnB,IAAI,CAACP,OAAO,CAACQ,IAAI,CAAC9C;oBAClB;gBACD;YACA,KAAKqC,QAAQU,OAAO;gBAAE;oBACrB,IAAI,CAACT,OAAO,CAACtB,IAAI,CAAChB;oBAClB;gBACD;YACA,KAAKqC,QAAQW,IAAI;gBAAE;oBAClB,IAAI,CAACV,OAAO,CAAC3B,IAAI,CAACX;oBAClB;gBACD;YACA;gBAAS;oBACRiD,WAAW;wBACV,IAAI,CAACX,OAAO,CAACY,OAAO,CAAClD;oBACtB,GAAG;oBACH;gBACD;QACD;IACD;IAEA,OAAOmD,UAAU,UAAU;IAC3B,OAAON,QAAQ,OAAO;IACtB,OAAOE,UAAU,OAAO;IACxB,OAAOC,OAAO,OAAO;AACtB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@node-cli/logger",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Arno Versini",
|
|
6
6
|
"description": "A tiny console logger for nodejs CLI apps",
|
|
@@ -11,9 +11,9 @@
|
|
|
11
11
|
],
|
|
12
12
|
"node": ">=16",
|
|
13
13
|
"dependencies": {
|
|
14
|
-
"boxen": "
|
|
14
|
+
"boxen": "8.0.1",
|
|
15
15
|
"kleur": "4.1.5",
|
|
16
|
-
"ora": "8.
|
|
16
|
+
"ora": "8.2.0"
|
|
17
17
|
},
|
|
18
18
|
"scripts": {
|
|
19
19
|
"build": "npm-run-all --serial clean build:types build:js build:barrel",
|
|
@@ -22,12 +22,17 @@
|
|
|
22
22
|
"build:types": "tsc",
|
|
23
23
|
"clean": "rimraf dist types coverage",
|
|
24
24
|
"lint": "biome lint src",
|
|
25
|
-
"test": "
|
|
26
|
-
"test:coverage": "
|
|
25
|
+
"test": "vitest run --globals",
|
|
26
|
+
"test:coverage": "vitest run --coverage --globals",
|
|
27
|
+
"test:watch": "vitest --globals",
|
|
27
28
|
"watch": "swc --strip-leading-paths --watch --out-dir dist src"
|
|
28
29
|
},
|
|
29
30
|
"publishConfig": {
|
|
30
31
|
"access": "public"
|
|
31
32
|
},
|
|
32
|
-
"
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@vitest/coverage-v8": "3.2.4",
|
|
35
|
+
"vitest": "3.2.4"
|
|
36
|
+
},
|
|
37
|
+
"gitHead": "2cef8f88aa5316a1789caad2bd7327ca908ccb9f"
|
|
33
38
|
}
|