1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.simpleSitemapAndIndex = void 0;
|
4 | const index_1 = require("../index");
|
5 | const zlib_1 = require("zlib");
|
6 | const fs_1 = require("fs");
|
7 | const path_1 = require("path");
|
8 | const stream_1 = require("stream");
|
9 | const util_1 = require("util");
|
10 | const url_1 = require("url");
|
11 | const pipeline = util_1.promisify(stream_1.pipeline);
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | const simpleSitemapAndIndex = async ({ hostname, sitemapHostname = hostname,
|
25 |
|
26 |
|
27 |
|
28 | sourceData, 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())
|
46 | .pipe(fs_1.createWriteStream(writePath));
|
47 | }
|
48 | else {
|
49 | pipeline = sitemapStream.pipe(fs_1.createWriteStream(writePath));
|
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 | };
|
79 | exports.simpleSitemapAndIndex = simpleSitemapAndIndex;
|
80 | exports.default = exports.simpleSitemapAndIndex;
|