Skip to content

Commit

Permalink
Misc refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
iamvishnusankar committed Jan 30, 2022
1 parent 17c5af0 commit efc7117
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 27 deletions.
Empty file.
7 changes: 5 additions & 2 deletions packages/next-sitemap/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import {
import { exportRobotsTxt } from './robots-txt'
import { merge } from '@corex/deepmerge'
import { exportSitemapIndex } from './sitemap-index/export'
import { Logger } from './logger'

// Async main
const main = async () => {
// Get config file path
const configFilePath = getConfigFilePath()
const configFilePath = await getConfigFilePath()

// Load next-sitemap.js
let config = await loadConfig(configFilePath)
Expand Down Expand Up @@ -80,7 +81,9 @@ const main = async () => {
if (config.generateRobotsTxt) {
await exportRobotsTxt(runtimePaths, config)
}

return allSitemaps
}

// Execute
main()
main().then(Logger.generationCompleted)
14 changes: 3 additions & 11 deletions packages/next-sitemap/src/file/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,9 @@ export const exportFile = async (
// Target folder
const folder = path.dirname(filePath)

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

// Create folder if folder not exist
if (!stat.isDirectory()) {
await fs.mkdir(folder)
}
// Get file stat and create export folder if it doesn't exist
await fs.stat(folder).catch(async () => fs.mkdir(folder))

// Write file
return fs.writeFile(filePath, content)
}
18 changes: 18 additions & 0 deletions packages/next-sitemap/src/logger/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export class Logger {
/**
* Log stats when the generation is completed
* @param allSitemaps
* @returns
*/
static generationCompleted(allSitemaps: string[]) {
// Initial stats
console.log(
`✅ [next-sitemap] Generated index sitemap and ${
allSitemaps?.length - 1
} sitemap(s)`
)

// log all sitemap list
return allSitemaps?.forEach((x) => console.log(` ○ ${x}`))
}
}
48 changes: 34 additions & 14 deletions packages/next-sitemap/src/path/index.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import path from 'path'
import {
import type {
ISitemapChunk,
IConfig,
IRuntimePaths,
ISitemapField,
} from '../interface'
import minimist from 'minimist'
import fs from 'fs'
import fs from 'node:fs/promises'
import path from 'node:path'
import { generateUrl } from '../url'

/**
* Return absolute path from path segments
* @param pathSegment
* @returns
*/
export const getPath = (...pathSegment: string[]): string => {
return path.resolve(process.cwd(), ...pathSegment)
}

/**
* Resolve sitemap chunk path
* @param indexSitemapPath
* @param chunks
* @param config
* @returns
*/
export const resolveSitemapChunks = (
indexSitemapPath: string,
chunks: ISitemapField[][],
config: IConfig
): ISitemapChunk[] => {
// Base directory of export folder
const folder = path.dirname(indexSitemapPath)

return chunks.map((chunk, index) => {
Expand All @@ -33,6 +46,11 @@ export const resolveSitemapChunks = (
})
}

/**
* Return all runtime paths
* @param config
* @returns
*/
export const getRuntimePaths = (config: IConfig): IRuntimePaths => {
return {
BUILD_MANIFEST: getPath(config.sourceDir!, 'build-manifest.json'),
Expand All @@ -52,19 +70,21 @@ export const getRuntimePaths = (config: IConfig): IRuntimePaths => {
}

/**
* @deprecated Use getConfigFilePath instead
* Get config file path
* @returns
*/
export const KNOWN_PATHS = {
CONFIG_FILE: getPath('next-sitemap.js'),
}

export const getConfigFilePath = () => {
export const getConfigFilePath = async () => {
// Extract args from command
const args = minimist(process.argv.slice(2))
const configPath = getPath(args.config || 'next-sitemap.js')

if (!fs.existsSync(configPath)) {
throw new Error(`${configPath} does not exist.`)
}
// Config file path
const configPath = getPath(args.config || 'next-sitemap.js')

return configPath
// Check file stat
return fs
.stat(configPath)
.then(() => configPath)
.catch(() => {
throw new Error(`${configPath} does not exist.`)
})
}

0 comments on commit efc7117

Please sign in to comment.