@sp-packages/printer v2.10.2
Printer
⨠Features
- šØ Rich formatting for messages (success, error, warning, info, headers)
- š Verbose mode for detailed logging
- š Quiet mode to suppress non-essential output
- š Spinner for showing progress during async operations
- š Usable via CLI or programmatically in Node.js
- š Structured output for better readability
- š Ideal for CI/CD, automation scripts, and development tools
š¦ Installation
Global Installation (For system-wide CLI use)
npm install -g @sp-packages/printer
This allows you to use printer
globally in your terminal.
Local Installation (For project-specific use)
npm install @sp-packages/printer --save-dev
Then, run it via:
npx printer success "Setup completed!"
š CLI Usage
Basic Usage
printer <type> <message>
Message Types:
- ā
success
- Green message for success output - ā
error
- Red message for errors (supports error objects) - ā
warning
- Yellow message for warnings - ā¹
info
- Blue message for general information - š
message
- Plain text message (no formatting)
Examples:
printer success "Operation completed successfully!"
printer error "Something went wrong"
printer warning "This is a warning message"
printer info "Starting process..."
printer message "Simple message without formatting"
š Programmatic Usage (Inside Node.js)
You can also use printer
inside your JavaScript/TypeScript projects.
Import and Use in Your Project
import { Printer } from '@sp-packages/printer';
Printer.success('Setup completed successfully!');
Printer.error('An error occurred', new Error('Database connection failed'));
Printer.warning('This is a warning message');
Printer.info('Fetching data...');
Printer.message('Regular message');
Using Spinner
The spinner feature is perfect for displaying progress during asynchronous operations. It shows an animated loading indicator that can be updated with success or error states.
import { Printer } from '@sp-packages/printer';
const spinner = Printer.spinner('Loading data...');
// During async operations
try {
await someAsyncOperation();
spinner.succeed('Data loaded successfully!');
} catch (error) {
spinner.fail('Failed to load data!');
}
Spinner Methods:
spinner.start()
- Start or restart the spinnerspinner.stop()
- Stop and clear the spinnerspinner.succeed(text?)
- Stop the spinner and show success statespinner.fail(text?)
- Stop the spinner and show failure statespinner.warn(text?)
- Stop the spinner and show warning statespinner.info(text?)
- Stop the spinner and show info state
Verbose Mode
Verbose mode provides detailed logging, which can be extremely helpful for debugging and development purposes. When enabled, it outputs additional information that can help you trace the execution flow and identify issues more easily.
Enable Verbose Mode:
Printer.enableVerbose();
Printer.log('This will display only in verbose mode.');
Disable Verbose Mode:
Printer.disableVerbose();
Example (Verbose Mode ON):
Printer.enableVerbose();
Printer.success('Process completed');
Printer.log('Detailed execution log...');
Output:
ā
[SUCCESS] Process completed
Detailed execution log...
Example (Verbose Mode OFF):
Printer.disableVerbose();
Printer.success('Process completed');
Printer.log('Detailed execution log...');
Output:
ā
[SUCCESS] Process completed
Benefits of Verbose Mode in npm Modules:
- Enhanced Debugging: Verbose mode provides more context and detailed logs, making it easier to pinpoint where things might be going wrong in your code.
- Better Traceability: With more information being logged, you can trace the execution path and understand the sequence of operations.
- Improved Development Experience: Developers can get insights into the internal workings of the module, which can be particularly useful during development and testing phases.
- Efficient Troubleshooting: Detailed logs can help in quickly identifying and resolving issues, reducing the time spent on debugging.
- Single Control Point: Verbose mode can be enabled or disabled with a single flag, providing a centralized way to control the verbosity of logs.
Verbose mode is particularly useful in CI/CD pipelines, automation scripts, and during the development of complex Node.js applications where understanding the flow of execution is crucial.
Quiet Mode
Quiet mode suppresses all output, including errors. This can be useful when you need to ensure that no messages clutter the console output in production or automated environments.
Enable Quiet Mode:
Printer.enableQuiet();
Disable Quiet Mode:
Printer.disableQuiet();
Example (Quiet Mode ON):
Printer.enableQuiet();
Printer.success('Process completed');
Printer.warning('This warning will NOT be displayed');
Printer.error('Critical error!');
Output:
Note: When quiet mode is enabled, no messages will be printed, including errors.
Example (Quiet Mode OFF):
Printer.disableQuiet();
Printer.success('Process completed');
Printer.warning('This warning WILL be displayed');
Output:
ā
[SUCCESS] Process completed
ā [WARNING] This warning WILL be displayed
šÆ Example Outputs
ā
[SUCCESS] Operation completed successfully!
ā [ERROR] An error occurred: Database connection failed
ā [WARNING] Be cautious! Proceeding with default values.
ā¹ [INFO] Fetching data...
š [MESSAGE] Process initiated.
š” Use Cases
- CI/CD Pipelines ā Enhance logs in automation scripts
- Node.js CLI Tools ā Format console outputs for better readability
- Development Debugging ā Enable verbose mode for debugging
- Project Setup Scripts ā Display status messages during installations
š¤ Contributing
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
š License
This project is licensed under the MIT License. See the LICENSE file for details.