aboutsummaryrefslogtreecommitdiffstats
path: root/qt-ui/src/designer-client.ts
blob: 6487716f467354f4ed2deed5fa185f7957ad04b3 (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
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
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only

import * as child_process from 'child_process';

import { createLogger } from 'qt-lib';

const logger = createLogger('designer-client');

export class DesignerClient {
  private process: child_process.ChildProcess | undefined;
  private readonly designerExePath: string;
  private readonly serverPort: number | undefined;
  constructor(designerExePath: string, serverPort?: number) {
    this.serverPort = serverPort;
    this.designerExePath = designerExePath;
  }

  public start(serverPort?: number) {
    const designerExePath = this.designerExePath;
    const designerServerPort = serverPort ?? this.serverPort;
    if (!designerServerPort) {
      const err = 'Designer server port is not set';
      logger.error(err);
      throw new Error(err);
    }

    if (designerExePath) {
      this.process = child_process
        .spawn(designerExePath, ['--client ' + designerServerPort.toString()], {
          shell: true
        })
        .on('exit', (number) => {
          this.process = undefined;
          logger.info('Designer client exited with code:' + number);
        })
        .on('error', () => {
          this.process = undefined;
          const message =
            'Failed to start designer client:' +
            'Exe:' +
            designerExePath +
            'Port:' +
            designerServerPort;
          logger.error(message);
          throw new Error(message);
        });
    }
  }
  get exe() {
    return this.designerExePath;
  }
  public isRunning() {
    return this.process !== undefined;
  }

  public stop() {
    if (this.process) {
      logger.debug('Stopping designer client');
      this.process.kill();
    }
  }

  public detach() {
    if (this.process) {
      this.process.unref();
    }
  }

  public dispose() {
    logger.debug('Disposing designer client');
    this.stop();
  }
}