]> git.immae.eu Git - github/fretlink/purs-loader.git/blame - src/Psc.js
Use emitError and emitWarning
[github/fretlink/purs-loader.git] / src / Psc.js
CommitLineData
531c751f 1'use strict';
2
3const path = require('path');
4
5const Promise = require('bluebird')
6
7const fs = Promise.promisifyAll(require('fs'))
8
9const spawn = require('cross-spawn')
10
11const debug = require('debug')('purs-loader');
12
13const dargs = require('./dargs');
14
15function compile(psModule) {
16 const options = psModule.options
17 const cache = psModule.cache
18 const stderr = []
19
20 if (cache.compilationStarted) return Promise.resolve(psModule)
21
22 cache.compilationStarted = true
23
24 const args = dargs(Object.assign({
25 _: options.src,
26 output: options.output,
27 }, options.pscArgs))
28
29 debug('spawning compiler %s %o', options.psc, args)
30
31 return (new Promise((resolve, reject) => {
514ab146 32 debug('compiling PureScript...')
531c751f 33
34 const compilation = spawn(options.psc, args)
35
36 compilation.stdout.on('data', data => stderr.push(data.toString()))
37 compilation.stderr.on('data', data => stderr.push(data.toString()))
38
39 compilation.on('close', code => {
514ab146 40 debug('finished compiling PureScript.')
531c751f 41 cache.compilationFinished = true
42 if (code !== 0) {
45c62a2c 43 const errorMessage = stderr.join('');
44 if (errorMessage.length) {
45 psModule.emitError(errorMessage);
46 }
4b99e432 47 reject(new Error('compilation failed'))
531c751f 48 } else {
45c62a2c 49 const warningMessage = stderr.join('');
50 if (options.warnings && warningMessage.length) {
51 psModule.emitWarning(warningMessage);
52 }
531c751f 53 resolve(psModule)
54 }
55 })
56 }))
57 .then(compilerOutput => {
58 if (options.bundle) {
59 return bundle(options, cache).then(() => psModule)
60 }
61 return psModule
62 })
63}
64module.exports.compile = compile;
65
66function bundle(options, cache) {
67 if (cache.bundle) return Promise.resolve(cache.bundle)
68
69 const stdout = []
70 const stderr = cache.bundle = []
71
72 const args = dargs(Object.assign({
73 _: [path.join(options.output, '*', '*.js')],
74 output: options.bundleOutput,
75 namespace: options.bundleNamespace,
76 }, options.pscBundleArgs))
77
78 cache.bundleModules.forEach(name => args.push('--module', name))
79
80 debug('spawning bundler %s %o', options.pscBundle, args.join(' '))
81
82 return (new Promise((resolve, reject) => {
514ab146 83 debug('bundling PureScript...')
531c751f 84
85 const compilation = spawn(options.pscBundle, args)
86
87 compilation.stdout.on('data', data => stdout.push(data.toString()))
88 compilation.stderr.on('data', data => stderr.push(data.toString()))
89 compilation.on('close', code => {
514ab146 90 debug('finished bundling PureScript.')
531c751f 91 if (code !== 0) {
45c62a2c 92 const errorMessage = stderr.join('');
93 if (errorMessage.length) {
94 psModule.emitError(errorMessage);
95 }
4b99e432 96 return reject(new Error('bundling failed'))
531c751f 97 }
98 cache.bundle = stderr
896c11ec 99 resolve(fs.appendFileAsync(options.bundleOutput, `module.exports = ${options.bundleNamespace}`))
531c751f 100 })
101 }))
102}