Skip to content

Commit

Permalink
- Added --config flag to support custom config file Fix: #61
Browse files Browse the repository at this point in the history
  • Loading branch information
iamvishnusankar committed Nov 12, 2020
1 parent 9f5466e commit 04b47d2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/next-sitemap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"build:esnext": "tsc --module esnext --outDir dist/esnext"
},
"dependencies": {
"@corex/deepmerge": "^2.4.24"
"@corex/deepmerge": "^2.4.24",
"minimist": "^1.2.5"
}
}
11 changes: 9 additions & 2 deletions packages/next-sitemap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@ import { loadManifest } from './manifest'
import { createUrlSet, generateUrl } from './url'
import { generateSitemap } from './sitemap'
import { toChunks } from './array'
import { resolveSitemapChunks, KNOWN_PATHS, getRuntimePaths } from './path'
import {
resolveSitemapChunks,
getRuntimePaths,
getConfigFilePath,
} from './path'
import { exportRobotsTxt } from './robots-txt'

// Get config file path
const configFilePath = getConfigFilePath()

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

// Get runtime paths
const runtimePaths = getRuntimePaths(config)
Expand Down
16 changes: 16 additions & 0 deletions packages/next-sitemap/src/path/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
IRuntimePaths,
ISitemapFiled,
} from '../interface'
import minimist from 'minimist'
import fs from 'fs'

export const getPath = (...pathSegment: string[]): string => {
return path.resolve(process.cwd(), ...pathSegment)
Expand Down Expand Up @@ -38,6 +40,20 @@ export const getRuntimePaths = (config: IConfig): IRuntimePaths => {
}
}

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

export const getConfigFilePath = () => {
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.`)
}

return configPath
}

0 comments on commit 04b47d2

Please sign in to comment.