diff options
Diffstat (limited to 'src/Psc.js')
-rw-r--r-- | src/Psc.js | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/src/Psc.js b/src/Psc.js new file mode 100644 index 0000000..9269e0f --- /dev/null +++ b/src/Psc.js | |||
@@ -0,0 +1,92 @@ | |||
1 | 'use strict'; | ||
2 | |||
3 | const path = require('path'); | ||
4 | |||
5 | const Promise = require('bluebird') | ||
6 | |||
7 | const fs = Promise.promisifyAll(require('fs')) | ||
8 | |||
9 | const spawn = require('cross-spawn') | ||
10 | |||
11 | const debug = require('debug')('purs-loader'); | ||
12 | |||
13 | const dargs = require('./dargs'); | ||
14 | |||
15 | function 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) => { | ||
32 | console.log('\nCompiling PureScript...') | ||
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 => { | ||
40 | console.log('Finished compiling PureScript.') | ||
41 | cache.compilationFinished = true | ||
42 | if (code !== 0) { | ||
43 | cache.errors = stderr.join('') | ||
44 | reject(true) | ||
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 | } | ||
58 | module.exports.compile = compile; | ||
59 | |||
60 | function 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) => { | ||
77 | console.log('Bundling PureScript...') | ||
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 => { | ||
84 | if (code !== 0) { | ||
85 | cache.errors = (cache.errors || '') + stderr.join('') | ||
86 | return reject(true) | ||
87 | } | ||
88 | cache.bundle = stderr | ||
89 | resolve(fs.appendFileAsync('output/bundle.js', `module.exports = ${options.bundleNamespace}`)) | ||
90 | }) | ||
91 | })) | ||
92 | } | ||