summaryrefslogtreecommitdiffhomepage
path: root/gui/tasks/dev-server.js
blob: 067174ab170c9f2a8424f0e59e44a2992fd7c549 (plain)
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
const serverFactory = require('spa-server');

function startWebServer(done) {
  serverFactory
    .create({
      path: './build',
      port: 8080,
      middleware: [correctWorkingDirectory],
    })
    .start(done);
}

function correctWorkingDirectory(request, response, next) {
  if (request.url === '/src/renderer/index.js') {
    const write = response.write.bind(response);
    response.write = (data) => {
      let s = data.toString();

      // Add code that changes to the correct working directory after `"use strict";` which is
      // located on the first line of the source file.
      const index = s.indexOf('\n');

      if (index !== -1) {
        const insertionIndex = index + 1;
        s =
          s.slice(0, insertionIndex) +
          'try{process.chdir("build/src/renderer")}catch(e){}\n' +
          s.slice(insertionIndex);
      }

      write(s);
    };
  }

  next();
}

startWebServer.displayName = 'start-dev-server';

exports.start = startWebServer;