@@ -6,12 +6,14 @@ var gutil = require('gulp-util')
66 , cp = require ( 'child_process' )
77 , fs = require ( 'fs' )
88 , path = require ( 'path' )
9+ , which = require ( 'which' )
910 , PLUGIN = 'gulp-purescript'
1011 , DOTPSCI = '.psci'
1112 , LOADM = ':m'
1213 , CWD = process . cwd ( )
1314 , OPTIONS = {
1415 psc : {
16+ cmd : 'psc' ,
1517 flags : {
1618 noPrelude : '--no-prelude' ,
1719 noOpts : '--no-opts' ,
@@ -24,6 +26,7 @@ var gutil = require('gulp-util')
2426 , multi : { modules : '--module' , codegen : '--codegen' }
2527 } ,
2628 pscMake : {
29+ cmd : 'psc-make' ,
2730 flags : {
2831 noPrelude : '--no-prelude' ,
2932 noOpts : '--no-opts' ,
@@ -34,7 +37,8 @@ var gutil = require('gulp-util')
3437 , single : { browserNamespace : '--browser-namespace' , output : '--output' }
3538 , multi : { }
3639 } ,
37- docgen : {
40+ pscDocs : {
41+ cmd : 'psc-docs' ,
3842 flags : {
3943 hierarchy : '--hierarchy-images'
4044 }
@@ -44,6 +48,18 @@ var gutil = require('gulp-util')
4448 }
4549;
4650
51+ function run ( cmd , args , k ) {
52+ var err = [ 'Failed to find ' + gutil . colors . magenta ( cmd ) , 'in your path.'
53+ , 'Please ensure that ' + gutil . colors . magenta ( cmd )
54+ , 'is available on your system.' ] . join ( ' ' )
55+ , that = this
56+ ;
57+ which ( cmd , function ( e ) {
58+ if ( e ) that . emit ( 'error' , new gutil . PluginError ( PLUGIN , err ) ) ;
59+ else k ( cp . spawn ( cmd , args ) ) ;
60+ } ) ;
61+ }
62+
4763function options ( o , opts ) {
4864 return Object . keys ( opts || { } ) . reduce ( function ( b , a ) {
4965 if ( a in o . flags && opts [ a ] === true ) return b . concat ( [ o . flags [ a ] ] ) ;
@@ -85,65 +101,68 @@ function psc(opts) {
85101 // won't receive any input stream from this function.
86102 return acc ( function ( files , cb ) {
87103 var args = files . concat ( options ( OPTIONS . psc , opts$prime ) )
88- , cmd = cp . spawn ( 'psc' , args )
89104 , buffero = new Buffer ( 0 )
90105 , buffere = new Buffer ( 0 )
91106 , that = this
92107 ;
93- cmd . stdout . on ( 'data' , function ( stdout ) { buffero = Buffer . concat ( [ buffero , new Buffer ( stdout ) ] ) ; } ) ;
94- cmd . stderr . on ( 'data' , function ( stderr ) { buffere = Buffer . concat ( [ buffere , new Buffer ( stderr ) ] ) ; } ) ;
95- cmd . on ( 'close' , function ( code ) {
96- if ( ! ! code ) that . emit ( 'error' , new gutil . PluginError ( PLUGIN , buffere . toString ( ) ) ) ;
97- else {
98- that . push ( new gutil . File ( {
99- path : output ,
100- contents : buffero
101- } ) ) ;
102- }
103- cb ( ) ;
104- } ) ;
108+ run . apply ( this , [ OPTIONS . psc . cmd , args , function ( cmd ) {
109+ cmd . stdout . on ( 'data' , function ( stdout ) { buffero = Buffer . concat ( [ buffero , new Buffer ( stdout ) ] ) ; } ) ;
110+ cmd . stderr . on ( 'data' , function ( stderr ) { buffere = Buffer . concat ( [ buffere , new Buffer ( stderr ) ] ) ; } ) ;
111+ cmd . on ( 'close' , function ( code ) {
112+ if ( ! ! code ) that . emit ( 'error' , new gutil . PluginError ( PLUGIN , buffere . toString ( ) ) ) ;
113+ else {
114+ that . push ( new gutil . File ( {
115+ path : output ,
116+ contents : buffero
117+ } ) ) ;
118+ }
119+ cb ( ) ;
120+ } ) ;
121+ } ] ) ;
105122 } ) ;
106123}
107124
108125function pscMake ( opts ) {
109126 return acc ( function ( files , cb ) {
110127 var args = options ( OPTIONS . pscMake , opts ) . concat ( files )
111- , cmd = cp . spawn ( 'psc-make' , args )
112128 , that = this
113129 ;
114- cmd . stdout . on ( 'data' , function ( stdout ) {
115- gutil . log ( 'Stdout from \'' + gutil . colors . cyan ( 'psc-make' ) + '\'\n' + gutil . colors . magenta ( stdout ) ) ;
116- } ) ;
117- cmd . stderr . on ( 'data' , function ( stderr ) {
118- gutil . log ( 'Stderr from \'' + gutil . colors . cyan ( 'psc-make' ) + '\'\n' + gutil . colors . magenta ( stderr ) ) ;
119- } ) ;
120- cmd . on ( 'close' , function ( code ) {
121- if ( ! ! code ) that . emit ( 'error' , new gutil . PluginError ( PLUGIN , 'psc-make has failed' ) ) ;
122- cb ( ) ;
123- } ) ;
130+ run . apply ( this , [ OPTIONS . pscMake . cmd , args , function ( cmd ) {
131+ cmd . stdout . on ( 'data' , function ( stdout ) {
132+ gutil . log ( 'Stdout from \'' + gutil . colors . cyan ( OPTIONS . pscMake . cmd ) + '\'\n' + gutil . colors . magenta ( stdout ) ) ;
133+ } ) ;
134+ cmd . stderr . on ( 'data' , function ( stderr ) {
135+ gutil . log ( 'Stderr from \'' + gutil . colors . cyan ( OPTIONS . pscMake . cmd ) + '\'\n' + gutil . colors . magenta ( stderr ) ) ;
136+ } ) ;
137+ cmd . on ( 'close' , function ( code ) {
138+ if ( ! ! code ) that . emit ( 'error' , new gutil . PluginError ( PLUGIN , OPTIONS . pscMake . cmd + ' has failed' ) ) ;
139+ cb ( ) ;
140+ } ) ;
141+ } ] ) ;
124142 } ) ;
125143}
126144
127145function pscDocs ( opts ) {
128146 return acc ( function ( files , cb ) {
129- var args = options ( OPTIONS . docgen , opts ) . concat ( files )
130- , cmd = cp . spawn ( 'psc-docs' , args )
147+ var args = options ( OPTIONS . pscDocs , opts ) . concat ( files )
131148 , buffero = new Buffer ( 0 )
132149 , buffere = new Buffer ( 0 )
133150 , that = this
134151 ;
135- cmd . stdout . on ( 'data' , function ( stdout ) { buffero = Buffer . concat ( [ buffero , new Buffer ( stdout ) ] ) ; } ) ;
136- cmd . stderr . on ( 'data' , function ( stderr ) { buffere = Buffer . concat ( [ buffere , new Buffer ( stderr ) ] ) ; } ) ;
137- cmd . on ( 'close' , function ( code ) {
138- if ( ! ! code ) that . emit ( 'error' , new gutil . PluginError ( PLUGIN , buffere . toString ( ) ) ) ;
139- else {
140- that . push ( new gutil . File ( {
141- path : '.' ,
142- contents : buffero
143- } ) ) ;
144- }
145- cb ( ) ;
146- } ) ;
152+ run . apply ( this , [ OPTIONS . pscDocs . cmd , args , function ( cmd ) {
153+ cmd . stdout . on ( 'data' , function ( stdout ) { buffero = Buffer . concat ( [ buffero , new Buffer ( stdout ) ] ) ; } ) ;
154+ cmd . stderr . on ( 'data' , function ( stderr ) { buffere = Buffer . concat ( [ buffere , new Buffer ( stderr ) ] ) ; } ) ;
155+ cmd . on ( 'close' , function ( code ) {
156+ if ( ! ! code ) that . emit ( 'error' , new gutil . PluginError ( PLUGIN , buffere . toString ( ) ) ) ;
157+ else {
158+ that . push ( new gutil . File ( {
159+ path : '.' ,
160+ contents : buffero
161+ } ) ) ;
162+ }
163+ cb ( ) ;
164+ } ) ;
165+ } ] ) ;
147166 } ) ;
148167}
149168
0 commit comments