Skip to content

Commit 5750ad1

Browse files
ematipicoastrobot-houston
authored andcommitted
[ci] format
1 parent 0e22462 commit 5750ad1

File tree

2 files changed

+67
-64
lines changed

2 files changed

+67
-64
lines changed

packages/integrations/sitemap/src/index.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import type { AstroConfig, AstroIntegration } from 'astro';
44
import type { EnumChangefreq, LinkItem as LinkItemBase, SitemapItemLoose } from 'sitemap';
55
import { ZodError } from 'zod';
66

7-
import { validateOptions } from './validate-options.js';
87
import { generateSitemap } from './generate-sitemap.js';
8+
import { validateOptions } from './validate-options.js';
99
import { writeSitemap } from './write-sitemap.js';
1010

1111
export { EnumChangefreq as ChangeFreqEnum } from 'sitemap';
@@ -167,13 +167,16 @@ const createPlugin = (options?: SitemapOptions): AstroIntegration => {
167167
}
168168
}
169169
const destDir = fileURLToPath(dir);
170-
await writeSitemap({
171-
hostname: finalSiteUrl.href,
172-
destinationDir: destDir,
173-
publicBasePath: config.base,
174-
sourceData: urlData,
175-
limit: entryLimit
176-
}, config)
170+
await writeSitemap(
171+
{
172+
hostname: finalSiteUrl.href,
173+
destinationDir: destDir,
174+
publicBasePath: config.base,
175+
sourceData: urlData,
176+
limit: entryLimit,
177+
},
178+
config
179+
);
177180
logger.info(`\`${OUTFILE}\` created at \`${path.relative(process.cwd(), destDir)}\``);
178181
} catch (err) {
179182
if (err instanceof ZodError) {
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,69 @@
1+
import { type WriteStream, createWriteStream } from 'fs';
12
import { normalize, resolve } from 'path';
2-
import { createWriteStream, type WriteStream } from 'fs'
3-
import { mkdir } from 'fs/promises';
4-
import { promisify } from 'util';
53
import { Readable, pipeline } from 'stream';
6-
import replace from 'stream-replace-string'
4+
import { promisify } from 'util';
5+
import { mkdir } from 'fs/promises';
6+
import replace from 'stream-replace-string';
77

88
import { SitemapAndIndexStream, SitemapStream } from 'sitemap';
99

1010
import type { AstroConfig } from 'astro';
11-
import type { SitemapItem } from "./index.js";
11+
import type { SitemapItem } from './index.js';
1212

1313
type WriteSitemapConfig = {
14-
hostname: string;
15-
sitemapHostname?: string;
16-
sourceData: SitemapItem[];
17-
destinationDir: string;
18-
publicBasePath?: string;
19-
limit?: number;
20-
}
14+
hostname: string;
15+
sitemapHostname?: string;
16+
sourceData: SitemapItem[];
17+
destinationDir: string;
18+
publicBasePath?: string;
19+
limit?: number;
20+
};
2121

2222
// adapted from sitemap.js/sitemap-simple
23-
export async function writeSitemap({ hostname, sitemapHostname = hostname,
24-
sourceData, destinationDir, limit = 50000, publicBasePath = './', }: WriteSitemapConfig, astroConfig: AstroConfig) {
23+
export async function writeSitemap(
24+
{
25+
hostname,
26+
sitemapHostname = hostname,
27+
sourceData,
28+
destinationDir,
29+
limit = 50000,
30+
publicBasePath = './',
31+
}: WriteSitemapConfig,
32+
astroConfig: AstroConfig
33+
) {
34+
await mkdir(destinationDir, { recursive: true });
35+
36+
const sitemapAndIndexStream = new SitemapAndIndexStream({
37+
limit,
38+
getSitemapStream: (i) => {
39+
const sitemapStream = new SitemapStream({
40+
hostname,
41+
});
42+
const path = `./sitemap-${i}.xml`;
43+
const writePath = resolve(destinationDir, path);
44+
if (!publicBasePath.endsWith('/')) {
45+
publicBasePath += '/';
46+
}
47+
const publicPath = normalize(publicBasePath + path);
2548

26-
await mkdir(destinationDir, { recursive: true })
49+
let stream: WriteStream;
50+
if (astroConfig.trailingSlash === 'never' || astroConfig.build.format === 'file') {
51+
// workaround for trailing slash issue in sitemap.js: https://github.com/ekalinin/sitemap.js/issues/403
52+
const host = hostname.endsWith('/') ? hostname.slice(0, -1) : hostname;
53+
const searchStr = `<loc>${host}/</loc>`;
54+
const replaceStr = `<loc>${host}</loc>`;
55+
stream = sitemapStream
56+
.pipe(replace(searchStr, replaceStr))
57+
.pipe(createWriteStream(writePath));
58+
} else {
59+
stream = sitemapStream.pipe(createWriteStream(writePath));
60+
}
2761

28-
const sitemapAndIndexStream = new SitemapAndIndexStream({
29-
limit,
30-
getSitemapStream: (i) => {
31-
const sitemapStream = new SitemapStream({
32-
hostname,
33-
});
34-
const path = `./sitemap-${i}.xml`;
35-
const writePath = resolve(destinationDir, path);
36-
if (!publicBasePath.endsWith('/')) {
37-
publicBasePath += '/';
38-
}
39-
const publicPath = normalize(publicBasePath + path);
62+
return [new URL(publicPath, sitemapHostname).toString(), sitemapStream, stream];
63+
},
64+
});
4065

41-
let stream: WriteStream
42-
if (astroConfig.trailingSlash === 'never' || astroConfig.build.format === 'file') {
43-
// workaround for trailing slash issue in sitemap.js: https://github.com/ekalinin/sitemap.js/issues/403
44-
const host = hostname.endsWith('/') ? hostname.slice(0, -1) : hostname
45-
const searchStr = `<loc>${host}/</loc>`
46-
const replaceStr = `<loc>${host}</loc>`
47-
stream = sitemapStream.pipe(replace(searchStr, replaceStr)).pipe(createWriteStream(writePath))
48-
} else {
49-
stream = sitemapStream.pipe(createWriteStream(writePath))
50-
}
51-
52-
return [
53-
new URL(
54-
publicPath,
55-
sitemapHostname
56-
).toString(),
57-
sitemapStream,
58-
stream,
59-
];
60-
},
61-
});
62-
63-
let src = Readable.from(sourceData)
64-
const indexPath = resolve(
65-
destinationDir,
66-
`./sitemap-index.xml`
67-
);
68-
return promisify(pipeline)(src, sitemapAndIndexStream, createWriteStream(indexPath));
69-
}
66+
let src = Readable.from(sourceData);
67+
const indexPath = resolve(destinationDir, `./sitemap-index.xml`);
68+
return promisify(pipeline)(src, sitemapAndIndexStream, createWriteStream(indexPath));
69+
}

0 commit comments

Comments
 (0)