Skip to content

Commit

Permalink
Merge pull request #329 from ekalinin/usability
Browse files Browse the repository at this point in the history
recursively create destination dir fix #322
  • Loading branch information
derduher authored Sep 6, 2020
2 parents 6f6d845 + 76458b1 commit 3c4752d
Show file tree
Hide file tree
Showing 10 changed files with 1,873 additions and 3,518 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 6.3.0

- simpleSitemap will create the dest directory if it doesn't exist
- allow user to not gzip fixes #322

## 6.2.0

- Add simplified interface for creating sitemaps and index
Expand Down
37 changes: 27 additions & 10 deletions lib/sitemap-simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import {
lineSeparatedURLsToSitemapOptions,
} from '../index';
import { createGzip } from 'zlib';
import { createWriteStream, createReadStream } from 'fs';
import { createWriteStream, createReadStream, promises } from 'fs';
import { resolve } from 'path';
import { Readable, pipeline as pline } from 'stream';
import { SitemapItemLoose } from './types';
import { promisify } from 'util';
import { URL } from 'url';

const pipeline = promisify(pline);
export const simpleSitemapAndIndex = ({
export const simpleSitemapAndIndex = async ({
hostname,
sitemapHostname = hostname, // if different
/**
Expand All @@ -21,24 +21,32 @@ export const simpleSitemapAndIndex = ({
sourceData,
destinationDir,
limit = 50000,
gzip = true,
}: {
hostname: string;
sitemapHostname?: string;
sourceData: SitemapItemLoose | string | Readable | string[];
destinationDir: string;
limit?: number;
gzip?: boolean;
}): Promise<void> => {
await promises.mkdir(destinationDir, { recursive: true });
const sitemapAndIndexStream = new SitemapAndIndexStream({
limit,
getSitemapStream: (i) => {
const sitemapStream = new SitemapStream({
hostname,
});
const path = `./sitemap-${i}.xml`;
const writePath = resolve(destinationDir, path + (gzip ? '.gz' : ''));

sitemapStream
.pipe(createGzip()) // compress the output of the sitemap
.pipe(createWriteStream(resolve(destinationDir, path + '.gz'))); // write it to sitemap-NUMBER.xml
if (gzip) {
sitemapStream
.pipe(createGzip()) // compress the output of the sitemap
.pipe(createWriteStream(writePath)); // write it to sitemap-NUMBER.xml
} else {
sitemapStream.pipe(createWriteStream(writePath)); // write it to sitemap-NUMBER.xml
}

return [new URL(path, sitemapHostname).toString(), sitemapStream];
},
Expand All @@ -55,12 +63,21 @@ export const simpleSitemapAndIndex = ({
"unhandled source type. You've passed in data that is not supported"
);
}
return pipeline(
src,
sitemapAndIndexStream,
createGzip(),
createWriteStream(resolve(destinationDir, './sitemap-index.xml.gz'))

const writePath = resolve(
destinationDir,
`./sitemap-index.xml${gzip ? '.gz' : ''}`
);
if (gzip) {
return pipeline(
src,
sitemapAndIndexStream,
createGzip(),
createWriteStream(writePath)
);
} else {
return pipeline(src, sitemapAndIndexStream, createWriteStream(writePath));
}
};

export default simpleSitemapAndIndex;
Loading

0 comments on commit 3c4752d

Please sign in to comment.