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 |
|
14 | const arg = require('arg');
|
15 | const pickStreamOrArg = (argv) => {
|
16 | if (!argv._.length) {
|
17 | return process.stdin;
|
18 | }
|
19 | else {
|
20 | return fs_1.createReadStream(argv._[0], { encoding: 'utf8' });
|
21 | }
|
22 | };
|
23 | const 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 | };
|
36 | const argv = arg(argSpec);
|
37 | function 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 | }
|
46 | if (argv['--version']) {
|
47 |
|
48 | const packagejson = require('../package.json');
|
49 | console.log(packagejson.version);
|
50 | }
|
51 | else if (argv['--help']) {
|
52 | console.log(`
|
53 | Turn a list of urls into a sitemap xml.
|
54 | Options:
|
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 | }
|
68 | else 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 | }
|
77 | else 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 | }
|
90 | else 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 | }
|
116 | else {
|
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 | }
|