UNPKG

3.64 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7exports.default = function () {
8 var _ref = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
9
10 var _ref$configFile = _ref.configFile;
11 var configFile = _ref$configFile === undefined ? null : _ref$configFile;
12 var _ref$policy = _ref.policy;
13 var policy = _ref$policy === undefined ? [{
14 allow: '/',
15 cleanParam: null,
16 crawlDelay: null,
17 userAgent: '*'
18 }] : _ref$policy;
19 var _ref$sitemap = _ref.sitemap;
20 var sitemap = _ref$sitemap === undefined ? null : _ref$sitemap;
21 var _ref$host = _ref.host;
22 var host = _ref$host === undefined ? null : _ref$host;
23
24 var options = {
25 host: host,
26 policy: policy,
27 sitemap: sitemap
28 };
29
30 var starter = Promise.resolve();
31
32 if (configFile) {
33 starter = new Promise(function (resolve) {
34 var optionsFromConfigFile = require(configFile); // eslint-disable-line global-require
35
36 options = Object.assign({}, options, optionsFromConfigFile.default ? optionsFromConfigFile.default : optionsFromConfigFile);
37
38 return resolve();
39 });
40 }
41
42 return starter.then(function () {
43 return new Promise(function (resolve, reject) {
44 if (!Array.isArray(options.policy)) {
45 return reject(new Error('Options "policy" must be array'));
46 }
47
48 if (Array.isArray(host)) {
49 return reject(new Error('Options "host" must be one'));
50 }
51
52 var contents = '';
53 var counter = 0;
54
55 options.policy.forEach(function (item) {
56 if (!item.userAgent || item.userAgent && item.userAgent.length === 0) {
57 return reject(new Error('Each "police" should have "User-agent"'));
58 }
59
60 contents += addLine('User-agent', item.userAgent);
61
62 if (item.allow) {
63 contents += addLine('Allow', item.allow);
64 }
65
66 if (item.disallow) {
67 contents += addLine('Disallow', item.disallow);
68 }
69
70 if (item.crawlDelay && typeof item.crawlDelay !== 'number' && !isFinite(item.crawlDelay)) {
71 return reject(new Error('Options "crawlDelay" must be integer or float'));
72 }
73
74 if (item.crawlDelay) {
75 contents += addLine('Crawl-delay', item.crawlDelay);
76 }
77
78 if (item.cleanParam) {
79 contents += addLine('Clean-param', item.cleanParam);
80 }
81
82 counter++;
83
84 if (counter !== options.policy.length) {
85 contents += '\n';
86 }
87
88 return item;
89 });
90
91 if (options.sitemap) {
92 contents += addLine('Sitemap', options.sitemap);
93 }
94
95 if (options.host) {
96 contents += addLine('Host', options.host);
97 }
98
99 return resolve(contents);
100 });
101 });
102};
103
104function capitaliseFirstLetter(string) {
105 return string.charAt(0).toUpperCase() + string.slice(1);
106}
107
108function addLine(name, rule) {
109 var contents = '';
110
111 if (rule && Object.prototype.toString.call(rule) === '[object Array]') {
112 rule.forEach(function (item) {
113 contents += addLine(name, item);
114 });
115 } else {
116 contents += capitaliseFirstLetter(name.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase()) + ': ' + rule + '\n';
117 }
118
119 return contents;
120}
\No newline at end of file