UNPKG

@plugjs/plug

Version:
168 lines (167 loc) 4.58 kB
// logging/options.ts import { EventEmitter } from "node:events"; import { getSingleton } from "../utils/singleton.mjs"; import { getLevelNumber, NOTICE } from "./levels.mjs"; var LogOptionsImpl = class extends EventEmitter { _output = process.stderr; _level = NOTICE; _colors = this._output.isTTY; _format = this._colors ? "fancy" : "plain"; _colorsSet = false; // have colors been set manually? _spinner = true; // by default, the spinner is enabled _lineLength = this._output.columns || 80; _lineLengthSet = false; // has line length been set manually? _showSources = true; // by default, always show source snippets _githubAnnotations = false; // ultimately set by the constructor _inspectOptions = {}; _taskLength = 0; _indentSize = 2; constructor() { super(); this.setMaxListeners(20); if (process.env.LOG_LEVEL) { this._level = getLevelNumber(process.env.LOG_LEVEL); } if (process.env.LOG_COLORS) { if (process.env.LOG_COLORS.toLowerCase() === "true") this.colors = true; if (process.env.LOG_COLORS.toLowerCase() === "false") this.colors = false; } this._githubAnnotations = process.env.GITHUB_ACTIONS === "true"; if (this._githubAnnotations) { this._colors = true; this._format = "plain"; this._spinner = false; } const options = JSON.parse(process.env.__LOG_OPTIONS || "{}"); Object.assign(this, options); } _notifyListeners() { super.emit("changed", this); } forkEnv(taskName) { void taskName; return { __LOG_OPTIONS: JSON.stringify({ level: this._level, colors: this._colors, format: this._format, lineLength: this._lineLength, taskLength: this._taskLength, showSources: this._showSources, githubAnnotations: this.githubAnnotations, indentSize: this.indentSize, spinner: false // forked spinner is always false }) }; } get output() { return this._output; } set output(output) { this._output = output; if (!this._colorsSet) this._colors = !!output.isTTY; if (!this._lineLengthSet) this._lineLength = output.columns; this._notifyListeners(); } get level() { return this._level; } set level(level) { this._level = level; this._notifyListeners(); } get colors() { return this._colors; } set colors(color) { this._colors = color; this._colorsSet = true; this._inspectOptions.colors = color; this._notifyListeners(); } get format() { return this._format; } set format(format) { this._format = format === "fancy" ? "fancy" : "plain"; this._notifyListeners(); } get spinner() { return this._spinner; } set spinner(spinner) { this._spinner = spinner; this._notifyListeners(); } get lineLength() { return this._lineLength; } set lineLength(lineLength) { this._lineLength = lineLength; this._lineLengthSet = true; this._notifyListeners(); } get taskLength() { return this._taskLength; } set taskLength(taskLength) { this._taskLength = taskLength; this._notifyListeners(); } get indentSize() { return this._indentSize; } set indentSize(indentSize) { this._indentSize = indentSize; if (this._indentSize < 1) this._indentSize = 1; this._notifyListeners(); } get showSources() { return this._showSources; } set showSources(showSources) { this._showSources = showSources; this._notifyListeners(); } get githubAnnotations() { return this._githubAnnotations; } set githubAnnotations(githubAnnotations) { this._githubAnnotations = githubAnnotations; this._notifyListeners(); } get inspectOptions() { return new Proxy(this._inspectOptions, { get: (target, prop) => { if (prop === "colors") return this.colors; if (prop === "breakLength") return this._lineLength; return target[prop]; }, set: (target, prop, value) => { if (prop === "colors") { this.colors = !!value; } else if (prop === "breakLength") { const length = parseInt(value); if (isNaN(length)) return false; this.lineLength = length; } else { target[prop] = value; } this._notifyListeners(); return true; } }); } }; var optionsKey = Symbol.for("plugjs:plug:types:LogOptions"); var logOptions = getSingleton(optionsKey, () => new LogOptionsImpl()); export { logOptions }; //# sourceMappingURL=options.mjs.map