@plugjs/plug
Version:
PlugJS Build System ===================
182 lines • 6.28 kB
JavaScript
import { EventEmitter } from 'node:events';
import { getSingleton } from "../utils/singleton.js";
import { getLevelNumber, NOTICE } from "./levels.js";
/* ========================================================================== *
* INTERNAL STATE *
* ========================================================================== */
class LogOptionsImpl 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();
/* Lotsa subscribers... */
this.setMaxListeners(20);
/* The `LOG_LEVEL` variable is one of our `debug`, `info`, ... */
if (process.env['LOG_LEVEL']) {
this._level = getLevelNumber(process.env['LOG_LEVEL']);
}
/* If the `LOG_COLORS` variable is specified, it should be `true` or `false` */
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;
// Other values don't change the value of `options.colors`
}
/* If the `GITHUB_ACTIONS` is `true` then enable annotations and use plain logs */
this._githubAnnotations = process.env['GITHUB_ACTIONS'] === 'true';
if (this._githubAnnotations) {
this._colors = true;
this._format = 'plain';
this._spinner = false;
}
/*
* The `__LOG_OPTIONS` variable is a JSON-serialized `LogOptions` object
* and it's processed _last_ as it's normally only created by fork below
* and consumed by the `Exec` plug (which has no other way of communicating)
*/
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;
},
});
}
}
/** Singleton key for {@link LogOptions} instance. */
const optionsKey = Symbol.for('plugjs:plug:types:LogOptions');
/** Shared instance of our {@link LogOptions}. */
export const logOptions = getSingleton(optionsKey, () => new LogOptionsImpl());
//# sourceMappingURL=options.js.map