Skip to content

Commit

Permalink
Async ops
Browse files Browse the repository at this point in the history
  • Loading branch information
iamvishnusankar committed Jan 30, 2022
1 parent 75aaf69 commit f7cb50c
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 33 deletions.
10 changes: 5 additions & 5 deletions packages/next-sitemap/src/cli.ts
Expand Up @@ -19,19 +19,19 @@ const main = async () => {
const configFilePath = 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)
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 Down Expand Up @@ -66,11 +66,11 @@ const main = async () => {
])

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

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

Expand Down
10 changes: 5 additions & 5 deletions packages/next-sitemap/src/config/index.ts
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
49 changes: 39 additions & 10 deletions packages/next-sitemap/src/file/index.ts
@@ -1,22 +1,51 @@
/* 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'

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 (await import(path)).default
}

// 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)

// Create folder if folder not exist
if (!stat.isDirectory()) {
await fs.mkdir(folder)
}

fs.writeFileSync(filePath, content)
return fs.writeFile(filePath, content)
}
31 changes: 24 additions & 7 deletions packages/next-sitemap/src/manifest/index.ts
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,
}
}
15 changes: 10 additions & 5 deletions packages/next-sitemap/src/robots-txt/export/index.ts
Expand Up @@ -2,15 +2,20 @@ import { IConfig, IRuntimePaths } from '../../interface'
import { generateRobotsTxt } from '../generate'
import { exportFile } from '../../file'

export const exportRobotsTxt = (
/**
* Export robots txt file
* @param runtimePaths
* @param config
*/
export const exportRobotsTxt = async (
runtimePaths: IRuntimePaths,
config: IConfig
): void => {
// generate robots text
): Promise<any> => {
// Generate robots text
const robotsTxt = generateRobotsTxt(config)

// create file
// Create file
if (robotsTxt) {
exportFile(runtimePaths.ROBOTS_TXT_FILE, robotsTxt)
await exportFile(runtimePaths.ROBOTS_TXT_FILE, robotsTxt)
}
}
9 changes: 8 additions & 1 deletion packages/next-sitemap/src/sitemap-index/export.ts
Expand Up @@ -3,14 +3,21 @@ import { exportFile } from '../file'
import type { IRuntimePaths, IConfig } from '../interface'
import { generateSitemapIndexXml } from './generate'

export const exportSitemapIndex = (
/**
* Export sitemap index file
* @param runtimePaths
* @param config
* @returns
*/
export const exportSitemapIndex = async (
runtimePaths: IRuntimePaths,
config: IConfig
) => {
// Remove first entry from additionalSitemaps (Index sitemap)
const [indexEntry, ...restSitemaps] =
config?.robotsTxtOptions?.additionalSitemaps ?? []

// Generate sitemap index content
const content = generateSitemapIndexXml(restSitemaps)

return exportFile(runtimePaths.SITEMAP_INDEX_FILE, content)
Expand Down

0 comments on commit f7cb50c

Please sign in to comment.