aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/compile.js
blob: 707605c6a4724ed3c2fa4c3a222ddb211bde141c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
'use strict';

const Promise = require('bluebird');

const spawn = require('cross-spawn');

const debug_ = require('debug');

const debug = debug_('purs-loader');

const debugVerbose = debug_('purs-loader:verbose');

const dargs = require('./dargs');

module.exports = function compile(psModule) {
  const options = psModule.options

  const compileCommand = options.psc || 'purs';

  const compileArgs = (options.psc ? [] : [ 'compile' ]).concat(dargs(Object.assign({
    _: options.src,
    output: options.output,
  }, options.pscArgs)))

  const stderr = [];

  debug('compile %s %o', compileCommand, compileArgs)

  return new Promise((resolve, reject) => {
    debug('compiling PureScript...')

    const compilation = spawn(compileCommand, compileArgs)

    compilation.stderr.on('data', data => {
      stderr.push(data.toString());
    });

    compilation.stdout.on('data', data => {
      debugVerbose(data.toString());
    });

    compilation.on('close', code => {
      debug('finished compiling PureScript.')

      if (code !== 0) {
        const errorMessage = stderr.join('');
        if (errorMessage.length) {
          psModule.emitError(errorMessage);
        }
        if (options.watch) {
          resolve(psModule);
        }
        else {
          reject(new Error('compilation failed'))
        }
      } else {
        const warningMessage = stderr.join('');
        if (options.warnings && warningMessage.length) {
          psModule.emitWarning(warningMessage);
        }
        resolve(psModule)
      }
    })
  });
};