All files / src/lib print-items-by-owner.js

36.84% Statements 14/38
100% Branches 1/1
0% Functions 0/1
36.84% Lines 14/38

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x                                                  
import { Logger } from '../logger.js';
import { styleText } from './style-text.js';
 
/**
 * Print a list of items grouped by owner with consistent formatting.
 * Items must have ownerId, ownerName and name properties.
 * @param {Array} items - The items to display
 * @param {Object} options
 * @param {string} options.itemName - Singular name for the items (e.g. "configuration provider")
 * @param {string} options.emptyCommand - Command suggestion shown when no items are found
 * @param {Function} options.getItemId - Function to get the item ID from an item
 * @param {Function} [options.getExtraLines] - Function returning extra detail lines to display below the item (nulls filtered)
 */
export function printItemsByOwner(items, { itemName, emptyCommand, getItemId, getExtraLines }) {
  if (items.length === 0) {
    Logger.println(`🔎 No ${itemName} found, create one with ${styleText('blue', emptyCommand)} command`);
    return;
  }

  const plural = items.length > 1 ? 's' : '';
  Logger.println(`🔎 Found ${items.length} ${itemName}${plural}:`);
  Logger.println();

  const groups = Object.groupBy(items, (item) => item.ownerId);
  for (const group of Object.values(groups)) {
    const { ownerId, ownerName } = group[0];
    Logger.println(`• ${styleText('bold', `${ownerId} (${ownerName})`)}`);
    for (const item of group) {
      Logger.println(`  • ${item.name || '(unnamed)'} ${styleText('grey', `(${getItemId(item)})`)}`);
      if (getExtraLines != null) {
        for (const line of [getExtraLines(item)].flat().filter(Boolean)) {
          Logger.println(`    ${line}`);
        }
      }
    }
    Logger.println();
  }
}