@@ -5,7 +5,7 @@ module.exports = function( grunt ) {
5
5
const fs = require ( "fs" ) ;
6
6
const path = require ( "path" ) ;
7
7
const which = require ( "which" ) ;
8
- const spawn = require ( "spawnback " ) ;
8
+ const cp = require ( "child_process " ) ;
9
9
const util = require ( "../lib/util" ) ;
10
10
const syntaxHighlight = require ( "../lib/highlight" ) ;
11
11
@@ -30,39 +30,38 @@ function checkXsltproc() {
30
30
return checkLibxml2 ( "xsltproc" ) ;
31
31
}
32
32
33
- grunt . registerMultiTask ( "xmllint" , "Lint xml files" , function ( ) {
33
+ grunt . registerMultiTask ( "xmllint" , "Lint xml files" , async function ( ) {
34
34
var task = this ,
35
35
taskDone = task . async ( ) ;
36
36
37
37
if ( ! checkXmllint ( ) ) {
38
38
return taskDone ( false ) ;
39
39
}
40
40
41
- util . eachFile ( this . filesSrc , function ( fileName , fileDone ) {
41
+ var count = 0 ;
42
+ async function lintFile ( fileName ) {
43
+ count ++ ;
42
44
grunt . verbose . write ( "Linting " + fileName + "..." ) ;
43
- spawn ( "xmllint" , [ "--noout" , fileName ] , function ( error ) {
44
- if ( error ) {
45
- grunt . verbose . error ( ) ;
46
- grunt . log . error ( error ) ;
47
- return fileDone ( ) ;
48
- }
49
-
50
- grunt . verbose . ok ( ) ;
51
- fileDone ( ) ;
52
- } ) ;
53
- } , function ( _error , count ) {
54
- if ( task . errorCount ) {
55
- grunt . warn ( "Task \"" + task . name + "\" failed." ) ;
56
- return taskDone ( ) ;
57
- }
45
+ cp . spawnSync ( "xmllint" , [ "--noout" , fileName ] ) ;
46
+ grunt . verbose . ok ( ) ;
47
+ }
58
48
59
- grunt . log . writeln ( "Lint free files: " + count ) ;
49
+ try {
50
+ await util . eachFile ( this . filesSrc , lintFile ) ;
51
+ } catch ( e ) {
52
+ grunt . verbose . error ( ) ;
53
+ grunt . log . error ( e ) ;
54
+ grunt . warn ( "Task \"" + task . name + "\" failed." ) ;
60
55
taskDone ( ) ;
61
- } ) ;
56
+ return ;
57
+ }
58
+
59
+ grunt . log . writeln ( "Lint free files: " + count ) ;
60
+ taskDone ( ) ;
62
61
} ) ;
63
62
64
63
grunt . registerMultiTask ( "build-xml-entries" ,
65
- "Process API xml files with xsl and syntax highlight" , function ( ) {
64
+ "Process API xml files with xsl and syntax highlight" , async function ( ) {
66
65
var task = this ,
67
66
taskDone = task . async ( ) ,
68
67
targetDir = grunt . config ( "wordpress.dir" ) + "/posts/post/" ;
@@ -73,45 +72,45 @@ grunt.registerMultiTask( "build-xml-entries",
73
72
74
73
grunt . file . mkdir ( targetDir ) ;
75
74
76
- util . eachFile ( this . filesSrc , function ( fileName , fileDone ) {
75
+ var count = 0 ;
76
+ async function transformFile ( fileName ) {
77
+ count ++ ;
77
78
grunt . verbose . write ( "Transforming " + fileName + "..." ) ;
78
- spawn ( "xsltproc" ,
79
- [ "--xinclude" , "entries2html.xsl" , fileName ] ,
80
- function ( error , content , stderr ) {
81
-
82
- // Certain errors won't cause the tranform to fail. For example, a
83
- // broken include will write to stderr, but still exit cleanly.
84
- if ( stderr && ! error ) {
85
- error = new Error ( stderr ) ;
86
- }
79
+ const result = cp . spawnSync ( "xsltproc" ,
80
+ [ "--xinclude" , "entries2html.xsl" , fileName ]
81
+ ) ;
87
82
88
- if ( error ) {
89
- grunt . verbose . error ( ) ;
90
- grunt . log . error ( error ) ;
91
- return fileDone ( error ) ;
92
- }
93
-
94
- grunt . verbose . ok ( ) ;
95
-
96
- var targetFileName = targetDir + path . basename ( fileName , ".xml" ) + ".html" ;
83
+ // Certain errors won't cause the tranform to fail. For example, a
84
+ // broken include will write to stderr, but still exit cleanly.
85
+ const error = result . stderr && result . stderr . toString ( ) ;
86
+ if ( error ) {
87
+ throw new Error ( error ) ;
88
+ }
97
89
98
- // Syntax highlight code blocks
99
- if ( ! grunt . option ( "nohighlight" ) ) {
100
- content = syntaxHighlight ( content ) ;
101
- }
90
+ let content = result . stdout . toString ( ) ;
102
91
103
- grunt . file . write ( targetFileName , content ) ;
104
- fileDone ( ) ;
105
- } ) ;
106
- } , function ( _error , count ) {
107
- if ( task . errorCount ) {
108
- grunt . warn ( "Task \"" + task . name + "\" failed." ) ;
109
- return taskDone ( ) ;
92
+ // Syntax highlight code blocks
93
+ if ( ! grunt . option ( "nohighlight" ) ) {
94
+ content = syntaxHighlight ( content ) ;
110
95
}
111
96
112
- grunt . log . writeln ( "Built " + count + " entries." ) ;
97
+ const targetFileName = targetDir + path . basename ( fileName , ".xml" ) + ".html" ;
98
+ grunt . file . write ( targetFileName , content ) ;
99
+ grunt . verbose . ok ( ) ;
100
+ }
101
+
102
+ try {
103
+ await util . eachFile ( this . filesSrc , transformFile ) ;
104
+ } catch ( e ) {
105
+ grunt . verbose . error ( ) ;
106
+ grunt . log . error ( e ) ;
107
+ grunt . warn ( "Task \"" + task . name + "\" failed." ) ;
113
108
taskDone ( ) ;
114
- } ) ;
109
+ return ;
110
+ }
111
+
112
+ grunt . log . writeln ( "Built " + count + " entries." ) ;
113
+ taskDone ( ) ;
115
114
} ) ;
116
115
117
116
grunt . registerTask ( "build-xml-categories" , function ( ) {
@@ -122,68 +121,70 @@ grunt.registerTask( "build-xml-categories", function() {
122
121
return taskDone ( false ) ;
123
122
}
124
123
125
- spawn ( "xsltproc" ,
126
- [ "--output" , "taxonomies.xml" ,
127
- path . join ( __dirname , "jquery-xml/cat2tax.xsl" ) , "categories.xml" ] ,
128
- function ( error ) {
129
- if ( error ) {
130
- grunt . verbose . error ( ) ;
131
- grunt . log . error ( error ) ;
132
- return taskDone ( ) ;
124
+ try {
125
+ cp . spawnSync ( "xsltproc" , [
126
+ "--output" , "taxonomies.xml" ,
127
+ path . join ( __dirname , "jquery-xml/cat2tax.xsl" ) ,
128
+ "categories.xml"
129
+ ] ) ;
130
+ } catch ( error ) {
131
+ grunt . verbose . error ( ) ;
132
+ grunt . log . error ( error ) ;
133
+ return taskDone ( ) ;
134
+ }
135
+
136
+ try {
137
+ cp . spawnSync ( "xsltproc" , [
138
+ "--output" , targetPath ,
139
+ path . join ( __dirname , "jquery-xml/xml2json.xsl" ) ,
140
+ "taxonomies.xml"
141
+ ] ) ;
142
+ } catch ( error ) {
143
+ grunt . verbose . error ( ) ;
144
+ grunt . log . error ( error ) ;
145
+ return taskDone ( ) ;
146
+ }
147
+
148
+ // xml2json can't determine when to use an array if there is only one child,
149
+ // so we need to ensure all child terms are stored in an array
150
+ var taxonomies = grunt . file . readJSON ( targetPath ) ;
151
+ function normalize ( term ) {
152
+ if ( term . children && term . children . item ) {
153
+ term . children = [ term . children . item ] ;
133
154
}
134
155
135
- spawn ( "xsltproc" ,
136
- [ "--output" , targetPath ,
137
- path . join ( __dirname , "jquery-xml/xml2json.xsl" ) , "taxonomies.xml" ] ,
138
- function ( error ) {
139
- if ( error ) {
140
- grunt . verbose . error ( ) ;
141
- grunt . log . error ( error ) ;
142
- return taskDone ( ) ;
143
- }
156
+ if ( term . children ) {
157
+ term . children . forEach ( normalize ) ;
158
+ }
159
+ }
160
+ taxonomies . category . forEach ( normalize ) ;
161
+ grunt . file . write ( targetPath , JSON . stringify ( taxonomies ) ) ;
144
162
145
- // xml2json can't determine when to use an array if there is only one child,
146
- // so we need to ensure all child terms are stored in an array
147
- var taxonomies = grunt . file . readJSON ( targetPath ) ;
148
- function normalize ( term ) {
149
- if ( term . children && term . children . item ) {
150
- term . children = [ term . children . item ] ;
151
- }
152
-
153
- if ( term . children ) {
154
- term . children . forEach ( normalize ) ;
155
- }
156
- }
157
- taxonomies . category . forEach ( normalize ) ;
158
- grunt . file . write ( targetPath , JSON . stringify ( taxonomies ) ) ;
159
-
160
- // Syntax highlight code blocks
161
- function highlightDescription ( category ) {
162
- if ( category . description ) {
163
- category . description = syntaxHighlight ( category . description ) ;
164
- }
165
- }
163
+ // Syntax highlight code blocks
164
+ function highlightDescription ( category ) {
165
+ if ( category . description ) {
166
+ category . description = syntaxHighlight ( category . description ) ;
167
+ }
168
+ }
166
169
167
- function highlightCategories ( categories ) {
168
- categories . forEach ( function ( category ) {
169
- highlightDescription ( category ) ;
170
- if ( category . children ) {
171
- highlightCategories ( category . children ) ;
172
- }
173
- } ) ;
170
+ function highlightCategories ( categories ) {
171
+ categories . forEach ( function ( category ) {
172
+ highlightDescription ( category ) ;
173
+ if ( category . children ) {
174
+ highlightCategories ( category . children ) ;
174
175
}
176
+ } ) ;
177
+ }
175
178
176
- if ( ! grunt . option ( "nohighlight" ) ) {
177
- taxonomies = grunt . file . readJSON ( targetPath ) ;
178
- highlightCategories ( taxonomies . category ) ;
179
- grunt . file . write ( targetPath , JSON . stringify ( taxonomies ) ) ;
180
- }
179
+ if ( ! grunt . option ( "nohighlight" ) ) {
180
+ taxonomies = grunt . file . readJSON ( targetPath ) ;
181
+ highlightCategories ( taxonomies . category ) ;
182
+ grunt . file . write ( targetPath , JSON . stringify ( taxonomies ) ) ;
183
+ }
181
184
182
- fs . unlinkSync ( "taxonomies.xml" ) ;
183
- grunt . verbose . ok ( ) ;
184
- taskDone ( ) ;
185
- } ) ;
186
- } ) ;
185
+ fs . unlinkSync ( "taxonomies.xml" ) ;
186
+ grunt . verbose . ok ( ) ;
187
+ taskDone ( ) ;
187
188
} ) ;
188
189
189
190
grunt . registerTask ( "build-xml-full" , function ( ) {
@@ -202,26 +203,32 @@ grunt.registerTask( "build-xml-full", function() {
202
203
}
203
204
} ) ;
204
205
205
- spawn ( "xsltproc" ,
206
- [ "--xinclude" , "--path" , process . cwd ( ) ,
207
-
208
- // "--output", grunt.config( "wordpress.dir" ) + "/resources/api.xml",
209
- path . join ( __dirname , "jquery-xml/all-entries.xsl" ) , "all-entries.xml" ] ,
210
- function ( error , result ) {
206
+ let result ;
207
+ let error ;
208
+ try {
209
+ result = cp . spawnSync ( "xsltproc" , [
210
+ "--xinclude" ,
211
+ "--path" , process . cwd ( ) ,
212
+ path . join ( __dirname , "jquery-xml/all-entries.xsl" ) ,
213
+ "all-entries.xml"
214
+ ] ) ;
215
+ } catch ( e ) {
216
+ error = e ;
217
+ }
211
218
212
- // For some reason using --output with xsltproc kills the --xinclude option,
213
- // so we let it write to stdout, then save it to a file
214
- grunt . file . write ( grunt . config ( "wordpress.dir" ) + "/resources/api.xml" , result ) ;
215
- fs . unlinkSync ( "all-entries.xml" ) ;
219
+ // For some reason using --output with xsltproc kills the --xinclude option,
220
+ // so we let it write to stdout, then save it to a file
221
+ const content = result . stdout . toString ( ) ;
222
+ grunt . file . write ( grunt . config ( "wordpress.dir" ) + "/resources/api.xml" , content ) ;
223
+ fs . unlinkSync ( "all-entries.xml" ) ;
216
224
217
- if ( error ) {
218
- grunt . verbose . error ( ) ;
219
- grunt . log . error ( error ) ;
220
- return taskDone ( false ) ;
221
- }
225
+ if ( error ) {
226
+ grunt . verbose . error ( ) ;
227
+ grunt . log . error ( error ) ;
228
+ return taskDone ( false ) ;
229
+ }
222
230
223
- taskDone ( ) ;
224
- } ) ;
231
+ taskDone ( ) ;
225
232
} ) ;
226
233
227
234
} ;
0 commit comments