@@ -6,12 +6,14 @@ var gutil = require('gulp-util')
6
6
, cp = require ( 'child_process' )
7
7
, fs = require ( 'fs' )
8
8
, path = require ( 'path' )
9
+ , which = require ( 'which' )
9
10
, PLUGIN = 'gulp-purescript'
10
11
, DOTPSCI = '.psci'
11
12
, LOADM = ':m'
12
13
, CWD = process . cwd ( )
13
14
, OPTIONS = {
14
15
psc : {
16
+ cmd : 'psc' ,
15
17
flags : {
16
18
noPrelude : '--no-prelude' ,
17
19
noOpts : '--no-opts' ,
@@ -24,6 +26,7 @@ var gutil = require('gulp-util')
24
26
, multi : { modules : '--module' , codegen : '--codegen' }
25
27
} ,
26
28
pscMake : {
29
+ cmd : 'psc-make' ,
27
30
flags : {
28
31
noPrelude : '--no-prelude' ,
29
32
noOpts : '--no-opts' ,
@@ -34,7 +37,8 @@ var gutil = require('gulp-util')
34
37
, single : { browserNamespace : '--browser-namespace' , output : '--output' }
35
38
, multi : { }
36
39
} ,
37
- docgen : {
40
+ pscDocs : {
41
+ cmd : 'psc-docs' ,
38
42
flags : {
39
43
hierarchy : '--hierarchy-images'
40
44
}
@@ -44,6 +48,18 @@ var gutil = require('gulp-util')
44
48
}
45
49
;
46
50
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
+
47
63
function options ( o , opts ) {
48
64
return Object . keys ( opts || { } ) . reduce ( function ( b , a ) {
49
65
if ( a in o . flags && opts [ a ] === true ) return b . concat ( [ o . flags [ a ] ] ) ;
@@ -85,65 +101,68 @@ function psc(opts) {
85
101
// won't receive any input stream from this function.
86
102
return acc ( function ( files , cb ) {
87
103
var args = files . concat ( options ( OPTIONS . psc , opts$prime ) )
88
- , cmd = cp . spawn ( 'psc' , args )
89
104
, buffero = new Buffer ( 0 )
90
105
, buffere = new Buffer ( 0 )
91
106
, that = this
92
107
;
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
+ } ] ) ;
105
122
} ) ;
106
123
}
107
124
108
125
function pscMake ( opts ) {
109
126
return acc ( function ( files , cb ) {
110
127
var args = options ( OPTIONS . pscMake , opts ) . concat ( files )
111
- , cmd = cp . spawn ( 'psc-make' , args )
112
128
, that = this
113
129
;
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
+ } ] ) ;
124
142
} ) ;
125
143
}
126
144
127
145
function pscDocs ( opts ) {
128
146
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 )
131
148
, buffero = new Buffer ( 0 )
132
149
, buffere = new Buffer ( 0 )
133
150
, that = this
134
151
;
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
+ } ] ) ;
147
166
} ) ;
148
167
}
149
168
0 commit comments