@@ -16,6 +16,42 @@ function addLine(name, rule) {
16
16
return contents ;
17
17
}
18
18
19
+ function generatePoliceItem ( item , index ) {
20
+ if ( ! item . userAgent || ( item . userAgent && item . userAgent . length === 0 ) ) {
21
+ throw new Error ( 'Each "police" should have "User-agent"' ) ;
22
+ }
23
+
24
+ let contents = '' ;
25
+
26
+ if ( index !== 0 ) {
27
+ contents += '\n' ;
28
+ }
29
+
30
+ contents += addLine ( 'User-agent' , item . userAgent ) ;
31
+
32
+ if ( item . allow ) {
33
+ contents += addLine ( 'Allow' , item . allow ) ;
34
+ }
35
+
36
+ if ( item . disallow ) {
37
+ contents += addLine ( 'Disallow' , item . disallow ) ;
38
+ }
39
+
40
+ if ( item . crawlDelay && typeof item . crawlDelay !== 'number' && ! isFinite ( item . crawlDelay ) ) {
41
+ throw new Error ( 'Options "crawlDelay" must be integer or float' ) ;
42
+ }
43
+
44
+ if ( item . crawlDelay ) {
45
+ contents += addLine ( 'Crawl-delay' , item . crawlDelay ) ;
46
+ }
47
+
48
+ if ( item . cleanParam ) {
49
+ contents += addLine ( 'Clean-param' , item . cleanParam ) ;
50
+ }
51
+
52
+ return contents ;
53
+ }
54
+
19
55
export default function ( {
20
56
configFile = null ,
21
57
policy = [ {
@@ -63,42 +99,9 @@ export default function ({
63
99
}
64
100
65
101
let contents = '' ;
66
- let counter = 0 ;
67
-
68
- options . policy . forEach ( ( item ) => {
69
- if ( ! item . userAgent || ( item . userAgent && item . userAgent . length === 0 ) ) {
70
- return reject ( new Error ( 'Each "police" should have "User-agent"' ) ) ;
71
- }
72
-
73
- contents += addLine ( 'User-agent' , item . userAgent ) ;
74
-
75
- if ( item . allow ) {
76
- contents += addLine ( 'Allow' , item . allow ) ;
77
- }
78
-
79
- if ( item . disallow ) {
80
- contents += addLine ( 'Disallow' , item . disallow ) ;
81
- }
82
-
83
- if ( item . crawlDelay && typeof item . crawlDelay !== 'number' && ! isFinite ( item . crawlDelay ) ) {
84
- return reject ( new Error ( 'Options "crawlDelay" must be integer or float' ) ) ;
85
- }
86
-
87
- if ( item . crawlDelay ) {
88
- contents += addLine ( 'Crawl-delay' , item . crawlDelay ) ;
89
- }
90
-
91
- if ( item . cleanParam ) {
92
- contents += addLine ( 'Clean-param' , item . cleanParam ) ;
93
- }
94
-
95
- counter ++ ;
96
-
97
- if ( counter !== options . policy . length ) {
98
- contents += '\n' ;
99
- }
100
102
101
- return item ;
103
+ options . policy . forEach ( ( item , index ) => {
104
+ contents += generatePoliceItem ( item , index ) ;
102
105
} ) ;
103
106
104
107
if ( options . sitemap ) {
0 commit comments