12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- import { Logger } from '../logger.js';
- import { withDefaultConfig } from '../utils/defaults.js';
- import { getConfigFilePath, getRuntimePaths } from '../utils/path.js';
- import { overwriteMerge } from '../utils/merge.js';
- import { loadJSON } from '../utils/file.js';
- export class ConfigParser {
-
- async getRuntimeConfig(runtimePaths) {
- const exportMarkerConfig = await loadJSON(runtimePaths.EXPORT_MARKER, false).catch((err) => {
- Logger.noExportMarker();
- throw err;
- });
- return {
- trailingSlash: exportMarkerConfig?.exportTrailingSlash,
- };
- }
-
- async withRuntimeConfig(config, runtimePaths) {
-
- const runtimeConfig = await this.getRuntimeConfig(runtimePaths);
-
- const trailingSlashConfig = {};
- if ('trailingSlash' in config) {
- trailingSlashConfig.trailingSlash = config?.trailingSlash;
- }
- return overwriteMerge(config, runtimeConfig, trailingSlashConfig);
- }
-
- async loadBaseConfig() {
-
- const path = await getConfigFilePath();
-
- Logger.log('✨', `Loading next-sitemap config:`, path);
-
- const baseConfig = await import(path);
- if (!baseConfig.default) {
- throw new Error('Unable to next-sitemap config file');
- }
- return withDefaultConfig(baseConfig.default);
- }
-
- async loadConfig() {
-
- const baseConfig = await this.loadBaseConfig();
-
- const runtimePaths = getRuntimePaths(baseConfig);
-
- const config = await this.withRuntimeConfig(baseConfig, runtimePaths);
-
- return {
- config,
- runtimePaths,
- };
- }
- }
|