]> git.immae.eu Git - github/fretlink/purs-loader.git/blob - index.js
updating example
[github/fretlink/purs-loader.git] / index.js
1 var cp = require('child_process')
2 , path = require('path')
3 , fs = require('fs')
4 , glob = require('glob')
5 , lodash = require('lodash')
6 , chalk = require('chalk')
7 , lu = require('loader-utils')
8 , cwd = process.cwd()
9 , BOWER_PATTERN = path.join('bower_components', 'purescript-*', 'src')
10 , PSC_MAKE = 'psc-make'
11 , OUTPUT = 'output'
12 , OPTIONS = {
13 'no-prelude': '--no-prelude',
14 'no-opts': '--no-opts',
15 'no-magic-do': '--no-magic-do',
16 'no-tco': '--no-tco',
17 'verbose-errors': '--verbose-errors',
18 'output': '--output'
19 }
20 ;
21
22 function pattern(root) {
23 var as = [ BOWER_PATTERN, root ];
24 return path.join('{' + as.join(',') + '}', '**', '*.purs');
25 }
26
27 module.exports = function(source){
28 var callback = this.async()
29 , request = lu.getRemainingRequest(this)
30 , root = path.dirname(path.relative(cwd, request))
31 , query = lu.parseQuery(this.query)
32 , opts = lodash.foldl(lodash.keys(query), function(acc, k){
33 var h = function(v){return acc.concat(query[k] && OPTIONS[k] ? [v] : []);}
34 if (k === OUTPUT) return h(OPTIONS[k] + '=' + query[k]);
35 else return h(OPTIONS[k]);
36 }, [])
37 ;
38 glob(pattern(root), function(e, files){
39 if (e !== null) callback(e);
40 else {
41 var cmd = cp.spawn(PSC_MAKE, opts.concat(files));
42 cmd.on('close', function(e){
43 if (e) callback(e);
44 else {
45 var module = path.basename(request, '.purs');
46 fs.readFile(path.join(query[OUTPUT] || OUTPUT, module, 'index.js'), 'utf-8', function(e, output){
47 if (e) callback(e);
48 else callback(e, output);
49 });
50 }
51 });
52 cmd.stdout.on('data', function(stdout){
53 console.log('Stdout from \'' + chalk.cyan(PSC_MAKE) + '\'\n' + chalk.magenta(stdout));
54 });
55 cmd.stderr.on('data', function(stderr){
56 console.log('Stderr from \'' + chalk.cyan(PSC_MAKE) + '\'\n' + chalk.magenta(stderr));
57 });
58 }
59 });
60 };