UNPKG

leasot

Version:

Parse and output TODOs and FIXMEs from comments in your files

51 lines (50 loc) 1.59 kB
import logSymbols from 'log-symbols'; import chalk from 'chalk'; import stripAnsi from 'strip-ansi'; import table from 'text-table'; import { EOL } from 'os'; import eol from 'eol'; function outputFooter(todos) { const total = todos.length; if (!total) { return EOL + ' ' + logSymbols.success + ' No todos/fixmes found'; } let msg = total + ' todo' + (total === 1 ? '' : 's'); msg += '/fixme' + (total === 1 ? '' : 's') + ' found'; return EOL + ' ' + (total ? logSymbols.error : logSymbols.success) + ' ' + msg; } function outputTable(todos) { let contents = ''; const headers = []; let previousFile; const mapTodo = (item, index) => { let text = chalk.cyan(item.text); if (item.ref) { text = chalk.gray('@' + item.ref) + ' ' + text; } const line = ['', chalk.gray('line ' + item.line), chalk.green(item.tag), text]; if (item.file !== previousFile) { headers[index] = item.file; } previousFile = item.file; return line; }; let t = table(todos.map(mapTodo), { stringLength(str) { return stripAnsi(str).length; }, }); //set filename headers t = eol .split(t) .map(function (el, i) { return headers[i] ? EOL + chalk.underline(headers[i]) + EOL + el : el; }) .join(EOL); contents += t + EOL; return contents; } /** * Report the items using a formatted table (Useful for CLI) */ export const reporter = (todos) => [outputTable(todos), outputFooter(todos)].join('');