Add sitemap generation utility
This commit is contained in:
Vendored
+2
@@ -3,6 +3,7 @@ import { program } from "commander";
|
||||
import start from "./start";
|
||||
import dev from "./dev";
|
||||
import build from "./build";
|
||||
import sitemap from "./sitemap";
|
||||
import { log } from "../utils/log";
|
||||
/**
|
||||
* # Describe Program
|
||||
@@ -17,6 +18,7 @@ program
|
||||
program.addCommand(dev());
|
||||
program.addCommand(start());
|
||||
program.addCommand(build());
|
||||
program.addCommand(sitemap());
|
||||
/**
|
||||
* # Handle Unavailable Commands
|
||||
*/
|
||||
|
||||
Vendored
+2
@@ -0,0 +1,2 @@
|
||||
import { Command } from "commander";
|
||||
export default function (): Command;
|
||||
Vendored
+39
@@ -0,0 +1,39 @@
|
||||
import { Command } from "commander";
|
||||
import { mkdirSync, writeFileSync } from "fs";
|
||||
import path from "path";
|
||||
import { log } from "../../utils/log";
|
||||
import grabDirNames from "../../utils/grab-dir-names";
|
||||
import grabConfig from "../../functions/grab-config";
|
||||
import grabConstants from "../../utils/grab-constants";
|
||||
import generateSitemap from "../../utils/generate-sitemap";
|
||||
export default function () {
|
||||
return new Command("sitemap")
|
||||
.description("Generate sitemap.xml from the pages directory")
|
||||
.option("-o, --output <path>", "Output file relative to the project root (default: public/sitemap.xml)")
|
||||
.option("--origin <url>", "Base URL for sitemap links")
|
||||
.action(async (options) => {
|
||||
const { ROOT_DIR, PUBLIC_DIR } = grabDirNames();
|
||||
global.BUNEXT_CONFIG = (await grabConfig()) || {};
|
||||
global.BUNEXT_CONSTANTS = grabConstants();
|
||||
const { output, ...sitemap_config } = global.BUNEXT_CONFIG.sitemap || {};
|
||||
try {
|
||||
const xml = await generateSitemap({
|
||||
...sitemap_config,
|
||||
origin: options.origin || sitemap_config.origin,
|
||||
});
|
||||
const output_path = options.output || output;
|
||||
const output_file = output_path
|
||||
? path.resolve(ROOT_DIR, output_path)
|
||||
: path.join(PUBLIC_DIR, "sitemap.xml");
|
||||
mkdirSync(path.dirname(output_file), { recursive: true });
|
||||
writeFileSync(output_file, xml, "utf-8");
|
||||
const url_count = xml.match(/<url>/g)?.length || 0;
|
||||
log.success(`Sitemap with ${url_count} URLs written to ${path.relative(ROOT_DIR, output_file)}`);
|
||||
}
|
||||
catch (error) {
|
||||
log.error(`Failed to generate sitemap: ${error?.message || error}`);
|
||||
process.exit(1);
|
||||
}
|
||||
process.exit();
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user