-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler.js
87 lines (72 loc) · 2.79 KB
/
handler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const Path = require('path');
const mkdirp = require('mkdirp');
const Async = require('async');
const scrollIndex = require('./lib/scroll-index');
const hitToSitemapEntry = require('./lib/hit-to-sitemap-entry');
const createSitemap = require('./lib/create-sitemap');
const createSitemapIndex = require('./lib/create-sitemap-index');
const uploadFiles = require('./lib/upload-files');
const addKeySerps = require('./lib/add-key-serps');
module.exports = (elastic, s3, settings) => {
const sitemapDir = Path.join(settings.tmpDir || 'tmp', 'sitemap' + Date.now().toString());
return (event, context, cb) => {
Async.waterfall([
// Create tmp dir for storing the created sitemaps
(cb) => mkdirp(sitemapDir, (err) => cb(err)),
// Add key SERP pages to sitemap
(cb) => {
const sitemaps = [];
addKeySerps(elastic, settings, sitemaps, cb);
},
(sitemaps, cb) => {
const filename = `sitemap-${0}.xml`;
const filePath = Path.join(sitemapDir, filename);
createSitemap(sitemaps, filePath, (err) => {
if (err) return cb(err);
console.log(`${filename} created`);
cb(null, [filename]);
});
},
// Create each sitemap
(sitemaps, cb) => {
scrollIndex(elastic, {
batchSize: settings.maxSitemapUrls,
pageSize: settings.pageSize,
fields: ['@admin.processed', '@datatype.base', 'summary.title', 'multimedia'],
// Transform a hit into a sitemap entry
onHit: (hit) => hitToSitemapEntry(hit, settings.siteUrl, settings.imageSiteUrl),
// Transform a batch of sitemap entries into a sitemap.xml
onBatch: (entries, cb) => {
const filename = `sitemap-${sitemaps.length}.xml`;
const filePath = Path.join(sitemapDir, filename);
createSitemap(entries, filePath, (err) => {
if (err) return cb(err);
console.log(`${filename} created`);
sitemaps.push(filename);
cb();
});
}
}, (err) => cb(err, sitemaps));
},
// Create sitemap index file
(sitemaps, cb) => {
const filePath = Path.join(sitemapDir, 'sitemap.xml');
createSitemapIndex(sitemaps, settings.sitemapUrl, filePath, (err) => {
if (err) return cb(err);
cb(null, ['sitemap.xml'].concat(sitemaps));
});
},
// Upload to s3
(sitemaps, cb) => {
uploadFiles(s3, sitemaps, sitemapDir, settings.s3.bucket, (err) => cb(err, sitemaps));
}
], (err, sitemaps) => {
if (err) {
console.error('Failed to create sitemap.xml', err);
return cb(err);
}
console.log('sitemap.xml created');
cb(null, sitemaps.map((s) => `${settings.sitemapUrl}/${s}`));
});
};
};