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