diff options
author | eric thul <thul.eric@gmail.com> | 2014-09-21 12:50:12 -0400 |
---|---|---|
committer | eric thul <thul.eric@gmail.com> | 2014-09-22 23:43:29 -0400 |
commit | a42f24b85a304e128c45003b806cc5c4d551b3af (patch) | |
tree | dcd945d01681a2f934f257ca0ba7088ce28f12e4 /index.js | |
parent | 542ab6f00d143b58345672918f831e14f40f8c25 (diff) | |
download | purs-loader-a42f24b85a304e128c45003b806cc5c4d551b3af.tar.gz purs-loader-a42f24b85a304e128c45003b806cc5c4d551b3af.tar.zst purs-loader-a42f24b85a304e128c45003b806cc5c4d551b3af.zip |
Implementation of the loader
Diffstat (limited to 'index.js')
-rw-r--r-- | index.js | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/index.js b/index.js new file mode 100644 index 0000000..a7665f3 --- /dev/null +++ b/index.js | |||
@@ -0,0 +1,55 @@ | |||
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 | , 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', | ||
16 | 'runtime-type-checks': '--runtime-type-checks', | ||
17 | 'verbose-errors': '--verbose-errors', | ||
18 | 'output': '--output' | ||
19 | } | ||
20 | ; | ||
21 | |||
22 | module.exports = function(source){ | ||
23 | var callback = this.async() | ||
24 | , request = lu.getRemainingRequest(this) | ||
25 | , root = path.dirname(path.relative(cwd, request)) | ||
26 | , query = lu.parseQuery(this.query) | ||
27 | , opts = lodash.foldl(lodash.keys(query), function(acc, k){ | ||
28 | var h = function(v){return acc.concat(query[k] && OPTIONS[k] ? [v] : []);} | ||
29 | if (k === OUTPUT) return h(OPTIONS[k] + '=' + query[k]); | ||
30 | else return h(OPTIONS[k]); | ||
31 | }, []) | ||
32 | ; | ||
33 | glob(path.join(root, '**', '*.purs'), function(e, files){ | ||
34 | if (e !== null) callback(e); | ||
35 | else { | ||
36 | var cmd = cp.spawn(PSC_MAKE, opts.concat(files)); | ||
37 | cmd.on('close', function(e){ | ||
38 | if (e) callback(e); | ||
39 | else { | ||
40 | var module = path.basename(request, '.purs'); | ||
41 | fs.readFile(path.join(opts[OPTIONS[OUTPUT]] || OUTPUT, module, 'index.js'), 'utf-8', function(e, output){ | ||
42 | if (e) callback(e); | ||
43 | else callback(e, output); | ||
44 | }); | ||
45 | } | ||
46 | }); | ||
47 | cmd.stdout.on('data', function(stdout){ | ||
48 | console.log('Stdout from \'' + chalk.cyan(PSC_MAKE) + '\'\n' + chalk.magenta(stdout)); | ||
49 | }); | ||
50 | cmd.stderr.on('data', function(stderr){ | ||
51 | console.log('Stderr from \'' + chalk.cyan(PSC_MAKE) + '\'\n' + chalk.magenta(stderr)); | ||
52 | }); | ||
53 | } | ||
54 | }); | ||
55 | }; | ||