aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/PscIde.js
diff options
context:
space:
mode:
authoreric thul <thul.eric@gmail.com>2016-06-12 15:17:44 -0400
committereric thul <thul.eric@gmail.com>2016-06-12 15:17:44 -0400
commit531c751fe5593750a377db38bcfaf9a5383ac661 (patch)
tree935b5f17cc29bee58e27b474fe604a3257b7fc63 /src/PscIde.js
parent7243be70a2163be2230a5f2739768137305a24ef (diff)
downloadpurs-loader-531c751fe5593750a377db38bcfaf9a5383ac661.tar.gz
purs-loader-531c751fe5593750a377db38bcfaf9a5383ac661.tar.zst
purs-loader-531c751fe5593750a377db38bcfaf9a5383ac661.zip
Reduce building of PureScript module map
Resolves #59 and resolves #60
Diffstat (limited to 'src/PscIde.js')
-rw-r--r--src/PscIde.js231
1 files changed, 231 insertions, 0 deletions
diff --git a/src/PscIde.js b/src/PscIde.js
new file mode 100644
index 0000000..d99b639
--- /dev/null
+++ b/src/PscIde.js
@@ -0,0 +1,231 @@
1'use strict';
2
3const path = require('path');
4
5const Promise = require('bluebird');
6
7const fs = Promise.promisifyAll(require('fs'));
8
9const retryPromise = require('promise-retry');
10
11const spawn = require('cross-spawn');
12
13const colors = require('chalk');
14
15const debug = require('debug')('purs-loader');
16
17const dargs = require('./dargs');
18
19const Psc = require('./Psc');
20
21const PsModuleMap = require('./PsModuleMap');
22
23function connect(psModule) {
24 const options = psModule.options
25 const cache = psModule.cache
26
27 if (cache.ideServer) return Promise.resolve(psModule)
28
29 cache.ideServer = true
30
31 const connect_ = () => new Promise((resolve, reject) => {
32 const args = dargs(options.pscIdeArgs)
33
34 debug('attempting to connect to psc-ide-server', args)
35
36 const ideClient = spawn('psc-ide-client', args)
37
38 ideClient.stderr.on('data', data => {
39 debug(data.toString())
40 cache.ideServer = false
41 reject(true)
42 })
43 ideClient.stdout.once('data', data => {
44 debug(data.toString())
45 if (data.toString()[0] === '{') {
46 const res = JSON.parse(data.toString())
47 if (res.resultType === 'success') {
48 cache.ideServer = ideServer
49 resolve(psModule)
50 } else {
51 cache.ideServer = ideServer
52 reject(true)
53 }
54 } else {
55 cache.ideServer = false
56 reject(true)
57 }
58 })
59 ideClient.stdin.resume()
60 ideClient.stdin.write(JSON.stringify({ command: 'load' }))
61 ideClient.stdin.write('\n')
62 })
63
64 const args = dargs(Object.assign({
65 outputDirectory: options.output,
66 }, options.pscIdeArgs))
67
68 debug('attempting to start psc-ide-server', args)
69
70 const ideServer = cache.ideServer = spawn('psc-ide-server', [])
71 ideServer.stderr.on('data', data => {
72 debug(data.toString())
73 })
74
75 return retryPromise((retry, number) => {
76 return connect_().catch(error => {
77 if (!cache.ideServer && number === 9) {
78 debug(error)
79
80 console.log(
81 'failed to connect to or start psc-ide-server, ' +
82 'full compilation will occur on rebuild'
83 )
84
85 return Promise.resolve(psModule)
86 }
87
88 return retry(error)
89 })
90 }, {
91 retries: 9,
92 factor: 1,
93 minTimeout: 333,
94 maxTimeout: 333,
95 })
96}
97module.exports.connect = connect;
98
99function rebuild(psModule) {
100 const options = psModule.options
101 const cache = psModule.cache
102
103 debug('attempting rebuild with psc-ide-client %s', psModule.srcPath)
104
105 const request = (body) => new Promise((resolve, reject) => {
106 const args = dargs(options.pscIdeArgs)
107 const ideClient = spawn('psc-ide-client', args)
108
109 var stdout = ''
110 var stderr = ''
111
112 ideClient.stdout.on('data', data => {
113 stdout = stdout + data.toString()
114 })
115
116 ideClient.stderr.on('data', data => {
117 stderr = stderr + data.toString()
118 })
119
120 ideClient.on('close', code => {
121 if (code !== 0) {
122 const error = stderr === '' ? 'Failed to spawn psc-ide-client' : stderr
123 return reject(new Error(error))
124 }
125
126 let res = null
127
128 try {
129 res = JSON.parse(stdout.toString())
130 debug(res)
131 } catch (err) {
132 return reject(err)
133 }
134
135 if (res && !Array.isArray(res.result)) {
136 return res.resultType === 'success'
137 ? resolve(psModule)
138 : reject('psc-ide rebuild failed')
139 }
140
141 Promise.map(res.result, (item, i) => {
142 debug(item)
143 return formatIdeResult(item, options, i, res.result.length)
144 })
145 .then(compileMessages => {
146 if (res.resultType === 'error') {
147 if (res.result.some(item => item.errorCode === 'UnknownModule' || item.errorCode === 'UnknownName')) {
148 debug('unknown module, attempting full recompile')
149 return Psc.compile(psModule)
150 .then(() => PsModuleMap.makeMap(options.src).then(map => {
151 debug('rebuilt module map');
152 cache.psModuleMap = map;
153 }))
154 .then(() => request({ command: 'load' }))
155 .then(resolve)
156 .catch(() => reject('psc-ide rebuild failed'))
157 }
158 cache.errors = compileMessages.join('\n')
159 reject('psc-ide rebuild failed')
160 } else {
161 cache.warnings = compileMessages.join('\n')
162 resolve(psModule)
163 }
164 })
165 })
166
167 ideClient.stdin.write(JSON.stringify(body))
168 ideClient.stdin.write('\n')
169 })
170
171 return request({
172 command: 'rebuild',
173 params: {
174 file: psModule.srcPath,
175 }
176 })
177}
178module.exports.rebuild = rebuild;
179
180function formatIdeResult(result, options, index, length) {
181 const srcPath = path.relative(options.context, result.filename)
182 const pos = result.position
183 const fileAndPos = `${srcPath}:${pos.startLine}:${pos.startColumn}`
184 let numAndErr = `[${index+1}/${length} ${result.errorCode}]`
185 numAndErr = options.pscIdeColors ? colors.yellow(numAndErr) : numAndErr
186
187 return fs.readFileAsync(result.filename, 'utf8').then(source => {
188 const lines = source.split('\n').slice(pos.startLine - 1, pos.endLine)
189 const endsOnNewline = pos.endColumn === 1 && pos.startLine !== pos.endLine
190 const up = options.pscIdeColors ? colors.red('^') : '^'
191 const down = options.pscIdeColors ? colors.red('v') : 'v'
192 let trimmed = lines.slice(0)
193
194 if (endsOnNewline) {
195 lines.splice(lines.length - 1, 1)
196 pos.endLine = pos.endLine - 1
197 pos.endColumn = lines[lines.length - 1].length || 1
198 }
199
200 // strip newlines at the end
201 if (endsOnNewline) {
202 trimmed = lines.reverse().reduce((trimmed, line, i) => {
203 if (i === 0 && line === '') trimmed.trimming = true
204 if (!trimmed.trimming) trimmed.push(line)
205 if (trimmed.trimming && line !== '') {
206 trimmed.trimming = false
207 trimmed.push(line)
208 }
209 return trimmed
210 }, []).reverse()
211 pos.endLine = pos.endLine - (lines.length - trimmed.length)
212 pos.endColumn = trimmed[trimmed.length - 1].length || 1
213 }
214
215 const spaces = ' '.repeat(String(pos.endLine).length)
216 let snippet = trimmed.map((line, i) => {
217 return ` ${pos.startLine + i} ${line}`
218 }).join('\n')
219
220 if (trimmed.length === 1) {
221 snippet += `\n ${spaces} ${' '.repeat(pos.startColumn - 1)}${up.repeat(pos.endColumn - pos.startColumn + 1)}`
222 } else {
223 snippet = ` ${spaces} ${' '.repeat(pos.startColumn - 1)}${down}\n${snippet}`
224 snippet += `\n ${spaces} ${' '.repeat(pos.endColumn - 1)}${up}`
225 }
226
227 return Promise.resolve(
228 `\n${numAndErr} ${fileAndPos}\n\n${snippet}\n\n${result.message}`
229 )
230 })
231}