@@ -142,75 +142,92 @@ var toArray = function (arr) {
142142} ;
143143
144144var spawn = require ( 'child_process' ) . spawn ;
145+ var exec = require ( 'child_process' ) . exec ;
146+ var os = require ( 'os' ) ;
147+
145148var defaultOptions = {
146149 onBuildStart : [ ] ,
147150 onBuildEnd : [ ] ,
148151 onBuildExit : [ ] ,
149152 dev : true ,
150- throwOnExecError : false ,
151153 verbose : false
152154} ;
153155
154- function puts ( error , stdout , stderr ) {
155- if ( error ) {
156- throw error ;
157- }
158- }
159-
160- function serializeScript ( script ) {
161- if ( typeof script === 'string' ) {
162- var _script$split = script . split ( ' ' ) ,
163- _script$split2 = toArray ( _script$split ) ,
164- _command = _script$split2 [ 0 ] ,
165- _args = _script$split2 . slice ( 1 ) ;
166-
167- return { command : _command , args : _args } ;
168- }
169- var command = script . command ,
170- args = script . args ;
171-
172- return { command : command , args : args } ;
173- }
174-
175- function handleScript ( script ) {
176- var _serializeScript = serializeScript ( script ) ,
177- command = _serializeScript . command ,
178- args = _serializeScript . args ;
179-
180- var proc = spawn ( command , args , { stdio : 'inherit' } ) ;
181- proc . on ( 'close' , puts ) ;
182- }
183-
184- function validateInput ( options ) {
185- if ( typeof options . onBuildStart === 'string' ) {
186- options . onBuildStart = options . onBuildStart . split ( '&&' ) ;
187- }
188- if ( typeof options . onBuildEnd === 'string' ) {
189- options . onBuildEnd = options . onBuildEnd . split ( '&&' ) ;
190- }
191- if ( typeof options . onBuildExit === 'string' ) {
192- options . onBuildExit = options . onBuildExit . split ( '&&' ) ;
193- }
194- return options ;
195- }
196-
197- function mergeOptions ( options , defaults ) {
198- for ( var key in defaults ) {
199- if ( options . hasOwnProperty ( key ) ) {
200- defaults [ key ] = options [ key ] ;
201- }
202- }
203- return defaults ;
204- }
205-
206156var WebpackShellPlugin = function ( ) {
207157 function WebpackShellPlugin ( options ) {
208158 classCallCheck ( this , WebpackShellPlugin ) ;
209159
210- this . options = validateInput ( mergeOptions ( options , defaultOptions ) ) ;
160+ this . options = this . validateInput ( this . mergeOptions ( options , defaultOptions ) ) ;
211161 }
212162
213163 createClass ( WebpackShellPlugin , [ {
164+ key : 'puts' ,
165+ value : function puts ( error , stdout , stderr ) {
166+ if ( error ) {
167+ throw error ;
168+ }
169+ }
170+ } , {
171+ key : 'spreadStdoutAndStdErr' ,
172+ value : function spreadStdoutAndStdErr ( proc ) {
173+ proc . stdout . pipe ( process . stdout ) ;
174+ proc . stderr . pipe ( process . stdout ) ;
175+ }
176+ } , {
177+ key : 'serializeScript' ,
178+ value : function serializeScript ( script ) {
179+ if ( typeof script === 'string' ) {
180+ var _script$split = script . split ( ' ' ) ,
181+ _script$split2 = toArray ( _script$split ) ,
182+ _command = _script$split2 [ 0 ] ,
183+ _args = _script$split2 . slice ( 1 ) ;
184+
185+ return { command : _command , args : _args } ;
186+ }
187+ var command = script . command ,
188+ args = script . args ;
189+
190+ return { command : command , args : args } ;
191+ }
192+ } , {
193+ key : 'handleScript' ,
194+ value : function handleScript ( script ) {
195+ if ( os . platform ( ) === 'win32' ) {
196+ this . spreadStdoutAndStdErr ( exec ( script , puts ) ) ;
197+ } else {
198+ var _serializeScript = this . serializeScript ( script ) ,
199+ command = _serializeScript . command ,
200+ args = _serializeScript . args ;
201+
202+ var proc = spawn ( command , args , { stdio : 'inherit' } ) ;
203+ proc . on ( 'close' , this . puts ) ;
204+ }
205+ }
206+ } , {
207+ key : 'validateInput' ,
208+ value : function validateInput ( options ) {
209+ if ( typeof options . onBuildStart === 'string' ) {
210+ options . onBuildStart = options . onBuildStart . split ( '&&' ) ;
211+ }
212+ if ( typeof options . onBuildEnd === 'string' ) {
213+ options . onBuildEnd = options . onBuildEnd . split ( '&&' ) ;
214+ }
215+ if ( typeof options . onBuildExit === 'string' ) {
216+ options . onBuildExit = options . onBuildExit . split ( '&&' ) ;
217+ }
218+ return options ;
219+ }
220+ } , {
221+ key : 'mergeOptions' ,
222+ value : function mergeOptions ( options , defaults ) {
223+ for ( var key in defaults ) {
224+ if ( options . hasOwnProperty ( key ) ) {
225+ defaults [ key ] = options [ key ] ;
226+ }
227+ }
228+ return defaults ;
229+ }
230+ } , {
214231 key : 'apply' ,
215232 value : function apply ( compiler ) {
216233 var _this = this ;
@@ -221,7 +238,9 @@ var WebpackShellPlugin = function () {
221238 }
222239 if ( _this . options . onBuildStart . length ) {
223240 console . log ( 'Executing pre-build scripts' ) ;
224- _this . options . onBuildStart . forEach ( handleScript ) ;
241+ for ( var i = 0 ; i < _this . options . onBuildStart . length ; i ++ ) {
242+ _this . handleScript ( _this . options . onBuildStart [ i ] ) ;
243+ }
225244 if ( _this . options . dev ) {
226245 _this . options . onBuildStart = [ ] ;
227246 }
@@ -231,7 +250,9 @@ var WebpackShellPlugin = function () {
231250 compiler . plugin ( 'emit' , function ( compilation , callback ) {
232251 if ( _this . options . onBuildEnd . length ) {
233252 console . log ( 'Executing post-build scripts' ) ;
234- _this . options . onBuildEnd . forEach ( handleScript ) ;
253+ for ( var i = 0 ; i < _this . options . onBuildEnd . length ; i ++ ) {
254+ _this . handleScript ( _this . options . onBuildEnd [ i ] ) ;
255+ }
235256 if ( _this . options . dev ) {
236257 _this . options . onBuildEnd = [ ] ;
237258 }
@@ -242,7 +263,9 @@ var WebpackShellPlugin = function () {
242263 compiler . plugin ( 'done' , function ( ) {
243264 if ( _this . options . onBuildExit . length ) {
244265 console . log ( 'Executing additional scripts before exit' ) ;
245- _this . options . onBuildExit . forEach ( handleScript ) ;
266+ for ( var i = 0 ; i < _this . options . onBuildExit . length ; i ++ ) {
267+ _this . handleScript ( _this . options . onBuildExit [ i ] ) ;
268+ }
246269 }
247270 } ) ;
248271 }
0 commit comments