UNPKG

5.2 kBJavaScriptView Raw
1#!/usr/bin/env node
2"use strict";
3Object.defineProperty(exports, "__esModule", { value: true });
4const fs_1 = require("fs");
5const xmllint_1 = require("./lib/xmllint");
6const errors_1 = require("./lib/errors");
7const sitemap_parser_1 = require("./lib/sitemap-parser");
8const utils_1 = require("./lib/utils");
9const sitemap_stream_1 = require("./lib/sitemap-stream");
10const sitemap_index_stream_1 = require("./lib/sitemap-index-stream");
11const url_1 = require("url");
12const zlib_1 = require("zlib");
13const types_1 = require("./lib/types");
14/* eslint-disable-next-line @typescript-eslint/no-var-requires */
15const arg = require('arg');
16const pickStreamOrArg = (argv) => {
17 if (!argv._.length) {
18 return process.stdin;
19 }
20 else {
21 return (0, fs_1.createReadStream)(argv._[0], { encoding: 'utf8' });
22 }
23};
24const argSpec = {
25 '--help': Boolean,
26 '--version': Boolean,
27 '--validate': Boolean,
28 '--index': Boolean,
29 '--index-base-url': String,
30 '--limit': Number,
31 '--parse': Boolean,
32 '--single-line-json': Boolean,
33 '--prepend': String,
34 '--gzip': Boolean,
35 '-h': '--help',
36};
37const argv = arg(argSpec);
38function getStream() {
39 if (argv._ && argv._.length) {
40 return (0, fs_1.createReadStream)(argv._[0]);
41 }
42 else {
43 console.warn('Reading from stdin. If you are not piping anything in, this command is not doing anything');
44 return process.stdin;
45 }
46}
47if (argv['--version']) {
48 /* eslint-disable-next-line @typescript-eslint/no-var-requires */
49 const packagejson = require('../package.json');
50 console.log(packagejson.version);
51}
52else if (argv['--help']) {
53 console.log(`
54Turn a list of urls into a sitemap xml.
55Options:
56 --help Print this text
57 --version Print the version
58 --validate Ensure the passed in file is conforms to the sitemap spec
59 --index Create an index and stream that out. Writes out sitemaps along the way.
60 --index-base-url Base url the sitemaps will be hosted eg. https://example.com/sitemaps/
61 --limit=45000 Set a custom limit to the items per sitemap
62 --parse Parse fed xml and spit out config
63 --prepend=sitemap.xml Prepend the streamed in sitemap configs to sitemap.xml
64 --gzip Compress output
65 --single-line-json When used with parse, it spits out each entry as json rather than the whole json.
66
67# examples
68
69Generate a sitemap index file as well as sitemaps
70 npx sitemap --gzip --index --index-base-url https://example.com/path/to/sitemaps/ < listofurls.txt > sitemap-index.xml.gz
71
72Add to a sitemap
73 npx sitemap --prepend sitemap.xml < listofurls.json
74
75Turn an existing sitemap into configuration understood by the sitemap library
76 npx sitemap --parse sitemap.xml
77
78Use XMLLib to validate your sitemap (requires xmllib)
79 npx sitemap --validate sitemap.xml
80`);
81}
82else if (argv['--parse']) {
83 let oStream = getStream()
84 .pipe(new sitemap_parser_1.XMLToSitemapItemStream({ level: types_1.ErrorLevel.THROW }))
85 .pipe(new sitemap_parser_1.ObjectStreamToJSON({ lineSeparated: !argv['--single-line-json'] }));
86 if (argv['--gzip']) {
87 oStream = oStream.pipe((0, zlib_1.createGzip)());
88 }
89 oStream.pipe(process.stdout);
90}
91else if (argv['--validate']) {
92 (0, xmllint_1.xmlLint)(getStream())
93 .then(() => console.log('valid'))
94 .catch(([error, stderr]) => {
95 if (error instanceof errors_1.XMLLintUnavailable) {
96 console.error(error.message);
97 return;
98 }
99 else {
100 console.log(stderr);
101 }
102 });
103}
104else if (argv['--index']) {
105 const limit = argv['--limit'];
106 const baseURL = argv['--index-base-url'];
107 if (!baseURL) {
108 throw new Error("You must specify where the sitemaps will be hosted. use --index-base-url 'https://example.com/path'");
109 }
110 const sms = new sitemap_index_stream_1.SitemapAndIndexStream({
111 limit,
112 getSitemapStream: (i) => {
113 const sm = new sitemap_stream_1.SitemapStream();
114 const path = `./sitemap-${i}.xml`;
115 let ws;
116 if (argv['--gzip']) {
117 ws = sm.pipe((0, zlib_1.createGzip)()).pipe((0, fs_1.createWriteStream)(path));
118 }
119 else {
120 ws = sm.pipe((0, fs_1.createWriteStream)(path));
121 }
122 return [new url_1.URL(path, baseURL).toString(), sm, ws];
123 },
124 });
125 let oStream = (0, utils_1.lineSeparatedURLsToSitemapOptions)(pickStreamOrArg(argv)).pipe(sms);
126 if (argv['--gzip']) {
127 oStream = oStream.pipe((0, zlib_1.createGzip)());
128 }
129 oStream.pipe(process.stdout);
130}
131else {
132 const sms = new sitemap_stream_1.SitemapStream();
133 if (argv['--prepend']) {
134 (0, fs_1.createReadStream)(argv['--prepend'])
135 .pipe(new sitemap_parser_1.XMLToSitemapItemStream())
136 .pipe(sms);
137 }
138 const oStream = (0, utils_1.lineSeparatedURLsToSitemapOptions)(pickStreamOrArg(argv)).pipe(sms);
139 if (argv['--gzip']) {
140 oStream.pipe((0, zlib_1.createGzip)()).pipe(process.stdout);
141 }
142 else {
143 oStream.pipe(process.stdout);
144 }
145}