UNPKG

3.61 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.simpleSitemapAndIndex = void 0;
4const index_1 = require("../index");
5const zlib_1 = require("zlib");
6const fs_1 = require("fs");
7const path_1 = require("path");
8const stream_1 = require("stream");
9const util_1 = require("util");
10const url_1 = require("url");
11const pipeline = util_1.promisify(stream_1.pipeline);
12/**
13 *
14 * @param {object} options -
15 * @param {string} options.hostname - The hostname for all URLs
16 * @param {string} [options.sitemapHostname] - The hostname for the sitemaps if different than hostname
17 * @param {SitemapItemLoose[] | string | Readable | string[]} options.sourceData - The urls you want to make a sitemap out of.
18 * @param {string} options.destinationDir - where to write the sitemaps and index
19 * @param {string} [options.publicBasePath] - where the sitemaps are relative to the hostname. Defaults to root.
20 * @param {number} [options.limit] - how many URLs to write before switching to a new file. Defaults to 50k
21 * @param {boolean} [options.gzip] - whether to compress the written files. Defaults to true
22 * @returns {Promise<void>} an empty promise that resolves when everything is done
23 */
24const simpleSitemapAndIndex = async ({ hostname, sitemapHostname = hostname, // if different
25/**
26 * Pass a line separated list of sitemap items or a stream or an array
27 */
28sourceData, destinationDir, limit = 50000, gzip = true, publicBasePath = './', }) => {
29 await fs_1.promises.mkdir(destinationDir, { recursive: true });
30 const sitemapAndIndexStream = new index_1.SitemapAndIndexStream({
31 limit,
32 getSitemapStream: (i) => {
33 const sitemapStream = new index_1.SitemapStream({
34 hostname,
35 });
36 const path = `./sitemap-${i}.xml`;
37 const writePath = path_1.resolve(destinationDir, path + (gzip ? '.gz' : ''));
38 if (!publicBasePath.endsWith('/')) {
39 publicBasePath += '/';
40 }
41 const publicPath = path_1.normalize(publicBasePath + path);
42 let pipeline;
43 if (gzip) {
44 pipeline = sitemapStream
45 .pipe(zlib_1.createGzip()) // compress the output of the sitemap
46 .pipe(fs_1.createWriteStream(writePath)); // write it to sitemap-NUMBER.xml
47 }
48 else {
49 pipeline = sitemapStream.pipe(fs_1.createWriteStream(writePath)); // write it to sitemap-NUMBER.xml
50 }
51 return [
52 new url_1.URL(`${publicPath}${gzip ? '.gz' : ''}`, sitemapHostname).toString(),
53 sitemapStream,
54 pipeline,
55 ];
56 },
57 });
58 let src;
59 if (typeof sourceData === 'string') {
60 src = index_1.lineSeparatedURLsToSitemapOptions(fs_1.createReadStream(sourceData));
61 }
62 else if (sourceData instanceof stream_1.Readable) {
63 src = sourceData;
64 }
65 else if (Array.isArray(sourceData)) {
66 src = stream_1.Readable.from(sourceData);
67 }
68 else {
69 throw new Error("unhandled source type. You've passed in data that is not supported");
70 }
71 const writePath = path_1.resolve(destinationDir, `./sitemap-index.xml${gzip ? '.gz' : ''}`);
72 if (gzip) {
73 return pipeline(src, sitemapAndIndexStream, zlib_1.createGzip(), fs_1.createWriteStream(writePath));
74 }
75 else {
76 return pipeline(src, sitemapAndIndexStream, fs_1.createWriteStream(writePath));
77 }
78};
79exports.simpleSitemapAndIndex = simpleSitemapAndIndex;
80exports.default = exports.simpleSitemapAndIndex;