default.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import figures from 'figures';
  2. import {
  3. gray,
  4. bold,
  5. redBright,
  6. yellowBright,
  7. } from 'yoctocolors';
  8. // Default when `verbose` is not a function
  9. export const defaultVerboseFunction = ({
  10. type,
  11. message,
  12. timestamp,
  13. piped,
  14. commandId,
  15. result: {failed = false} = {},
  16. options: {reject = true},
  17. }) => {
  18. const timestampString = serializeTimestamp(timestamp);
  19. const icon = ICONS[type]({failed, reject, piped});
  20. const color = COLORS[type]({reject});
  21. return `${gray(`[${timestampString}]`)} ${gray(`[${commandId}]`)} ${color(icon)} ${color(message)}`;
  22. };
  23. // Prepending the timestamp allows debugging the slow paths of a subprocess
  24. const serializeTimestamp = timestamp => `${padField(timestamp.getHours(), 2)}:${padField(timestamp.getMinutes(), 2)}:${padField(timestamp.getSeconds(), 2)}.${padField(timestamp.getMilliseconds(), 3)}`;
  25. const padField = (field, padding) => String(field).padStart(padding, '0');
  26. const getFinalIcon = ({failed, reject}) => {
  27. if (!failed) {
  28. return figures.tick;
  29. }
  30. return reject ? figures.cross : figures.warning;
  31. };
  32. const ICONS = {
  33. command: ({piped}) => piped ? '|' : '$',
  34. output: () => ' ',
  35. ipc: () => '*',
  36. error: getFinalIcon,
  37. duration: getFinalIcon,
  38. };
  39. const identity = string => string;
  40. const COLORS = {
  41. command: () => bold,
  42. output: () => identity,
  43. ipc: () => identity,
  44. error: ({reject}) => reject ? redBright : yellowBright,
  45. duration: () => gray,
  46. };