UNPKG

@intlayer/config

Version:

Retrieve Intlayer configurations and manage environment variables for both server-side and client-side environments.

78 lines (76 loc) 2.53 kB
import { BLUE, RED } from "../colors.mjs"; import { extractErrorMessage } from "../utils/extractErrorMessage.mjs"; import { colorize, getPrefix, spinnerFrames, v, x } from "../logger.mjs"; //#region src/bundle/logBundle.ts var BundleLogger = class { status = "pending"; spinnerTimer = null; spinnerIndex = 0; renderedLines = 0; isFinished = false; prefix; lastRenderedState = ""; error; constructor(configuration) { this.prefix = getPrefix(configuration?.log?.prefix) ?? ""; } setStatus(status) { if (this.isFinished) return; this.status = status; if (status === "success") this.finish(); else { this.startSpinner(); this.render(); } } setError(error) { if (this.isFinished) return; this.status = "error"; this.error = extractErrorMessage(error); this.finish(); } startSpinner() { if (this.spinnerTimer || this.isFinished) return; this.spinnerTimer = setInterval(() => { this.spinnerIndex = (this.spinnerIndex + 1) % spinnerFrames.length; this.render(); }, 100); } stopSpinner() { if (!this.spinnerTimer) return; clearInterval(this.spinnerTimer); this.spinnerTimer = null; } finish() { this.isFinished = true; this.stopSpinner(); this.render(); } render() { const frame = spinnerFrames[this.spinnerIndex]; const clock = colorize(frame, BLUE); const lines = []; if (this.status === "installing") lines.push(`${this.prefix} ${clock} Fetching and resolving packages...`); else if (this.status === "bundling") { lines.push(`${this.prefix} ${v} Fetching and resolving packages...`); lines.push(`${this.prefix} ${clock} Bundling application...`); } else if (this.status === "success") { lines.push(`${this.prefix} ${v} Fetching and resolving packages...`); lines.push(`${this.prefix} ${v} Bundling application...`); } else if (this.status === "error") lines.push(`${this.prefix} ${x} Bundle failed: ${colorize(this.error ?? "Unknown error", RED)}`); const currentState = lines.join("\n"); if (currentState === this.lastRenderedState) return; this.lastRenderedState = currentState; if (this.renderedLines > 0) process.stdout.write(`\x1b[${this.renderedLines}F`); const totalLinesToClear = Math.max(this.renderedLines, lines.length); for (let i = 0; i < totalLinesToClear; i++) { process.stdout.write("\x1B[2K"); if (lines[i] !== void 0) process.stdout.write(lines[i]); process.stdout.write("\n"); } this.renderedLines = lines.length; } }; //#endregion export { BundleLogger }; //# sourceMappingURL=logBundle.mjs.map