UNPKG

4.5 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");
13/* eslint-disable-next-line @typescript-eslint/no-var-requires */
14const arg = require('arg');
15const pickStreamOrArg = (argv) => {
16 if (!argv._.length) {
17 return process.stdin;
18 }
19 else {
20 return fs_1.createReadStream(argv._[0], { encoding: 'utf8' });
21 }
22};
23const argSpec = {
24 '--help': Boolean,
25 '--version': Boolean,
26 '--validate': Boolean,
27 '--index': Boolean,
28 '--index-base-url': String,
29 '--limit': Number,
30 '--parse': Boolean,
31 '--single-line-json': Boolean,
32 '--prepend': String,
33 '--gzip': Boolean,
34 '--h': '--help',
35};
36const argv = arg(argSpec);
37function getStream() {
38 if (argv._ && argv._.length) {
39 return fs_1.createReadStream(argv._[0]);
40 }
41 else {
42 console.warn('Reading from stdin. If you are not piping anything in, this command is not doing anything');
43 return process.stdin;
44 }
45}
46if (argv['--version']) {
47 /* eslint-disable-next-line @typescript-eslint/no-var-requires */
48 const packagejson = require('../package.json');
49 console.log(packagejson.version);
50}
51else if (argv['--help']) {
52 console.log(`
53Turn a list of urls into a sitemap xml.
54Options:
55 --help Print this text
56 --version Print the version
57 --validate ensure the passed in file is conforms to the sitemap spec
58 --index create an index and stream that out, write out sitemaps along the way
59 --index-base-url base url the sitemaps will be hosted eg. https://example.com/sitemaps/
60 --limit=45000 set a custom limit to the items per sitemap
61 --parse Parse fed xml and spit out config
62 --prepend sitemap.xml < urlsToAdd.json
63 --gzip compress output
64 --single-line-json When used with parse, it spits out each entry as json rather
65 than the whole json.
66`);
67}
68else if (argv['--parse']) {
69 let oStream = getStream()
70 .pipe(new sitemap_parser_1.XMLToSitemapItemStream())
71 .pipe(new sitemap_parser_1.ObjectStreamToJSON({ lineSeparated: !argv['--single-line-json'] }));
72 if (argv['--gzip']) {
73 oStream = oStream.pipe(zlib_1.createGzip());
74 }
75 oStream.pipe(process.stdout);
76}
77else if (argv['--validate']) {
78 xmllint_1.xmlLint(getStream())
79 .then(() => console.log('valid'))
80 .catch(([error, stderr]) => {
81 if (error instanceof errors_1.XMLLintUnavailable) {
82 console.error(error.message);
83 return;
84 }
85 else {
86 console.log(stderr);
87 }
88 });
89}
90else if (argv['--index']) {
91 const limit = argv['--limit'];
92 const baseURL = argv['--index-base-url'];
93 if (!baseURL) {
94 throw new Error("You must specify where the sitemaps will be hosted. use --index-base-url 'https://example.com/path'");
95 }
96 const sms = new sitemap_index_stream_1.SitemapAndIndexStream({
97 limit,
98 getSitemapStream: (i) => {
99 const sm = new sitemap_stream_1.SitemapStream();
100 const path = `./sitemap-${i}.xml`;
101 if (argv['--gzip']) {
102 sm.pipe(zlib_1.createGzip()).pipe(fs_1.createWriteStream(path));
103 }
104 else {
105 sm.pipe(fs_1.createWriteStream(path));
106 }
107 return [new url_1.URL(path, baseURL).toString(), sm];
108 },
109 });
110 let oStream = utils_1.lineSeparatedURLsToSitemapOptions(pickStreamOrArg(argv)).pipe(sms);
111 if (argv['--gzip']) {
112 oStream = oStream.pipe(zlib_1.createGzip());
113 }
114 oStream.pipe(process.stdout);
115}
116else {
117 const sms = new sitemap_stream_1.SitemapStream();
118 if (argv['--prepend']) {
119 fs_1.createReadStream(argv['--prepend'])
120 .pipe(new sitemap_parser_1.XMLToSitemapItemStream())
121 .pipe(sms);
122 }
123 const oStream = utils_1.lineSeparatedURLsToSitemapOptions(pickStreamOrArg(argv)).pipe(sms);
124 if (argv['--gzip']) {
125 oStream.pipe(zlib_1.createGzip()).pipe(process.stdout);
126 }
127 else {
128 oStream.pipe(process.stdout);
129 }
130}