UNPKG

8.05 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.SiteMapper = undefined;
7
8var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
9
10var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
11
12var _config = require('./config');
13
14var _fs = require('fs');
15
16var _fs2 = _interopRequireDefault(_fs);
17
18var _underscore = require('underscore');
19
20var _sitemap = require('./sitemap');
21
22var _async = require('async');
23
24var _async2 = _interopRequireDefault(_async);
25
26function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
27
28function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
29
30var SiteMapper = exports.SiteMapper = function () {
31 function SiteMapper(sitemapConfig) {
32 _classCallCheck(this, SiteMapper);
33
34 this.sitemapConfig = (0, _underscore.extend)({}, _config.config.defaultSitemapConfig, sitemapConfig);
35 _config.config.log.info('Site mapper config ', this.sitemapConfig);
36 var buildDir = function buildDir(parentPath, nextPath) {
37 var fullPath = void 0;
38 if (parentPath.length > 0 && parentPath != "/") {
39 fullPath = parentPath + '/' + nextPath;
40 } else {
41 fullPath = '' + parentPath + nextPath;
42 }
43 if (fullPath != "." && fullPath != "") {
44 if (!_fs2.default.existsSync(fullPath)) {
45 _fs2.default.mkdirSync(fullPath);
46 }
47 } else if (fullPath == "") {
48 fullPath = "/";
49 }
50 return fullPath;
51 };
52
53 this.sitemapConfig.targetDirectory.split('/').reduce(buildDir, '');
54 this.sources = this.initializeSources();
55 this.sitemaps = [];
56 }
57
58 _createClass(SiteMapper, [{
59 key: 'initializeSources',
60 value: function initializeSources() {
61 var _this = this;
62
63 var sources = [];
64 var buildSource = function buildSource(sourceDefinition, sourceName) {
65 var sourceConfig = sourceDefinition(_this.sitemapConfig);
66 var sourceClass = sourceConfig.type;
67 var sourceOptions = sourceConfig.options;
68 _config.config.log.trace('Building source ' + sourceName + ' with options ', sourceOptions);
69 if (!sourceOptions.siteMap.urlFormatter) {
70 sourceOptions.siteMap.urlFormatter = _config.config.defaultUrlFormatter(_this.sitemapConfig);
71 }
72 sourceOptions.name = sourceName;
73 return new sourceClass(sourceOptions);
74 };
75 _config.config.log.info("Initializing sources");
76 var includes = this.sitemapConfig.sources && this.sitemapConfig.sources.includes;
77 var excludes = this.sitemapConfig.sources && this.sitemapConfig.sources.excludes;
78 (0, _underscore.each)(_config.config.sources, function (sourceDefinition, sourceName) {
79 if (includes && includes.length > 0) {
80 // If there is an explicit includes list, only include sources in this list
81 if (includes.indexOf(sourceName) >= 0) {
82 _config.config.log.info(' -> Explicit include of source ' + sourceName);
83 sources.push(buildSource(sourceDefinition, sourceName));
84 }
85 } else {
86 if (excludes && excludes.indexOf(sourceName) >= 0) {
87 _config.config.log.info(' -> Explicit exclude of source ' + sourceName);
88 return;
89 }
90 _config.config.log.info(' -> Including ' + sourceName + ' since it is not explicitly excluded');
91 sources.push(buildSource(sourceDefinition, sourceName));
92 }
93 });
94 if (sources.length <= 0) {
95 throw new Error('No sitemap source definitions matched includes: ' + includes);
96 }
97 _config.config.log.info('Initialized ' + sources.length + ' sources');
98 return sources;
99 }
100 }, {
101 key: 'generateSitemap',
102 value: function generateSitemap(done) {
103 var _this2 = this;
104
105 _async2.default.series([function (done) {
106 return _this2.generateFiles(done);
107 }, function (done) {
108 return _this2.generateIndex(done);
109 }], done);
110 }
111 }, {
112 key: 'generateIndex',
113 value: function generateIndex(done) {
114 this.indexFileName = this.sitemapConfig.targetDirectory + '/' + this.sitemapConfig.sitemapIndex;
115 _config.config.log.info('Creating sitemap index: ' + this.indexFileName);
116 var index = _fs2.default.createWriteStream(this.indexFileName);
117 index.on("finish", done);
118 index.on("error", done);
119 index.write(_config.config.sitemapIndexHeader);
120 this.sitemaps.forEach(function (sitemap) {
121 sitemap.allFiles().forEach(function (file) {
122 index.write(file.toIndexXml());
123 });
124 });
125 index.write("</sitemapindex>");
126 index.end();
127 }
128 }, {
129 key: 'generateFiles',
130 value: function generateFiles(done) {
131 var _this3 = this;
132
133 var tasks = this.sources.map(function (source) {
134 return function (cb) {
135 var ended = false;
136 var endTask = function endTask(err) {
137 if (!ended) {
138 ended = true;
139 cb(err);
140 } else {
141 _config.config.log.trace("Tried to call back more than once with: ", err);
142 }
143 };
144 _config.config.log.info('Generating sitemap(s) for source ' + source.name);
145 var sitemap = new _sitemap.Sitemap({ source: source, sitemapConfig: _this3.sitemapConfig }).on('error', function (err) {
146 if (source.ignoreErrors) {
147 _config.config.log.warn('Ignoring error by configuration', err);
148 endTask();
149 } else {
150 endTask(err);
151 }
152 }).on('finish', function () {
153 var closedRemaining = false;
154 var waiting = function waiting() {
155 var _sitemap$totalUrls = sitemap.totalUrls();
156
157 var _sitemap$totalUrls2 = _slicedToArray(_sitemap$totalUrls, 2);
158
159 var pushed = _sitemap$totalUrls2[0];
160 var drained = _sitemap$totalUrls2[1];
161
162 _config.config.log.trace('Waiting for sitemap(s) for source ' + source.name + ', processed ' + drained + '/' + pushed + ' URLS');
163 if (drained < pushed) {
164 setTimeout(waiting, 100);
165 } else {
166 if (!closedRemaining) {
167 closedRemaining = true;
168 sitemap.closeRemaining();
169 setTimeout(waiting, 100);
170 } else {
171 _config.config.log.info('Processed ' + drained + '/' + pushed + ' URLS to sitemap ' + source.name);
172 _this3.sitemaps.push(sitemap);
173 endTask(null);
174 }
175 }
176 };
177 setTimeout(waiting, 100);
178 });
179 sitemap.open();
180 };
181 });
182 _async2.default.series(tasks, function (err, results) {
183 done(err, _this3.sitemaps);
184 });
185 }
186 }]);
187
188 return SiteMapper;
189}();
\No newline at end of file