Skip to content

Commit

Permalink
feat: add hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
CBW2007 committed Jan 22, 2022
1 parent 259c2bc commit cfab1e9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
14 changes: 11 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,23 @@ const xmlDeclaration = '<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet t
const xmlns = 'http://www.sitemaps.org/schemas/sitemap/0.9'

class SitemapManager {
options: Options
options: FullOptions
#urlDatas: {
[category: string]: string
} = {}

#isFinished: Boolean = false

constructor (options: Options) {
this.options = options
this.options = {
outDir: options.outDir || './public',
siteURL: options.siteURL,
pathPrefix: options.pathPrefix || '',
hooks: {
warningHandler: options.hooks?.warningHandler || utils.defaultHookSet.defaultWarningHandler,
fileHandler: options.hooks?.fileHandler || utils.defaultHookSet.defaultFileHandler
}
}
}

addUrl (category: string, url: UrlObj[] | UrlObj): void {
Expand All @@ -40,7 +48,7 @@ class SitemapManager {
xmlContents = xmlContents + `<sitemap>${utils.objToString({ loc: new URL(path.join(this.options.pathPrefix || '', `sitemap-${category}.xml`), this.options.siteURL), lastmod: new Date() })}</sitemap>`
}
try {
fs.writeFileSync(
this.options.hooks.fileHandler(
path.resolve(this.options?.outDir || './public', 'sitemap.xml'),
xmlDeclaration + `<sitemapindex xmlns="${xmlns}">${xmlContents}</sitemapindex>`
)
Expand Down
22 changes: 21 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
import { URL } from 'url'

export type WarningHandler = (msg: string) => void

export type FileHandler = (file: string, data: string) => void

interface HookSet {
warningHandler?: WarningHandler,
fileHandler?: FileHandler
}

export interface FullOptions {
outDir: string,
siteURL: string,
pathPrefix: string,
hooks: {
warningHandler: WarningHandler,
fileHandler: FileHandler
}
}

export interface Options {
outDir?: string,
siteURL: string,
pathPrefix?: string
pathPrefix?: string,
hooks?: HookSet
}

export interface UrlObj {
Expand Down
14 changes: 13 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import { UrlObj } from './types'
import { UrlObj, WarningHandler, FileHandler } from './types'
import * as fs from 'fs'

const freqMap = ['', 'never', 'yearly', 'monthly', 'weekly', 'daily', 'hourly', 'always']

const defaultWarningHandler: WarningHandler = (msg) => {
throw new Error(msg)
}

const defaultFileHandler: FileHandler = fs.writeFileSync

export const defaultHookSet = {
defaultWarningHandler,
defaultFileHandler
}

export function dateConverter (date: Date | string): string {
return new Date(date).toISOString()
}
Expand Down

0 comments on commit cfab1e9

Please sign in to comment.