Skip to content

Commit

Permalink
Merge pull request #506 from iamvishnusankar/feat/improve-readability
Browse files Browse the repository at this point in the history
[Feat] Improve code readability
  • Loading branch information
iamvishnusankar committed Oct 17, 2022
2 parents 7864dec + cf60734 commit 016c755
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 46 deletions.
25 changes: 25 additions & 0 deletions packages/next-sitemap/src/builders/exportable-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import type {
IConfig,
IExportable,
INextSitemapResult,
IRuntimePaths,
ISitemapField,
} from '../interface.js'
Expand All @@ -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[] = []
Expand Down Expand Up @@ -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<INextSitemapResult> {
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(),
}
}
}
2 changes: 1 addition & 1 deletion packages/next-sitemap/src/builders/sitemap-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class SitemapBuilder {
return [
'<?xml version="1.0" encoding="UTF-8"?>',
'<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',
allSitemaps?.map((x) => `<sitemap><loc>${x}</loc></sitemap>`).join('\n'),
...(allSitemaps?.map((x) => `<sitemap><loc>${x}</loc></sitemap>`) ?? []),
'</sitemapindex>',
].join('\n')
}
Expand Down
51 changes: 10 additions & 41 deletions packages/next-sitemap/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,25 @@
/* 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 {
/**
* Main method
* @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!)
Expand All @@ -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)
}
}
29 changes: 25 additions & 4 deletions packages/next-sitemap/src/parsers/config-parser.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -11,7 +11,7 @@ export class ConfigParser {
* @param runtimePaths
* @returns
*/
async getRuntimeConfig(
private async getRuntimeConfig(
runtimePaths: IRuntimePaths
): Promise<Partial<IConfig>> {
const exportMarkerConfig = await loadJSON<IExportMarker>(
Expand All @@ -33,7 +33,7 @@ export class ConfigParser {
* @param runtimePaths
* @returns
*/
async withRuntimeConfig(
private async withRuntimeConfig(
config: IConfig,
runtimePaths: IRuntimePaths
): Promise<IConfig> {
Expand All @@ -53,7 +53,7 @@ export class ConfigParser {
* Load next-sitemap.config.js as module
* @returns
*/
async loadBaseConfig(): Promise<IConfig> {
private async loadBaseConfig(): Promise<IConfig> {
// Get config file path
const path = await getConfigFilePath()

Expand All @@ -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,
}
}
}

0 comments on commit 016c755

Please sign in to comment.