|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var gutil = require('gulp-util') |
| 4 | + , through = require('through2') |
| 5 | + , cp = require('child_process') |
| 6 | + , PLUGIN = 'gulp-purescript' |
| 7 | + , OPTIONS = { |
| 8 | + psc: { |
| 9 | + flags: { |
| 10 | + noPrelude: '--no-prelude', |
| 11 | + noOpts: '--no-opts', |
| 12 | + noMagicDo: '--no-magic-do', |
| 13 | + noTco: '--no-tco', |
| 14 | + runtimeTypeChecks: '--runtime-type-checks', |
| 15 | + main: '--main', |
| 16 | + verboseErrors: '--verbose-errors' |
| 17 | + } |
| 18 | + , single: {browserNamespace: '--browser-namespace', externs: '--externs', main: '--main'} |
| 19 | + , multi: {modules: '--module', codegen: '--codegen'} |
| 20 | + }, |
| 21 | + pscMake: { |
| 22 | + flags: { |
| 23 | + noPrelude: '--no-prelude', |
| 24 | + noOpts: '--no-opts', |
| 25 | + noMagicDo: '--no-magic-do', |
| 26 | + noTco: '--no-tco', |
| 27 | + runtimeTypeChecks: '--runtime-type-checks', |
| 28 | + verboseErrors: '--verbose-errors' |
| 29 | + } |
| 30 | + , single: {browserNamespace: '--browser-namespace'} |
| 31 | + , multi: {} |
| 32 | + } |
| 33 | + } |
| 34 | +; |
| 35 | + |
| 36 | +function options(o, opts) { |
| 37 | + return Object.keys(opts || {}).reduce(function(b, a){ |
| 38 | + if (a in o.flags && opts[a] === true) return b.concat([o.flags[a]]); |
| 39 | + else if (a in o.single && typeof opts[a] === "string") return b.concat([o.single[a] + '=' + opts[a]]); |
| 40 | + else if (a in o.multi) { |
| 41 | + if (typeof opts[a] === "string") return b.concat([o.multi[a] + '=' + opts[a]]); |
| 42 | + else { |
| 43 | + return b.concat(opts[a].map(function(x){ |
| 44 | + return o.multi[a] + '=' + x; |
| 45 | + })); |
| 46 | + } |
| 47 | + } |
| 48 | + else return b; |
| 49 | + }, []); |
| 50 | +} |
| 51 | + |
| 52 | +function acc(f) { |
| 53 | + var files = []; |
| 54 | + return through.obj(function(file, env, cb){ |
| 55 | + if (file.isNull()) { |
| 56 | + this.push(file); |
| 57 | + return cb(); |
| 58 | + } |
| 59 | + if (file.isStream()) { |
| 60 | + this.emit('error', new gutil.PluginError(PLUGIN, 'Streaming not supported')); |
| 61 | + return cb(); |
| 62 | + } |
| 63 | + files.push(file.path); |
| 64 | + cb(); |
| 65 | + }, function(cb){f.apply(this, [files, cb]);}); |
| 66 | +} |
| 67 | + |
| 68 | +function psc(opts) { |
| 69 | + return acc(function(files, cb){ |
| 70 | + var args = files.concat(options(OPTIONS.psc, opts)) |
| 71 | + , cmd = cp.spawn('psc', args) |
| 72 | + , buffer = new Buffer(0) |
| 73 | + , that = this |
| 74 | + ; |
| 75 | + cmd.stdout.on('data', function(stdout){buffer = Buffer.concat([buffer, new Buffer(stdout)]);}); |
| 76 | + cmd.stderr.on('data', function(stderr){ |
| 77 | + gutil.log('Stderr from \'' + gutil.colors.cyan('psc') + '\'\n' + gutil.colors.magenta(stderr)); |
| 78 | + }); |
| 79 | + cmd.on('close', function(code){ |
| 80 | + if (!!code) that.emit('error', new gutil.PluginError(PLUGIN, buffer.toString())); |
| 81 | + else { |
| 82 | + that.push(new gutil.File({ |
| 83 | + path: 'psc.js', |
| 84 | + contents: buffer |
| 85 | + })); |
| 86 | + } |
| 87 | + cb(); |
| 88 | + }); |
| 89 | + }); |
| 90 | +} |
| 91 | + |
| 92 | +function pscMake(opts) { |
| 93 | + return acc(function(files, cb){ |
| 94 | + var args = options(OPTIONS.pscMake, opts).concat(files) |
| 95 | + , cmd = cp.spawn('psc-make', args) |
| 96 | + , that = this |
| 97 | + ; |
| 98 | + cmd.stdout.on('data', function(stdout){ |
| 99 | + gutil.log('Stdout from \'' + gutil.colors.cyan('psc-make') + '\'\n' + gutil.colors.magenta(stdout)); |
| 100 | + }); |
| 101 | + cmd.stderr.on('data', function(stderr){ |
| 102 | + gutil.log('Stderr from \'' + gutil.colors.cyan('psc-make') + '\'\n' + gutil.colors.magenta(stderr)); |
| 103 | + }); |
| 104 | + cmd.on('close', function(code){ |
| 105 | + if (!!code) that.emit('error', new gutil.PluginError(PLUGIN, 'psc-make has failed')); |
| 106 | + cb(); |
| 107 | + }); |
| 108 | + }); |
| 109 | +} |
| 110 | + |
| 111 | +module.exports = { |
| 112 | + psc: psc, |
| 113 | + pscMake: pscMake |
| 114 | +} |
0 commit comments