Skip to content

Commit

Permalink
Merge pull request #278 from iamvishnusankar/refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
iamvishnusankar committed Jan 30, 2022
2 parents 57d3622 + 72f62db commit 3b38eec
Show file tree
Hide file tree
Showing 17 changed files with 206 additions and 10,096 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,5 @@ coverage
dist
junit.xml
tsconfig.tsbuildinfo
public
**/public
**/public
2 changes: 1 addition & 1 deletion azure-pipeline.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: 2.0$(rev:.r)
name: 2.1$(rev:.r)
trigger:
branches:
include:
Expand Down
1 change: 1 addition & 0 deletions examples/basic/next-sitemap.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = {
siteUrl: process.env.SITE_URL || 'https://example.com',
generateRobotsTxt: true,
sitemapSize: 1000,
// optional
robotsTxtOptions: {
additionalSitemaps: [
Expand Down
15 changes: 0 additions & 15 deletions examples/basic/public/robots.txt

This file was deleted.

5,003 changes: 0 additions & 5,003 deletions examples/basic/public/sitemap-0.xml

This file was deleted.

5,003 changes: 0 additions & 5,003 deletions examples/basic/public/sitemap-1.xml

This file was deleted.

5 changes: 0 additions & 5 deletions examples/basic/public/sitemap-2.xml

This file was deleted.

9 changes: 0 additions & 9 deletions examples/basic/public/sitemap.xml

This file was deleted.

44 changes: 29 additions & 15 deletions packages/next-sitemap/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,28 @@ import {
} from './path'
import { exportRobotsTxt } from './robots-txt'
import { merge } from '@corex/deepmerge'
import { exportSitemapIndex } from './sitemap-index/export'
import { Logger } from './logger'

// Async main
import { exportSitemapIndex } from './sitemap-index/export'
;(async () => {
const main = async () => {
// Get config file path
const configFilePath = getConfigFilePath()
const configFilePath = await getConfigFilePath()

// Load next-sitemap.js
let config = loadConfig(configFilePath)
let config = await loadConfig(configFilePath)

// Get runtime paths
const runtimePaths = getRuntimePaths(config)

// get runtime config
const runtimeConfig = getRuntimeConfig(runtimePaths)
// Get runtime config
const runtimeConfig = await getRuntimeConfig(runtimePaths)

// Update config with runtime config
config = updateConfig(config, runtimeConfig)

// Load next.js manifest files
const manifest = loadManifest(runtimePaths)
const manifest = await loadManifest(runtimePaths)

// Create url-set based on config and manifest
const urlSet = await createUrlSet(config, manifest)
Expand All @@ -50,13 +51,21 @@ import { exportSitemapIndex } from './sitemap-index/export'
const allSitemaps: string[] = [runtimePaths.SITEMAP_INDEX_URL]

// Generate sitemaps from chunks
sitemapChunks.forEach((chunk) => {
generateSitemap(chunk)
allSitemaps.push(generateUrl(config.siteUrl, `/${chunk.filename}`))
})
await Promise.all(
sitemapChunks.map(async (chunk) => {
// Get sitemap absolute url
const sitemapUrl = generateUrl(config.siteUrl, `/${chunk.filename}`)

// Add generate sitemap to sitemap list
allSitemaps.push(sitemapUrl)

// Generate sitemap
return generateSitemap(chunk)
})
)

// combine-merge allSitemaps with user-provided additionalSitemaps
const updatedConfig = merge([
config = merge([
{
robotsTxtOptions: {
additionalSitemaps: allSitemaps,
Expand All @@ -66,10 +75,15 @@ import { exportSitemapIndex } from './sitemap-index/export'
])

// Export sitemap index file
exportSitemapIndex(runtimePaths, updatedConfig)
await exportSitemapIndex(runtimePaths, config)

// Generate robots.txt
if (config.generateRobotsTxt) {
exportRobotsTxt(runtimePaths, updatedConfig)
await exportRobotsTxt(runtimePaths, config)
}
})()

return allSitemaps
}

// Execute
main().then(Logger.generationCompleted)
10 changes: 5 additions & 5 deletions packages/next-sitemap/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {
import { merge } from '@corex/deepmerge'
import { loadFile } from '../file'

export const loadConfig = (path: string): IConfig => {
const baseConfig = loadFile<IConfig>(path)
export const loadConfig = async (path: string): Promise<IConfig> => {
const baseConfig = await loadFile<IConfig>(path)
return withDefaultConfig(baseConfig!)
}

Expand Down Expand Up @@ -62,10 +62,10 @@ export const withDefaultConfig = (config: Partial<IConfig>): IConfig => {
return updateConfig(defaultConfig, config)
}

export const getRuntimeConfig = (
export const getRuntimeConfig = async (
runtimePaths: IRuntimePaths
): Partial<IConfig> => {
const exportMarkerConfig = loadFile<IExportMarker>(
): Promise<Partial<IConfig>> => {
const exportMarkerConfig = await loadFile<IExportMarker>(
runtimePaths.EXPORT_MARKER,
false
)
Expand Down
55 changes: 45 additions & 10 deletions packages/next-sitemap/src/file/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,57 @@
/* eslint-disable @typescript-eslint/no-var-requires */
import fs from 'fs'
import path from 'path'
import fs from 'node:fs/promises'
import path from 'node:path'
import { Logger } from '../logger'

export const loadFile = <T>(path: string, throwError = true): T | undefined => {
if (fs.existsSync(path)) {
return require(path) as T
/**
* Load file
* @param path
* @param throwError
* @returns
*/
export const loadFile = async <T>(
path: string,
throwError = true
): Promise<T | undefined> => {
// Get path stat
const stat = await fs.stat(path)

// Import and return if the file exist
if (stat.isFile()) {
return require(path)
}

// Handle error
if (throwError) {
new Error(`${path} does not exist.`)
throw new Error(`${path} does not exist.`)
}
}

export const exportFile = (filePath: string, content: string): void => {
/**
* Export file
* @param filePath
* @param content
* @returns
*/
export const exportFile = async (
filePath: string,
content: string
): Promise<any> => {
// Target folder
const folder = path.dirname(filePath)
if (!fs.existsSync(folder)) {
fs.mkdirSync(folder)

// Get file stat
const stat = await fs.stat(folder).catch(() => ({
isDirectory: () => false,
}))

// Directory
if (!stat.isDirectory()) {
await fs.mkdir(folder).catch(() => {
return
})
}

fs.writeFileSync(filePath, content)
// Write file
return fs.writeFile(filePath, content)
}
45 changes: 45 additions & 0 deletions packages/next-sitemap/src/logger/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Generic console logger
*/
export class Logger {
/**
* Generic log
* @param arg0
* @param filePath
*/
static log(emoji: string, ...text: string[]): any {
return console.log(emoji, `[next-sitemap]`, ...text)
}

/**
* Log stats when the generation is completed
* @param allSitemaps
* @returns
*/
static generationCompleted(allSitemaps: string[]) {
// Initial stats
Logger.log(
`✅`,
`Generated index sitemap and ${allSitemaps?.length - 1} sitemap(s)`
)

// Temp assign
let sitemapsList = allSitemaps

// Only show 5 entries on console
if (sitemapsList?.length > 7) {
sitemapsList = [
...sitemapsList.splice(0, 3),
'...',
...sitemapsList.splice(sitemapsList.length - 2, 2),
]
}

// log all sitemap list
return sitemapsList?.forEach((x, index) =>
x === '...'
? console.log(` ${x}`)
: console.log(` ○ ${x}`, index === 0 ? '(index)' : '')
)
}
}
31 changes: 24 additions & 7 deletions packages/next-sitemap/src/manifest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,36 @@ import {
} from '../interface'
import { loadFile } from '../file'

export const loadManifest = (runtimePaths: IRuntimePaths): INextManifest => {
const build = loadFile<IBuildManifest>(runtimePaths.BUILD_MANIFEST)!
export const loadManifest = async (
runtimePaths: IRuntimePaths
): Promise<INextManifest> => {
// Load build manifest
const buildManifest = await loadFile<IBuildManifest>(
runtimePaths.BUILD_MANIFEST
)!

const preRender = loadFile<IPreRenderManifest>(
// Throw error if no build manifest exist
if (!buildManifest) {
throw new Error(
'Unable to find build manifest, make sure to build your next project before running next-sitemap command'
)
}

// Load pre-render manifest
const preRenderManifest = await loadFile<IPreRenderManifest>(
runtimePaths.PRERENDER_MANIFEST,
false
)

const routes = loadFile<IRoutesManifest>(runtimePaths.ROUTES_MANIFEST, false)
// Load routes manifest
const routesManifest = await loadFile<IRoutesManifest>(
runtimePaths.ROUTES_MANIFEST,
false
)

return {
build,
preRender,
routes,
build: buildManifest,
preRender: preRenderManifest,
routes: routesManifest,
}
}

0 comments on commit 3b38eec

Please sign in to comment.