UNPKG

templates-mo

Version:

Templates is a scaffolding framework that makes code generation simple, dynamic, and reusable. Generate files, parts of your app, or whole project structures—without the repetitive copy-pasting

81 lines (80 loc) • 2.49 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.logFunctions = void 0; const debug_1 = __importDefault(require("debug")); require("./formatters"); const createDebugGroup_1 = __importDefault(require("./createDebugGroup")); exports.logFunctions = [ 'info', 'error', 'debug', 'success', 'warn', 'log', ]; class CreateDebug { constructor(name, opts = {}) { this.name = name; this._logger = (0, debug_1.default)(this.name); this.opts = Object.assign(Object.assign({}, CreateDebug.DEFAULT_OPTS), opts); this._groups = {}; exports.logFunctions.forEach((type) => { const instanceKey = `_${type}`; this[instanceKey] = this._logger.extend(type); this[instanceKey].color = this._logger.color; this[type] = (...args) => { this._resync(); this[instanceKey](...args); }; }); this._resync(); } _resync() { const { disableLog } = this.opts; exports.logFunctions.forEach((type) => { const instanceKey = `_${type}`; if (type === 'log') { // Log always is enabled this[instanceKey].enabled = !disableLog; } else { this[instanceKey].enabled = this.isEnabled(); } }); } isEnabled() { return this._logger.enabled; } enable() { this._logger.enabled = true; this._resync(); return this; } group(name, { clear = false } = {}) { if (this._groups[name] && !clear) { return this._groups[name]; } const newGroup = new createDebugGroup_1.default(name); this._groups[name] = newGroup; return newGroup; } printGroup(group) { let groupArray = null; if (typeof group === 'string') { groupArray = this._groups[group]; } else { groupArray = group; } for (let i = 0; i < groupArray.queue.length; i++) { const [level, ...args] = groupArray.queue[i]; this[level](...args); } this._groups[groupArray.name] = undefined; } } CreateDebug.DEFAULT_OPTS = { disableLog: false }; exports.default = CreateDebug;