logger.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. * Generic console logger
  3. */
  4. export class Logger {
  5. /**
  6. * Missing build
  7. */
  8. static noExportMarker() {
  9. Logger.error('Unable to find export-maker.\nMake sure to build the project using `next build` command\n');
  10. }
  11. /**
  12. * Log missing config file
  13. */
  14. static noConfigFile() {
  15. Logger.error('Unable to find next-sitemap.config.js or custom config file.\n\nIMPORTANT: Default config file has been renamed to `next-sitemap.config.js`\n\nIf you are using custom config file, make sure to invoke `next-sitemap --config <custom-config-file>.js`\n');
  16. }
  17. /**
  18. * Generic error logger
  19. * @param text
  20. * @returns
  21. */
  22. static error(...text) {
  23. return console.error(`\x1b[31m`, `❌`, `[next-sitemap]`, ...text);
  24. }
  25. /**
  26. * Generic log
  27. * @param emoji
  28. * @param text
  29. */
  30. static log(emoji, ...text) {
  31. return console.log(emoji, `[next-sitemap]`, ...text);
  32. }
  33. static logList(title, list) {
  34. console.log(`-----------------------------------------------------\n`, title, `\n-----------------------------------------------------\n`);
  35. // Only show 5 entries on console
  36. if (list?.length > 7) {
  37. list = [...list.splice(0, 3), '...', ...list.splice(list.length - 2, 2)];
  38. }
  39. // log all sitemap list
  40. list?.forEach((x) => x === '...' ? console.log(` ${x}`) : console.log(` ○ ${x}`));
  41. console.log(`\n`);
  42. }
  43. /**
  44. * Log stats when the generation is completed
  45. * @param result
  46. * @returns
  47. */
  48. static generationCompleted(result) {
  49. // Initial stats
  50. Logger.log(`✅`, 'Generation completed');
  51. const indexCount = result.sitemapIndices.length;
  52. const sitemapCount = result.sitemaps.length;
  53. console.table({
  54. indexSitemaps: indexCount,
  55. sitemaps: sitemapCount,
  56. });
  57. // Log sitemap index list
  58. if (indexCount > 0) {
  59. Logger.logList('SITEMAP INDICES', result.sitemapIndices);
  60. }
  61. // Log sitemap list
  62. if (sitemapCount > 0) {
  63. Logger.logList('SITEMAPS', result.sitemaps);
  64. }
  65. }
  66. }