]>
Commit | Line | Data |
---|---|---|
1 | 'use strict'; | |
2 | ||
3 | const Promise = require('bluebird'); | |
4 | ||
5 | const spawn = require('cross-spawn'); | |
6 | ||
7 | const debug_ = require('debug'); | |
8 | ||
9 | const debug = debug_('purs-loader'); | |
10 | ||
11 | const debugVerbose = debug_('purs-loader:verbose'); | |
12 | ||
13 | const dargs = require('./dargs'); | |
14 | ||
15 | module.exports = function compile(psModule) { | |
16 | const options = psModule.options | |
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 | const stderr = []; | |
26 | ||
27 | debug('compile %s %O', compileCommand, compileArgs) | |
28 | ||
29 | return new Promise((resolve, reject) => { | |
30 | debug('compiling PureScript...') | |
31 | ||
32 | const compilation = spawn(compileCommand, compileArgs) | |
33 | ||
34 | compilation.stderr.on('data', data => { | |
35 | stderr.push(data.toString()); | |
36 | }); | |
37 | ||
38 | compilation.stdout.on('data', data => { | |
39 | debugVerbose(data.toString()); | |
40 | }); | |
41 | ||
42 | compilation.on('close', code => { | |
43 | debug('finished compiling PureScript.') | |
44 | ||
45 | if (code !== 0) { | |
46 | const errorMessage = stderr.join(''); | |
47 | if (errorMessage.length) { | |
48 | psModule.emitError(errorMessage); | |
49 | } | |
50 | if (options.watch) { | |
51 | resolve(psModule); | |
52 | } | |
53 | else { | |
54 | reject(new Error('compilation failed')) | |
55 | } | |
56 | } else { | |
57 | const warningMessage = stderr.join(''); | |
58 | if (options.warnings && warningMessage.length) { | |
59 | psModule.emitWarning(warningMessage); | |
60 | } | |
61 | resolve(psModule) | |
62 | } | |
63 | }) | |
64 | }); | |
65 | }; |