1
1
'use strict' ;
2
2
3
- var gutil = require ( 'gulp-util' )
4
- , through = require ( 'through2' )
5
- , lodash = require ( 'lodash' )
6
- , cp = require ( 'child_process' )
7
- , fs = require ( 'fs' )
8
- , path = require ( 'path' )
9
- , which = require ( 'which' )
10
- , PLUGIN = 'gulp-purescript'
11
- , DOTPSCI = '.psci'
12
- , LOADM = ':m'
13
- , CWD = process . cwd ( )
14
- , OPTIONS = {
15
- psc : {
16
- cmd : 'psc' ,
17
- flags : {
18
- noPrelude : '--no-prelude' ,
19
- noOpts : '--no-opts' ,
20
- noMagicDo : '--no-magic-do' ,
21
- noTco : '--no-tco' ,
22
- main : '--main' ,
23
- verboseErrors : '--verbose-errors'
24
- }
25
- , single : { browserNamespace : '--browser-namespace' , externs : '--externs' , main : '--main' , output : '--output' }
26
- , multi : { modules : '--module' , codegen : '--codegen' }
27
- } ,
28
- pscMake : {
29
- cmd : 'psc-make' ,
30
- flags : {
31
- noPrelude : '--no-prelude' ,
32
- noOpts : '--no-opts' ,
33
- noMagicDo : '--no-magic-do' ,
34
- noTco : '--no-tco' ,
35
- verboseErrors : '--verbose-errors'
36
- }
37
- , single : { browserNamespace : '--browser-namespace' , output : '--output' }
38
- , multi : { }
39
- } ,
40
- pscDocs : {
41
- cmd : 'psc-docs' ,
42
- flags : {
43
- hierarchy : '--hierarchy-images'
44
- }
45
- , single : { }
46
- , multi : { }
47
- }
48
- }
49
- ;
3
+ var fs = require ( 'fs' ) ;
4
+
5
+ var path = require ( 'path' ) ;
6
+
7
+ var child_process = require ( 'child_process' ) ;
8
+
9
+ var gutil = require ( 'gulp-util' ) ;
10
+
11
+ var through2 = require ( 'through2' ) ;
12
+
13
+ var lodash = require ( 'lodash' ) ;
14
+
15
+ var which = require ( 'which' ) ;
16
+
17
+ var minimist = require ( 'minimist' ) ;
18
+
19
+ var logalot = require ( 'logalot' ) ;
20
+
21
+ var multipipe = require ( 'multipipe' ) ;
22
+
23
+ var options = require ( './options' ) ;
24
+
25
+ var pluginName = 'gulp-purescript' ;
50
26
51
- function run ( cmd , args , k ) {
27
+ var psciFilename = '.psci' ;
28
+
29
+ var psciLoadCommand = ':m' ;
30
+
31
+ var argv = minimist ( process . argv . slice ( 2 ) ) ;
32
+
33
+ var isVerbose = argv . verbose ;
34
+
35
+ var cwd = process . cwd ( ) ;
36
+
37
+ var PluginError = gutil . PluginError ;
38
+
39
+ var File = gutil . File ;
40
+
41
+ function runCommand ( cmd , args , k ) {
52
42
var err = [ 'Failed to find ' + gutil . colors . magenta ( cmd ) , 'in your path.'
53
43
, 'Please ensure that ' + gutil . colors . magenta ( cmd )
54
- , 'is available on your system.' ] . join ( ' ' )
55
- , that = this
56
- ;
44
+ , 'is available on your system.' ] . join ( ' ' ) ;
45
+
46
+ var that = this ;
47
+
57
48
which ( cmd , function ( e ) {
58
- if ( e ) that . emit ( 'error' , new gutil . PluginError ( PLUGIN , err ) ) ;
59
- else k ( cp . spawn ( cmd , args ) ) ;
49
+ if ( e ) that . emit ( 'error' , new PluginError ( pluginName , err ) ) ;
50
+ else k ( child_process . spawn ( cmd , args ) ) ;
60
51
} ) ;
61
52
}
62
53
63
- function options ( o , opts ) {
54
+ function mkOptions ( o , opts ) {
64
55
return Object . keys ( opts || { } ) . reduce ( function ( b , a ) {
65
56
if ( a in o . flags && opts [ a ] === true ) return b . concat ( [ o . flags [ a ] ] ) ;
66
57
else if ( a in o . single && typeof opts [ a ] === 'string' ) return b . concat ( [ o . single [ a ] + '=' + opts [ a ] ] ) ;
@@ -76,111 +67,131 @@ function options(o, opts) {
76
67
} , [ ] ) ;
77
68
}
78
69
79
- function acc ( f ) {
80
- var files = [ ] ;
81
- return through . obj ( function ( file , env , cb ) {
82
- if ( file . isNull ( ) ) {
83
- this . push ( file ) ;
84
- return cb ( ) ;
85
- }
86
- if ( file . isStream ( ) ) {
87
- this . emit ( 'error' , new gutil . PluginError ( PLUGIN , 'Streaming not supported' ) ) ;
88
- return cb ( ) ;
70
+ function collectPaths ( ) {
71
+ var paths = [ ] ;
72
+
73
+ function transform ( chunk , encoding , callback ) {
74
+ if ( chunk . isNull ( ) ) callback ( null , chunk ) ;
75
+ else if ( chunk . isStream ( ) ) callback ( new PluginError ( pluginName , 'Streaming not supported' ) ) ;
76
+ else {
77
+ paths . push ( chunk . path ) ;
78
+ callback ( ) ;
89
79
}
90
- files . push ( file . path ) ;
91
- cb ( ) ;
92
- } , function ( cb ) { f . apply ( this , [ files , cb ] ) ; } ) ;
80
+ }
81
+
82
+ function flush ( callback ) {
83
+ this . push ( paths ) ;
84
+ callback ( ) ;
85
+ } ;
86
+
87
+ return through2 . obj ( transform , flush ) ;
93
88
}
94
89
95
90
function psc ( opts ) {
96
- var output = opts && opts . output ? opts . output : 'psc.js'
97
- , opts$prime = lodash . omit ( opts || { } , 'output' )
98
- ;
91
+ var output = opts && opts . output ? opts . output : 'psc.js' ;
92
+
93
+ var opts$prime = lodash . omit ( opts || { } , 'output' ) ;
94
+
99
95
// The `output` given there will be passed to gulp, not `psc` command.
100
96
// If it was passed to `psc` command, the file will be created and gulp
101
97
// won't receive any input stream from this function.
102
- return acc ( function ( files , cb ) {
103
- var args = files . concat ( options ( OPTIONS . psc , opts$prime ) )
104
- , buffero = new Buffer ( 0 )
105
- , buffere = new Buffer ( 0 )
106
- , that = this
107
- ;
108
- run . apply ( this , [ OPTIONS . psc . cmd , args , function ( cmd ) {
98
+ function transform ( chunk , encoding , callback ) {
99
+ var args = chunk . concat ( mkOptions ( options . psc , opts$prime ) ) ;
100
+
101
+ var buffero = new Buffer ( 0 ) ;
102
+
103
+ var buffere = new Buffer ( 0 ) ;
104
+
105
+ runCommand . apply ( this , [ options . psc . cmd , args , function ( cmd ) {
109
106
cmd . stdout . on ( 'data' , function ( stdout ) { buffero = Buffer . concat ( [ buffero , new Buffer ( stdout ) ] ) ; } ) ;
107
+
110
108
cmd . stderr . on ( 'data' , function ( stderr ) { buffere = Buffer . concat ( [ buffere , new Buffer ( stderr ) ] ) ; } ) ;
109
+
111
110
cmd . on ( 'close' , function ( code ) {
112
- if ( ! ! code ) that . emit ( 'error' , new gutil . PluginError ( PLUGIN , buffere . toString ( ) ) ) ;
111
+ if ( code !== 0 ) callback ( new PluginError ( pluginName , buffere . toString ( ) ) ) ;
113
112
else {
114
- that . push ( new gutil . File ( {
113
+ callback ( null , new File ( {
115
114
path : output ,
116
115
contents : buffero
117
116
} ) ) ;
118
117
}
119
- cb ( ) ;
120
118
} ) ;
121
119
} ] ) ;
122
- } ) ;
120
+ }
121
+
122
+ return multipipe ( collectPaths ( ) , through2 . obj ( transform ) ) ;
123
123
}
124
124
125
125
function pscMake ( opts ) {
126
- return acc ( function ( files , cb ) {
127
- var args = options ( OPTIONS . pscMake , opts ) . concat ( files )
128
- , that = this
129
- ;
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
- } ) ;
126
+ function transform ( chunk , encoding , callback ) {
127
+ var args = mkOptions ( options . pscMake , opts ) . concat ( chunk ) ;
128
+
129
+ var buffero = new Buffer ( 0 ) ;
130
+
131
+ var buffere = new Buffer ( 0 ) ;
132
+
133
+ runCommand . apply ( this , [ options . pscMake . cmd , args , function ( cmd ) {
134
+ cmd . stdout . on ( 'data' , function ( stdout ) { buffero = Buffer . concat ( [ buffero , new Buffer ( stdout ) ] ) ; } ) ;
135
+
136
+ cmd . stderr . on ( 'data' , function ( stderr ) { buffere = Buffer . concat ( [ buffere , new Buffer ( stderr ) ] ) ; } ) ;
137
+
137
138
cmd . on ( 'close' , function ( code ) {
138
- if ( ! ! code ) that . emit ( 'error' , new gutil . PluginError ( PLUGIN , OPTIONS . pscMake . cmd + ' has failed' ) ) ;
139
- cb ( ) ;
139
+ var message =
140
+ function ( ) { return [ gutil . colors . cyan ( options . pscMake . cmd )
141
+ , buffero . toString ( )
142
+ , buffere . toString ( ) ] . join ( '\n' ) } ;
143
+
144
+ if ( code !== 0 ) callback ( new PluginError ( pluginName , message ( ) ) ) ;
145
+ else {
146
+ if ( isVerbose ) logalot . info ( message ( ) ) ;
147
+ callback ( ) ;
148
+ }
140
149
} ) ;
141
150
} ] ) ;
142
- } ) ;
151
+ } ;
152
+
153
+ return multipipe ( collectPaths ( ) , through2 . obj ( transform ) ) ;
143
154
}
144
155
145
156
function pscDocs ( opts ) {
146
- return acc ( function ( files , cb ) {
147
- var args = options ( OPTIONS . pscDocs , opts ) . concat ( files )
148
- , buffero = new Buffer ( 0 )
149
- , buffere = new Buffer ( 0 )
150
- , that = this
151
- ;
152
- run . apply ( this , [ OPTIONS . pscDocs . cmd , args , function ( cmd ) {
157
+ function transform ( chunk , encoding , callback ) {
158
+ var args = mkOptions ( options . pscDocs , opts ) . concat ( chunk ) ;
159
+
160
+ var buffero = new Buffer ( 0 ) ;
161
+
162
+ var buffere = new Buffer ( 0 ) ;
163
+
164
+ runCommand . apply ( this , [ options . pscDocs . cmd , args , function ( cmd ) {
153
165
cmd . stdout . on ( 'data' , function ( stdout ) { buffero = Buffer . concat ( [ buffero , new Buffer ( stdout ) ] ) ; } ) ;
166
+
154
167
cmd . stderr . on ( 'data' , function ( stderr ) { buffere = Buffer . concat ( [ buffere , new Buffer ( stderr ) ] ) ; } ) ;
168
+
155
169
cmd . on ( 'close' , function ( code ) {
156
- if ( ! ! code ) that . emit ( 'error' , new gutil . PluginError ( PLUGIN , buffere . toString ( ) ) ) ;
170
+ if ( code !== 0 ) callback ( new PluginError ( pluginName , buffere . toString ( ) ) ) ;
157
171
else {
158
- that . push ( new gutil . File ( {
172
+ callback ( null , new File ( {
159
173
path : '.' ,
160
174
contents : buffero
161
175
} ) ) ;
162
176
}
163
- cb ( ) ;
164
177
} ) ;
165
178
} ] ) ;
166
- } ) ;
179
+ }
180
+
181
+ return multipipe ( collectPaths ( ) , through2 . obj ( transform ) ) ;
167
182
}
168
183
169
184
function dotPsci ( opts ) {
170
- var stream = through . obj ( function ( file , env , cb ) {
171
- if ( file . isNull ( ) ) {
172
- this . push ( file ) ;
173
- return cb ( ) ;
185
+ function transform ( chunk , encoding , callback ) {
186
+ if ( chunk . isNull ( ) ) callback ( null , chunk ) ;
187
+ else if ( chunk . isStream ( ) ) callback ( new PluginError ( pluginName , 'Streaming not supported' ) ) ;
188
+ else {
189
+ var buffer = new Buffer ( psciLoadCommand + ' ' + path . relative ( cwd , chunk . path ) + '\n' ) ;
190
+ callback ( null , buffer ) ;
174
191
}
175
- if ( file . isStream ( ) ) {
176
- this . emit ( 'error' , new gutil . PluginError ( PLUGIN , 'Streaming not supported' ) ) ;
177
- return cb ( ) ;
178
- }
179
- this . push ( new Buffer ( LOADM + ' ' + path . relative ( CWD , file . path ) + '\n' ) ) ;
180
- cb ( ) ;
181
- } ) ;
182
- stream . pipe ( fs . createWriteStream ( DOTPSCI ) ) ;
183
- return stream ;
192
+ }
193
+
194
+ return multipipe ( through2 . obj ( transform ) , fs . createWriteStream ( psciFilename ) ) ;
184
195
}
185
196
186
197
module . exports = {
0 commit comments