11'use strict' ;
22
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' ;
5026
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 ) {
5242 var err = [ 'Failed to find ' + gutil . colors . magenta ( cmd ) , 'in your path.'
5343 , '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+
5748 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 ) ) ;
6051 } ) ;
6152}
6253
63- function options ( o , opts ) {
54+ function mkOptions ( o , opts ) {
6455 return Object . keys ( opts || { } ) . reduce ( function ( b , a ) {
6556 if ( a in o . flags && opts [ a ] === true ) return b . concat ( [ o . flags [ a ] ] ) ;
6657 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) {
7667 } , [ ] ) ;
7768}
7869
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 ( ) ;
8979 }
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 ) ;
9388}
9489
9590function 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+
9995 // The `output` given there will be passed to gulp, not `psc` command.
10096 // If it was passed to `psc` command, the file will be created and gulp
10197 // 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 ) {
109106 cmd . stdout . on ( 'data' , function ( stdout ) { buffero = Buffer . concat ( [ buffero , new Buffer ( stdout ) ] ) ; } ) ;
107+
110108 cmd . stderr . on ( 'data' , function ( stderr ) { buffere = Buffer . concat ( [ buffere , new Buffer ( stderr ) ] ) ; } ) ;
109+
111110 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 ( ) ) ) ;
113112 else {
114- that . push ( new gutil . File ( {
113+ callback ( null , new File ( {
115114 path : output ,
116115 contents : buffero
117116 } ) ) ;
118117 }
119- cb ( ) ;
120118 } ) ;
121119 } ] ) ;
122- } ) ;
120+ }
121+
122+ return multipipe ( collectPaths ( ) , through2 . obj ( transform ) ) ;
123123}
124124
125125function 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+
137138 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+ }
140149 } ) ;
141150 } ] ) ;
142- } ) ;
151+ } ;
152+
153+ return multipipe ( collectPaths ( ) , through2 . obj ( transform ) ) ;
143154}
144155
145156function 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 ) {
153165 cmd . stdout . on ( 'data' , function ( stdout ) { buffero = Buffer . concat ( [ buffero , new Buffer ( stdout ) ] ) ; } ) ;
166+
154167 cmd . stderr . on ( 'data' , function ( stderr ) { buffere = Buffer . concat ( [ buffere , new Buffer ( stderr ) ] ) ; } ) ;
168+
155169 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 ( ) ) ) ;
157171 else {
158- that . push ( new gutil . File ( {
172+ callback ( null , new File ( {
159173 path : '.' ,
160174 contents : buffero
161175 } ) ) ;
162176 }
163- cb ( ) ;
164177 } ) ;
165178 } ] ) ;
166- } ) ;
179+ }
180+
181+ return multipipe ( collectPaths ( ) , through2 . obj ( transform ) ) ;
167182}
168183
169184function 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 ) ;
174191 }
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 ) ) ;
184195}
185196
186197module . exports = {
0 commit comments