]> git.immae.eu Git - github/fretlink/purs-loader.git/blame - src/Psc.js
Update debug statements during build/bundle
[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) {
43 cache.errors = stderr.join('')
4b99e432 44 reject(new Error('compilation failed'))
531c751f 45 } else {
46 cache.warnings = stderr.join('')
47 resolve(psModule)
48 }
49 })
50 }))
51 .then(compilerOutput => {
52 if (options.bundle) {
53 return bundle(options, cache).then(() => psModule)
54 }
55 return psModule
56 })
57}
58module.exports.compile = compile;
59
60function bundle(options, cache) {
61 if (cache.bundle) return Promise.resolve(cache.bundle)
62
63 const stdout = []
64 const stderr = cache.bundle = []
65
66 const args = dargs(Object.assign({
67 _: [path.join(options.output, '*', '*.js')],
68 output: options.bundleOutput,
69 namespace: options.bundleNamespace,
70 }, options.pscBundleArgs))
71
72 cache.bundleModules.forEach(name => args.push('--module', name))
73
74 debug('spawning bundler %s %o', options.pscBundle, args.join(' '))
75
76 return (new Promise((resolve, reject) => {
514ab146 77 debug('bundling PureScript...')
531c751f 78
79 const compilation = spawn(options.pscBundle, args)
80
81 compilation.stdout.on('data', data => stdout.push(data.toString()))
82 compilation.stderr.on('data', data => stderr.push(data.toString()))
83 compilation.on('close', code => {
514ab146 84 debug('finished bundling PureScript.')
531c751f 85 if (code !== 0) {
86 cache.errors = (cache.errors || '') + stderr.join('')
4b99e432 87 return reject(new Error('bundling failed'))
531c751f 88 }
89 cache.bundle = stderr
896c11ec 90 resolve(fs.appendFileAsync(options.bundleOutput, `module.exports = ${options.bundleNamespace}`))
531c751f 91 })
92 }))
93}