diff options
author | eric thul <thul.eric@gmail.com> | 2017-04-22 10:52:54 -0400 |
---|---|---|
committer | eric <thul.eric@gmail.com> | 2017-04-23 18:20:22 -0400 |
commit | 1c12889c0adf91cf3116a9d5ff44b7466b1dfcc9 (patch) | |
tree | 1f3ac400c629bc040a8c54478363956e37a585b4 /src/compile.js | |
parent | e1bf1f98de548fb2cbaca1a2b6c1d4cb3e173848 (diff) | |
download | purs-loader-1c12889c0adf91cf3116a9d5ff44b7466b1dfcc9.tar.gz purs-loader-1c12889c0adf91cf3116a9d5ff44b7466b1dfcc9.tar.zst purs-loader-1c12889c0adf91cf3116a9d5ff44b7466b1dfcc9.zip |
Support for PureScript 0.11
Resolves #89
Diffstat (limited to 'src/compile.js')
-rw-r--r-- | src/compile.js | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/compile.js b/src/compile.js new file mode 100644 index 0000000..8b5d87f --- /dev/null +++ b/src/compile.js | |||
@@ -0,0 +1,58 @@ | |||
1 | 'use strict'; | ||
2 | |||
3 | const Promise = require('bluebird'); | ||
4 | |||
5 | const spawn = require('cross-spawn'); | ||
6 | |||
7 | const debug = require('debug')('purs-loader'); | ||
8 | |||
9 | const dargs = require('./dargs'); | ||
10 | |||
11 | module.exports = function compile(psModule) { | ||
12 | const options = psModule.options | ||
13 | |||
14 | const cache = psModule.cache | ||
15 | |||
16 | const stderr = [] | ||
17 | |||
18 | const compileCommand = options.psc || 'purs'; | ||
19 | |||
20 | const compileArgs = (options.psc ? [] : [ 'compile' ]).concat(dargs(Object.assign({ | ||
21 | _: options.src, | ||
22 | output: options.output, | ||
23 | }, options.pscArgs))) | ||
24 | |||
25 | debug('spawning compiler %s %o', compileCommand, compileArgs) | ||
26 | |||
27 | return new Promise((resolve, reject) => { | ||
28 | debug('compiling PureScript...') | ||
29 | |||
30 | const compilation = spawn(compileCommand, compileArgs) | ||
31 | |||
32 | compilation.stderr.on('data', data => { | ||
33 | stderr.push(data.toString()); | ||
34 | }); | ||
35 | |||
36 | compilation.on('close', code => { | ||
37 | debug('finished compiling PureScript.') | ||
38 | if (code !== 0) { | ||
39 | const errorMessage = stderr.join(''); | ||
40 | if (errorMessage.length) { | ||
41 | psModule.emitError(errorMessage); | ||
42 | } | ||
43 | if (options.watch) { | ||
44 | resolve(psModule); | ||
45 | } | ||
46 | else { | ||
47 | reject(new Error('compilation failed')) | ||
48 | } | ||
49 | } else { | ||
50 | const warningMessage = stderr.join(''); | ||
51 | if (options.warnings && warningMessage.length) { | ||
52 | psModule.emitWarning(warningMessage); | ||
53 | } | ||
54 | resolve(psModule) | ||
55 | } | ||
56 | }) | ||
57 | }); | ||
58 | }; | ||