1 | #!/usr/bin/env node
|
2 | "use strict";
|
3 | Object.defineProperty(exports, "__esModule", { value: true });
|
4 | const fs_1 = require("fs");
|
5 | const xmllint_1 = require("./lib/xmllint");
|
6 | const errors_1 = require("./lib/errors");
|
7 | const sitemap_parser_1 = require("./lib/sitemap-parser");
|
8 | const utils_1 = require("./lib/utils");
|
9 | const sitemap_stream_1 = require("./lib/sitemap-stream");
|
10 | const sitemap_index_stream_1 = require("./lib/sitemap-index-stream");
|
11 | const url_1 = require("url");
|
12 | const zlib_1 = require("zlib");
|
13 | const types_1 = require("./lib/types");
|
14 |
|
15 | const arg = require('arg');
|
16 | const 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 | };
|
24 | const 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 | };
|
37 | const argv = arg(argSpec);
|
38 | function 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 | }
|
47 | if (argv['--version']) {
|
48 |
|
49 | const packagejson = require('../package.json');
|
50 | console.log(packagejson.version);
|
51 | }
|
52 | else if (argv['--help']) {
|
53 | console.log(`
|
54 | Turn a list of urls into a sitemap xml.
|
55 | Options:
|
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 |
|
69 | Generate 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 |
|
72 | Add to a sitemap
|
73 | npx sitemap --prepend sitemap.xml < listofurls.json
|
74 |
|
75 | Turn an existing sitemap into configuration understood by the sitemap library
|
76 | npx sitemap --parse sitemap.xml
|
77 |
|
78 | Use XMLLib to validate your sitemap (requires xmllib)
|
79 | npx sitemap --validate sitemap.xml
|
80 | `);
|
81 | }
|
82 | else 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 | }
|
91 | else 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 | }
|
104 | else 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 | }
|
131 | else {
|
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 | }
|