path.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* eslint-disable @typescript-eslint/no-non-null-assertion */
  2. /* eslint-disable @typescript-eslint/explicit-module-boundary-types */
  3. import minimist from 'minimist';
  4. import fs from 'node:fs/promises';
  5. import path from 'node:path';
  6. import { Logger } from '../logger.js';
  7. import { generateUrl } from './url.js';
  8. import { pathToFileURL } from 'url';
  9. /**
  10. * Return absolute path from path segments
  11. * @param pathSegment
  12. * @returns
  13. */
  14. export const getPath = (...pathSegment) => {
  15. return path.resolve(process.cwd(), ...pathSegment);
  16. };
  17. /**
  18. * Return all runtime paths
  19. * @param config
  20. * @returns
  21. */
  22. export const getRuntimePaths = (config) => {
  23. // Check whether user enabled index sitemap or not
  24. const sitemapIndexEnabled = config?.generateIndexSitemap;
  25. // Set sitemap index file
  26. const SITEMAP_INDEX_FILE = sitemapIndexEnabled
  27. ? getPath(config.outDir, `${config.sitemapBaseFileName}.xml`)
  28. : undefined;
  29. // Set sitemap index url
  30. const SITEMAP_INDEX_URL = sitemapIndexEnabled
  31. ? generateUrl(config?.siteUrl, `${config.sitemapBaseFileName}.xml`)
  32. : undefined;
  33. return {
  34. BUILD_MANIFEST: getPath(config.sourceDir, 'build-manifest.json'),
  35. PRERENDER_MANIFEST: getPath(config.sourceDir, 'prerender-manifest.json'),
  36. ROUTES_MANIFEST: getPath(config.sourceDir, 'routes-manifest.json'),
  37. EXPORT_MARKER: getPath(config.sourceDir, 'export-marker.json'),
  38. ROBOTS_TXT_FILE: getPath(config.outDir, 'robots.txt'),
  39. SITEMAP_INDEX_URL,
  40. SITEMAP_INDEX_FILE,
  41. };
  42. };
  43. /**
  44. * Get config file path
  45. * @returns
  46. */
  47. export const getConfigFilePath = async () => {
  48. // Extract args from command
  49. const args = minimist(process.argv.slice(2));
  50. // Config file path
  51. const configPath = getPath(args.config || 'next-sitemap.config.js');
  52. // Check file stat
  53. return fs
  54. .stat(configPath)
  55. .then(() => pathToFileURL(configPath).toString())
  56. .catch((err) => {
  57. Logger.noConfigFile();
  58. throw err;
  59. });
  60. };