aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/PscIde.js
blob: 8a0e823c91ff22b74a81edf95cc2d959cd7f08de (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
'use strict';

const path = require('path');

const Promise = require('bluebird');

const fs = Promise.promisifyAll(require('fs'));

const retryPromise = require('promise-retry');

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

const colors = require('chalk');

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

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

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

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

function connect(psModule) {
  const options = psModule.options
  const cache = psModule.cache

  if (cache.ideServer) return Promise.resolve(psModule)

  cache.ideServer = true

  const connect_ = () => new Promise((resolve, reject) => {
    const args = dargs(options.pscIdeArgs)

    debug('attempting to connect to psc-ide-server', args)

    const ideClient = spawn('psc-ide-client', args)

    ideClient.stderr.on('data', data => {
      debug(data.toString())
      cache.ideServer = false
      reject(new Error('psc-ide-client failed'))
    })
    ideClient.stdout.once('data', data => {
      debug(data.toString())
      if (data.toString()[0] === '{') {
        const res = JSON.parse(data.toString())
        if (res.resultType === 'success') {
          cache.ideServer = ideServer
          resolve(psModule)
        } else {
          cache.ideServer = ideServer
          reject(new Error('psc-ide-client failed'))
        }
      } else {
        cache.ideServer = false
        reject(new Error('psc-ide-client failed'))
      }
    })
    ideClient.stdin.resume()
    ideClient.stdin.write(JSON.stringify({ command: 'load' }))
    ideClient.stdin.write('\n')
  })

  const serverArgs = dargs(Object.assign({
    outputDirectory: options.output,
    "_": options.src
  }, options.pscIdeServerArgs))

  debug('attempting to start psc-ide-server', serverArgs)

  const ideServer = cache.ideServer = spawn('psc-ide-server', serverArgs)
  ideServer.stderr.on('data', data => {
    debug(data.toString())
  })

  return retryPromise((retry, number) => {
    return connect_().catch(error => {
      if (!cache.ideServer && number === 9) {
        debug(error)

        console.warn('Failed to connect to or start psc-ide-server. A full compilation will occur on rebuild');

        return Promise.resolve(psModule)
      }

      return retry(error)
    })
  }, {
    retries: 9,
    factor: 1,
    minTimeout: 333,
    maxTimeout: 333,
  })
}
module.exports.connect = connect;

function rebuild(psModule) {
  const options = psModule.options
  const cache = psModule.cache

  debug('attempting rebuild with psc-ide-client %s', psModule.srcPath)

  const request = (body) => new Promise((resolve, reject) => {
    const args = dargs(options.pscIdeArgs)
    const ideClient = spawn('psc-ide-client', args)

    var stdout = ''
    var stderr = ''

    ideClient.stdout.on('data', data => {
      stdout = stdout + data.toString()
    })

    ideClient.stderr.on('data', data => {
      stderr = stderr + data.toString()
    })

    ideClient.on('close', code => {
      if (code !== 0) {
        const error = stderr === '' ? 'Failed to spawn psc-ide-client' : stderr
        return reject(new Error(error))
      }

      let res = null

      try {
        res = JSON.parse(stdout.toString())
        debug(res)
      } catch (err) {
        return reject(err)
      }

      if (res && !Array.isArray(res.result)) {
        return resolve(psModule);
      }

      Promise.map(res.result, (item, i) => {
        debug(item)
        return formatIdeResult(item, options, i, res.result.length)
      })
      .then(compileMessages => {
        if (res.resultType === 'error') {
          if (res.result.some(item => {
            const isUnknownModule = item.errorCode === 'UnknownModule';

            const isUnknownModuleImport = item.errorCode === 'UnknownName' && /Unknown module/.test(item.message);

            return isUnknownModule || isUnknownModuleImport;
          })) {
            debug('unknown module, attempting full recompile')
            return Psc.compile(psModule)
              .then(() => PsModuleMap.makeMap(options.src).then(map => {
                debug('rebuilt module map');
                cache.psModuleMap = map;
              }))
              .then(() => request({ command: 'load' }))
              .then(resolve)
              .catch(() => resolve(psModule))
          }
          const errorMessage = compileMessages.join('\n');
          if (errorMessage.length) {
            psModule.emitError(errorMessage);
          }
          resolve(psModule);
        } else {
          const warningMessage = compileMessages.join('\n');
          if (options.warnings && warningMessage.length) {
            psModule.emitWarning(warningMessage);
          }
          resolve(psModule);
        }
      })
    })

    ideClient.stdin.write(JSON.stringify(body))
    ideClient.stdin.write('\n')
  })

  return request({
    command: 'rebuild',
    params: {
      file: psModule.srcPath,
    }
  })
}
module.exports.rebuild = rebuild;

function formatIdeResult(result, options, index, length) {
  let numAndErr = `[${index+1}/${length} ${result.errorCode}]`
  numAndErr = options.pscIdeColors ? colors.yellow(numAndErr) : numAndErr

  function makeResult() {
    return Promise.resolve(`\n${numAndErr} ${result.message}`)
  }

  function makeResultSnippet(filename, pos) {
    const srcPath = path.relative(options.context, filename);
    const fileAndPos = `${srcPath}:${pos.startLine}:${pos.startColumn}`

    return fs.readFileAsync(filename, 'utf8').then(source => {
      const lines = source.split('\n').slice(pos.startLine - 1, pos.endLine)
      const endsOnNewline = pos.endColumn === 1 && pos.startLine !== pos.endLine
      const up = options.pscIdeColors ? colors.red('^') : '^'
      const down = options.pscIdeColors ? colors.red('v') : 'v'
      let trimmed = lines.slice(0)

      if (endsOnNewline) {
        lines.splice(lines.length - 1, 1)
        pos.endLine = pos.endLine - 1
        pos.endColumn = lines[lines.length - 1].length || 1
      }

      // strip newlines at the end
      if (endsOnNewline) {
        trimmed = lines.reverse().reduce((trimmed, line, i) => {
          if (i === 0 && line === '') trimmed.trimming = true
          if (!trimmed.trimming) trimmed.push(line)
          if (trimmed.trimming && line !== '') {
            trimmed.trimming = false
            trimmed.push(line)
          }
          return trimmed
        }, []).reverse()
        pos.endLine = pos.endLine - (lines.length - trimmed.length)
        pos.endColumn = trimmed[trimmed.length - 1].length || 1
      }

      const spaces = ' '.repeat(String(pos.endLine).length)
      let snippet = trimmed.map((line, i) => {
        return `  ${pos.startLine + i}  ${line}`
      }).join('\n')

      if (trimmed.length === 1) {
        snippet += `\n  ${spaces}  ${' '.repeat(pos.startColumn - 1)}${up.repeat(pos.endColumn - pos.startColumn + 1)}`
      } else {
        snippet = `  ${spaces}  ${' '.repeat(pos.startColumn - 1)}${down}\n${snippet}`
        snippet += `\n  ${spaces}  ${' '.repeat(pos.endColumn - 1)}${up}`
      }

      return Promise.resolve(`\n${numAndErr} ${fileAndPos}\n\n${snippet}\n\n${result.message}`)
    })
  }

  return result.filename && result.position ? makeResultSnippet(result.filename, result.position) : makeResult();
}