cli.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /* eslint-disable @typescript-eslint/no-non-null-assertion */
  2. import { Logger } from './logger.js';
  3. import { toChunks } from './utils/array.js';
  4. import { ConfigParser } from './parsers/config-parser.js';
  5. import { ManifestParser } from './parsers/manifest-parser.js';
  6. import { UrlSetBuilder } from './builders/url-set-builder.js';
  7. import { ExportableBuilder } from './builders/exportable-builder.js';
  8. export class CLI {
  9. /**
  10. * Main method
  11. * @returns
  12. */
  13. async main() {
  14. // Load config from `next-sitemap.config.js` along with runtimePaths info
  15. const { config, runtimePaths } = await new ConfigParser().loadConfig();
  16. // Load next.js manifest
  17. const manifest = await new ManifestParser().loadManifest(runtimePaths);
  18. // Generate url set
  19. const urlSet = await new UrlSetBuilder(config, manifest).createUrlSet();
  20. // Split sitemap into multiple files
  21. const chunks = toChunks(urlSet, config.sitemapSize);
  22. // Create ExportableBuilder instance
  23. const expoBuilder = new ExportableBuilder(config, runtimePaths);
  24. // Register sitemap exports
  25. await expoBuilder.registerSitemaps(chunks);
  26. // Register index sitemap if user config allows generation
  27. if (config.generateIndexSitemap) {
  28. await expoBuilder.registerIndexSitemap();
  29. }
  30. // Register robots.txt export if user config allows generation
  31. if (config?.generateRobotsTxt) {
  32. await expoBuilder.registerRobotsTxt();
  33. }
  34. // Export all files
  35. return expoBuilder.exportAll();
  36. }
  37. /**
  38. * Execute and log result
  39. * @returns
  40. */
  41. async execute() {
  42. return this.main().then(Logger.generationCompleted);
  43. }
  44. }