diff --git a/packages/next-sitemap/src/builders/exportable-builder.ts b/packages/next-sitemap/src/builders/exportable-builder.ts index ce5f1c61..c019ced7 100644 --- a/packages/next-sitemap/src/builders/exportable-builder.ts +++ b/packages/next-sitemap/src/builders/exportable-builder.ts @@ -2,6 +2,7 @@ import type { IConfig, IExportable, + INextSitemapResult, IRuntimePaths, ISitemapField, } from '../interface.js' @@ -11,6 +12,7 @@ import { generateUrl } from '../utils/url.js' import { combineMerge } from '../utils/merge.js' import { RobotsTxtBuilder } from './robots-txt-builder.js' import { defaultRobotsTxtTransformer } from '../utils/defaults.js' +import { exportFile } from '../utils/file.js' export class ExportableBuilder { exportableList: IExportable[] = [] @@ -206,7 +208,30 @@ export class ExportableBuilder { return this.exportableUrlReducer((x) => x.type == 'sitemap') } + /** + * Generate sitemap indices + * @returns + */ generatedSitemapIndices() { return this.exportableUrlReducer((x) => x.type == 'sitemap-index') } + + /** + * Export all registered files + * @returns + */ + async exportAll(): Promise { + await Promise.all( + this.exportableList?.map(async (item) => + exportFile(item.filename, item.content) + ) + ) + + // Create result object + return { + runtimePaths: this.runtimePaths, + sitemaps: this.generatedSitemaps(), + sitemapIndices: this.generatedSitemapIndices(), + } + } } diff --git a/packages/next-sitemap/src/cli.ts b/packages/next-sitemap/src/cli.ts index 0b7a4cde..bfad85d0 100644 --- a/packages/next-sitemap/src/cli.ts +++ b/packages/next-sitemap/src/cli.ts @@ -1,13 +1,10 @@ /* eslint-disable @typescript-eslint/no-non-null-assertion */ -import { INextSitemapResult } from './interface.js' import { Logger } from './logger.js' -import { getRuntimePaths } from './utils/path.js' import { toChunks } from './utils/array.js' import { ConfigParser } from './parsers/config-parser.js' import { ManifestParser } from './parsers/manifest-parser.js' import { UrlSetBuilder } from './builders/url-set-builder.js' import { ExportableBuilder } from './builders/exportable-builder.js' -import { exportFile } from './utils/file.js' export class CLI { /** @@ -15,29 +12,14 @@ export class CLI { * @returns */ async main() { - // Create config parser instance - const configParser = new ConfigParser() - - // Load base config from `next-sitemap.config.js` - let config = await configParser.loadBaseConfig() - - // Find the runtime paths using base config - const runtimePaths = getRuntimePaths(config) - - // Update base config with runtime config - config = await configParser.withRuntimeConfig(config, runtimePaths) - - // Create next.js manifest parser instance - const manifestParser = new ManifestParser() + // Load config from `next-sitemap.config.js` along with runtimePaths info + const { config, runtimePaths } = await new ConfigParser().loadConfig() // Load next.js manifest - const manifest = await manifestParser.loadManifest(runtimePaths) - - // Create UrlSetBuilder instance - const urlSetBuilder = new UrlSetBuilder(config, manifest) + const manifest = await new ManifestParser().loadManifest(runtimePaths) // Generate url set - const urlSet = await urlSetBuilder.createUrlSet() + const urlSet = await new UrlSetBuilder(config, manifest).createUrlSet() // Split sitemap into multiple files const chunks = toChunks(urlSet, config.sitemapSize!) @@ -59,27 +41,14 @@ export class CLI { } // Export all files - await Promise.all( - expoBuilder.exportableList?.map(async (item) => - exportFile(item.filename, item.content) - ) - ) - - // Create result object - const result: INextSitemapResult = { - runtimePaths, - sitemaps: expoBuilder.generatedSitemaps(), - sitemapIndices: expoBuilder.generatedSitemapIndices(), - } - - return result + return expoBuilder.exportAll() } + /** + * Execute and log result + * @returns + */ async execute() { - // Run main method - const result = await this.main() - - // Log result - Logger.generationCompleted(result) + return this.main().then(Logger.generationCompleted) } } diff --git a/packages/next-sitemap/src/parsers/config-parser.ts b/packages/next-sitemap/src/parsers/config-parser.ts index ffc2d9a9..c05d3015 100644 --- a/packages/next-sitemap/src/parsers/config-parser.ts +++ b/packages/next-sitemap/src/parsers/config-parser.ts @@ -1,6 +1,6 @@ import { Logger } from '../logger.js' import { withDefaultConfig } from '../utils/defaults.js' -import { getConfigFilePath } from '../utils/path.js' +import { getConfigFilePath, getRuntimePaths } from '../utils/path.js' import type { IConfig, IRuntimePaths, IExportMarker } from '../interface.js' import { overwriteMerge } from '../utils/merge.js' import { loadJSON } from '../utils/file.js' @@ -11,7 +11,7 @@ export class ConfigParser { * @param runtimePaths * @returns */ - async getRuntimeConfig( + private async getRuntimeConfig( runtimePaths: IRuntimePaths ): Promise> { const exportMarkerConfig = await loadJSON( @@ -33,7 +33,7 @@ export class ConfigParser { * @param runtimePaths * @returns */ - async withRuntimeConfig( + private async withRuntimeConfig( config: IConfig, runtimePaths: IRuntimePaths ): Promise { @@ -53,7 +53,7 @@ export class ConfigParser { * Load next-sitemap.config.js as module * @returns */ - async loadBaseConfig(): Promise { + private async loadBaseConfig(): Promise { // Get config file path const path = await getConfigFilePath() @@ -69,4 +69,25 @@ export class ConfigParser { return withDefaultConfig(baseConfig.default) } + + /** + * Load full config + * @returns + */ + async loadConfig() { + // Load base config + const baseConfig = await this.loadBaseConfig() + + // Find the runtime paths using base config + const runtimePaths = getRuntimePaths(baseConfig) + + // Update base config with runtime config + const config = await this.withRuntimeConfig(baseConfig, runtimePaths) + + // Return full result + return { + config, + runtimePaths, + } + } }