UNPKG

sass-formatter

Version:
123 lines (122 loc) 4.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LogDebugResult = LogDebugResult; exports.ResetDebugLog = ResetDebugLog; exports.SetDebugLOCAL_CONTEXT = SetDebugLOCAL_CONTEXT; exports.SetConvertData = SetConvertData; exports.PushDebugInfo = PushDebugInfo; const suf_log_1 = require("suf-log"); (0, suf_log_1.SetEnvironment)('node'); const colon = (0, suf_log_1.styler)(':', '#777'); // const quote = styler('"', '#f64'); const pipe = (0, suf_log_1.styler)('|', '#f64'); const TEXT = (text) => (0, suf_log_1.styler)(text, '#eee'); const NUMBER = (number) => (0, suf_log_1.styler)(number.toString(), '#f03'); const BOOL = (bool) => (0, suf_log_1.styler)(bool.toString(), bool ? '#4f6' : '#f03'); function LogDebugResult(result) { const data = StoreLog.logs; let out = (0, suf_log_1.styler)('FORMAT', { "font-weight": 'bold', color: '#0af' }); for (let i = 0; i < data.length; i++) { out += '\n'; out += InfoLogHelper(data[i]); } out += ` ${pipe}${(0, suf_log_1.styler)(replaceWhitespace(result.replace(/\n/g, '|\n|')), '#c76')}${pipe}`; console.log(out); } function ResetDebugLog() { StoreLog.reset(); } class StoreLog { static tempConvertData; static tempLOCAL_CONTEXT; static resetTemp() { this.tempConvertData = undefined; this.tempLOCAL_CONTEXT = undefined; } static logs = []; static reset() { this.resetTemp(); this.logs = []; } } function SetDebugLOCAL_CONTEXT(data) { StoreLog.tempLOCAL_CONTEXT = data; } function SetConvertData(data) { StoreLog.tempConvertData = data; } function PushDebugInfo(info) { if (info.debug) { StoreLog.logs.push({ info, convertData: StoreLog.tempConvertData, LOCAL_CONTEXT: StoreLog.tempLOCAL_CONTEXT, }); } StoreLog.resetTemp(); } function InfoLogHelper(data) { const { convertData, info, LOCAL_CONTEXT } = data; const notProvided = null; const title = (0, suf_log_1.styler)(info.title, '#cc0'); const lineNumber = `${TEXT('Line Number')}${colon} ${NUMBER(info.lineNumber)}`; const offset = info.offset !== undefined ? `${TEXT('Offset')}${colon} ${NUMBER(info.offset)}` : ''; const originalOffset = info.originalOffset !== undefined ? `${TEXT('Original Offset')}${colon} ${NUMBER(info.originalOffset)}` : ''; const nextLine = info.nextLine !== undefined ? JSON.stringify(info.nextLine) .replace(/[{}]/g, '') .replace(/:/g, ': ') .replace(/,/g, ', ') .replace(/".*?"/g, (s) => { return (0, suf_log_1.styler)(s, '#c76'); }) : notProvided; const replace = info.replaceSpaceOrTabs !== undefined ? BOOL(info.replaceSpaceOrTabs) : notProvided; const CONVERT = convertData ? ` ${TEXT('Convert')} ${colon} ${(0, suf_log_1.styler)(convertData.type, '#f64')}` : ''; const newText = info.newLineText ? `\n ${TEXT('New')} ${colon} ${(0, suf_log_1.styler)(replaceWhitespace(info.newLineText.replace(/\n/g, '\\n')), '#0af')}` : ''; switch (info.newLineText) { case 'DELETED': return ` ${title} ${lineNumber} ${TEXT('Next Line')}${colon} ${nextLine}`; case 'NEWLINE': case 'NULL': return ` ${title} ${lineNumber}`; default: let data = ''; data += nextLine !== null ? `\n ${TEXT('Next Line')} ${colon} ${nextLine}` : ''; data += replace !== null ? `\n ${TEXT('Replace')} ${colon} ${replace}` : ''; if (LOCAL_CONTEXT) { data += `\n ${(0, suf_log_1.styler)('LOCAL_CONTEXT', '#f64')} ${(0, suf_log_1.styler)('{', '#777')}`; for (const key in LOCAL_CONTEXT) { if (Object.prototype.hasOwnProperty.call(LOCAL_CONTEXT, key)) { const val = LOCAL_CONTEXT[key]; data += `\n ${(0, suf_log_1.styler)(key, '#777')}${colon} ${parseValue(val)}`; } } data += (0, suf_log_1.styler)('\n }', '#777'); } return ` ${title} ${lineNumber} ${offset} ${originalOffset} ${TEXT('Old')} ${colon} ${(0, suf_log_1.styler)(replaceWhitespace(info.oldLineText), '#d75')}${newText}${CONVERT}${data}`; } } function replaceWhitespace(text) { return text.replace(/ /g, '·').replace(/\t/g, '⟶'); } function parseValue(val) { const type = typeof val; if (type === 'boolean') { return BOOL(val); } else if (type === 'string') { return (0, suf_log_1.styler)(val, '#f64'); } else if (type === 'object') { return (0, suf_log_1.styler)(JSON.stringify(val), '#0af'); } return val; }