filecoin-pin
Version:
Bridge IPFS content to Filecoin Onchain Cloud using familiar tools
159 lines • 4.07 kB
JavaScript
/**
* Logging utilities for consistent output across TTY and non-TTY environments
*
* Provides a unified interface for logging that:
* - Uses Clack's formatted output in TTY mode
* - Falls back to plain console.log in non-TTY mode
* - Maintains consistent indentation across all modes
*/
import { log as clackLog } from '@clack/prompts';
import pc from 'picocolors';
/**
* Check if we're in TTY mode
*/
function isTTY() {
return process.stdout.isTTY ?? false;
}
/**
* Buffer for collecting log lines to output together
*/
let lineBuffer = [];
/**
* Logger that adapts to TTY/non-TTY environments
*/
export { isTTY };
export const log = {
/**
* Log a plain message
*/
message(message) {
if (isTTY()) {
// If we have buffered lines, flush them first
if (lineBuffer.length > 0) {
clackLog.message(lineBuffer.join('\n'));
lineBuffer = [];
}
clackLog.message(message);
}
else {
console.log(`${message}\n`);
}
},
/**
* Add a line to the buffer (for batched output in TTY mode)
*/
line(message) {
if (isTTY()) {
lineBuffer.push(message);
}
else {
console.log(message);
}
},
/**
* Flush any buffered lines
*/
flush() {
if (isTTY() && lineBuffer.length > 0) {
// Join all lines, including empty ones for controlled spacing
clackLog.message(lineBuffer.join('\n'));
lineBuffer = [];
}
},
/**
* Log an info message
*/
info(message) {
if (isTTY()) {
clackLog.info(message);
}
else {
console.log(message);
}
},
/**
* Log a success message
*/
success(message) {
if (isTTY()) {
clackLog.success(message);
}
else {
console.log(message);
}
},
/**
* Log a warning message
*/
warn(message) {
if (isTTY()) {
clackLog.warn(message);
}
else {
console.log(message);
}
},
/**
* Log a section with title and content
* In TTY mode, batches output to reduce vertical space
* In non-TTY mode, uses plain text
*/
section(title, content) {
const lines = Array.isArray(content) ? content : [content];
if (isTTY()) {
const output = [`\n${pc.bold(title)}`, ...lines.map((line) => ` ${line}`)].join('\n');
clackLog.message(output);
}
else {
console.log(`\n${title}`);
for (const line of lines) {
console.log(` ${line}`);
}
}
},
/**
* This section is intended to be used with an active spinner. It will not insert a newline before the title and should be spaced properly with closed/open spinners.
*/
spinnerSection(title, content, options = { indentLevel: 1 }) {
const lines = Array.isArray(content) ? content : [content];
if (isTTY()) {
clackLog.message(pc.bold(title));
}
else {
console.log(`${title}`);
}
for (const line of lines) {
this.indent(line, options.indentLevel);
}
if (lineBuffer.length === 0) {
this.newline();
}
this.flush();
},
/**
* Log with custom indentation
* Adds to buffer in TTY mode for batched output
*/
indent(message, level = 1) {
const indent = ' '.repeat(level);
if (isTTY()) {
lineBuffer.push(`${indent}${message}`);
}
else {
console.log(`${indent}${message}`);
}
},
/**
* Log a blank line
* In TTY mode, just adds to buffer
*/
newline() {
if (isTTY()) {
lineBuffer.push('');
}
else {
console.log();
}
},
};
//# sourceMappingURL=cli-logger.js.map