diff options
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 | }; | ||